diff --git a/lib/primes.mbt b/lib/primes.mbt index 7c31af5..35ba9cb 100644 --- a/lib/primes.mbt +++ b/lib/primes.mbt @@ -19,6 +19,24 @@ pub fn to_json(self : Cirru) -> Json { } } +pub impl @json.FromJson for Cirru with from_json(json, path) { + match json { + String(a) => { + Cirru::Leaf(a) + } + Array(xs) => { + let ys = [] + for idx, x in xs { + let v = @json.FromJson::from_json!(x, path.add_index(idx)) + ys.push(v) + } + Cirru::List(ys) + } + // _ => @json.decode_error!((path, "Char::from_json: expected string")) + _ => raise @json.JsonDecodeError((path, "Cirru::from_json: expected string or array")) + } +} + fn Cirru::default() -> Cirru { Cirru::List(Array::new()) } diff --git a/main/main.mbt b/main/main.mbt index 6166897..7fff491 100644 --- a/main/main.mbt +++ b/main/main.mbt @@ -1,4 +1,4 @@ -fn main { +pub fn main_parser() -> Unit { // let tree = @lib.parse?("DEMO") // match tree { // Result::Ok(tree) => { @@ -23,6 +23,26 @@ fn main { } } +fn main { + let demo = "folding" + let demo_file = fsReadSync("./test/cirru/\{demo}.cirru") + let json_file = fsReadSync("./test/data/\{demo}.json") + + let defined= @json.parse?(json_file).unwrap() + let tree: Array[@lib.Cirru] = @json.from_json?(defined).unwrap() + println("TREE:") + for item in tree { + println(item.to_json().stringify()) + } + + let ret = @lib.format?(tree, {use_inline: false}).unwrap() + println("Generated:") + println(ret) + println("Expected:") + println(demo_file) + println("") +} + pub extern "js" fn fsReadSync(path : String) -> String = #|(path) => { #| const fs = require("node:fs");