Skip to content

Commit

Permalink
impl from_json and try writer
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 14, 2024
1 parent e6886d4 commit abb1e2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/primes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
22 changes: 21 additions & 1 deletion main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main {
pub fn main_parser() -> Unit {
// let tree = @lib.parse?("DEMO")
// match tree {
// Result::Ok(tree) => {
Expand All @@ -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");
Expand Down

0 comments on commit abb1e2f

Please sign in to comment.