From dcc0ad816ca31b234757a985599dc29527abce97 Mon Sep 17 00:00:00 2001 From: tiye Date: Thu, 9 Jan 2025 02:43:01 +0800 Subject: [PATCH 1/2] add atom in edn format --- src/lib/edn.mbt | 10 ++++++ src/lib/edn_test.mbt | 7 ++++ src/lib/lib.mbt | 86 ++++++++++++++++++++++++-------------------- src/main/main.mbt | 4 +++ 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/src/lib/edn.mbt b/src/lib/edn.mbt index fcedc88..19205cd 100644 --- a/src/lib/edn.mbt +++ b/src/lib/edn.mbt @@ -20,6 +20,7 @@ pub(all) enum Edn { Buffer(EdnBufferView) /// reference to Rust data, not interpretable in Calcit // AnyRef(EdnAnyRef) + Atom(Edn) } derive(Eq, Hash, Default) ///| @@ -51,9 +52,15 @@ pub fn to_string(self : Edn) -> String { Edn::Set(s) => s.to_string() Edn::Map(m) => m.to_string() Edn::Record(r) => r.to_string() + Edn::Atom(a) => "atom " + a.to_string() } } +///| +pub impl Show for Edn with output(self, logger) -> Unit { + logger.write_string(self.to_string()) +} + // TODO Hash ///| @@ -95,6 +102,9 @@ pub fn compare(self : Edn, right : Edn) -> Int { (Edn::Map(a), Edn::Map(b)) => a.compare(b) (Edn::Map(_), _) => -1 (_, Edn::Map(_)) => 1 + (Edn::Atom(a), Edn::Atom(b)) => a.compare(b) + (Edn::Atom(_), _) => -1 + (_, Edn::Atom(_)) => 1 (Edn::Record(a), Edn::Record(b)) => a.compare(b) // (Edn::Record(_), _) => -1 // (_, Edn::Record(_)) => 1 diff --git a/src/lib/edn_test.mbt b/src/lib/edn_test.mbt index e0e3a7d..7944c45 100644 --- a/src/lib/edn_test.mbt +++ b/src/lib/edn_test.mbt @@ -3,3 +3,10 @@ test "hello" { fail!("@lib.hello() != \"Hello, world!\"") } } + +test "atom inside edn" { + let edn = @lib.Edn::Atom(@lib.Edn::Number(1.0)) + assert_eq!(@lib.format!(edn, true).trim("\n"), "atom 1") + let parsed = @lib.parse!("atom 1") + assert_eq!(parsed, @lib.Edn::Atom(@lib.Edn::Number(1.0))) +} diff --git a/src/lib/lib.mbt b/src/lib/lib.mbt index 992f397..3f69f29 100644 --- a/src/lib/lib.mbt +++ b/src/lib/lib.mbt @@ -56,7 +56,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { if xs.is_empty() { raise EdnCommonError("empty expr is invalid for edn") } else { - println("extracting list: " + xs.to_string()) + // println("extracting list: " + xs.to_string()) match xs[0] { @cirru_parser.Cirru::Leaf(s) => match s { @@ -83,6 +83,12 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { } ret.or_error!(EdnCommonError("missing edn do value")) } + "atom" => + if xs.length() == 2 { + Edn::Atom(extract_cirru_edn!(xs[1])) + } else { + raise EdnCommonError("missing edn atom value") + } "::" => { let mut tag : Edn? = None let extra : Array[Edn] = [] @@ -289,27 +295,26 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { let ys : Array[@cirru_parser.Cirru] = [] ys.push(@cirru_parser.Cirru::Leaf("{}")) let items = Array::from_iter(xs._.iter()) - items.sort_by( - fn(left, right) { - let (a1, a2) = left - let (b1, b2) = right - match - (a1.is_literal(), b1.is_literal(), a2.is_literal(), b2.is_literal()) { - (true, true, true, false) => -1 - (true, true, false, true) => 1 - (true, false, _, _) => -1 - (false, true, _, _) => 1 - // _ => a1.cmp(b1) - _ => a1.compare(b1) - } - }, - ) + items.sort_by(fn(left, right) { + let (a1, a2) = left + let (b1, b2) = right + match + (a1.is_literal(), b1.is_literal(), a2.is_literal(), b2.is_literal()) { + (true, true, true, false) => -1 + (true, true, false, true) => 1 + (true, false, _, _) => -1 + (false, true, _, _) => 1 + // _ => a1.cmp(b1) + _ => a1.compare(b1) + } + }) for pair in items.iter() { let (k, v) = pair ys.push( - @cirru_parser.Cirru::List( - [assemble_cirru_node!(k), assemble_cirru_node!(v)], - ), + @cirru_parser.Cirru::List([ + assemble_cirru_node!(k), + assemble_cirru_node!(v), + ]), ) } @cirru_parser.Cirru::List(ys) @@ -319,26 +324,22 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { ys.push(@cirru_parser.Cirru::Leaf("%{}")) ys.push(@cirru_parser.Cirru::Leaf(":" + name._)) let ordered_entries = entries - ordered_entries.sort_by( - fn(left, right) { - let (_a1, a2) = left - let (_b1, b2) = right - match (a2.is_literal(), b2.is_literal()) { - (true, false) => -1 - (false, true) => 1 - _ => 0 - } - }, - ) + ordered_entries.sort_by(fn(left, right) { + let (_a1, a2) = left + let (_b1, b2) = right + match (a2.is_literal(), b2.is_literal()) { + (true, false) => -1 + (false, true) => 1 + _ => 0 + } + }) for entry in ordered_entries { let v = entry.1 ys.push( - @cirru_parser.Cirru::List( - [ - @cirru_parser.Cirru::Leaf(":" + entry.0._), - assemble_cirru_node!(v), - ], - ), + @cirru_parser.Cirru::List([ + @cirru_parser.Cirru::Leaf(":" + entry.0._), + assemble_cirru_node!(v), + ]), ) } @cirru_parser.Cirru::List(ys) @@ -366,6 +367,12 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { @cirru_parser.Cirru::List(ys) } // Edn::AnyRef(..) => unreachable!("AnyRef is not serializable"), + Edn::Atom(v) => { + let ys : Array[@cirru_parser.Cirru] = [] + ys.push(@cirru_parser.Cirru::Leaf("atom")) + ys.push(assemble_cirru_node!(v)) + @cirru_parser.Cirru::List(ys) + } } } @@ -376,9 +383,10 @@ pub fn format(data : Edn, use_inline : Bool) -> String!EdnCommonError { @cirru_parser.Cirru::Leaf(s) => @cirru_parser.format?( [ - @cirru_parser.Cirru::List( - [@cirru_parser.Cirru::Leaf("do"), @cirru_parser.Cirru::Leaf(s)], - ), + @cirru_parser.Cirru::List([ + @cirru_parser.Cirru::Leaf("do"), + @cirru_parser.Cirru::Leaf(s), + ]), ], options, ) diff --git a/src/main/main.mbt b/src/main/main.mbt index d04b35d..7aeba19 100644 --- a/src/main/main.mbt +++ b/src/main/main.mbt @@ -17,4 +17,8 @@ fn main { } Err(e) => println("error:" + e.to_string()) } + let v = @lib.format?(@lib.Edn::Atom(@lib.Edn::Number(1.0)), true).unwrap() + println(v) + let parsed = @lib.parse?("atom 1").unwrap() + println(parsed.to_string()) } From 10a4e5c49ea4b8cf233dd1322e17fee7980e6743 Mon Sep 17 00:00:00 2001 From: tiye Date: Thu, 9 Jan 2025 02:50:08 +0800 Subject: [PATCH 2/2] simplify enum constructor usage; tag 0.0.5 --- .github/workflows/check.yml | 2 +- moon.mod.json | 2 +- src/lib/edn.mbt | 124 +++++++++++++++--------------- src/lib/edn_test.mbt | 4 +- src/lib/lib.mbt | 149 +++++++++++++++--------------------- 5 files changed, 127 insertions(+), 154 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 68c7e4e..ddbbc61 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -27,7 +27,7 @@ jobs: - name: moon test run: | - # moon test --target js + moon test --target js moon run src/main --target js # - name: moon bundle diff --git a/moon.mod.json b/moon.mod.json index ada6e5d..106a97d 100644 --- a/moon.mod.json +++ b/moon.mod.json @@ -1,6 +1,6 @@ { "name": "tiye/cirru-edn", - "version": "0.0.4", + "version": "0.0.5", "deps": { "tiye/cirru-parser": "0.0.10" }, diff --git a/src/lib/edn.mbt b/src/lib/edn.mbt index 19205cd..2e9dd91 100644 --- a/src/lib/edn.mbt +++ b/src/lib/edn.mbt @@ -26,12 +26,12 @@ pub(all) enum Edn { ///| pub fn to_string(self : Edn) -> String { match self { - Edn::Nil => "nil".to_string() - Edn::Tag(t) => t.to_string() - Edn::Bool(b) => if b { "true" } else { "false" } - Edn::Number(n) => n.to_string() - Edn::Symbol(s) => s - Edn::Str(s) => + Nil => "nil".to_string() + Tag(t) => t.to_string() + Bool(b) => if b { "true" } else { "false" } + Number(n) => n.to_string() + Symbol(s) => s + Str(s) => if is_simple_token(s) { s } else { @@ -45,14 +45,14 @@ pub fn to_string(self : Edn) -> String { } ret } - Edn::Quote(e) => e.to_string() - Edn::Buffer(b) => b.to_string() - Edn::Tuple(t) => t.to_string() - Edn::List(l) => l.to_string() - Edn::Set(s) => s.to_string() - Edn::Map(m) => m.to_string() - Edn::Record(r) => r.to_string() - Edn::Atom(a) => "atom " + a.to_string() + Quote(e) => e.to_string() + Buffer(b) => b.to_string() + Tuple(t) => t.to_string() + List(l) => l.to_string() + Set(s) => s.to_string() + Map(m) => m.to_string() + Record(r) => r.to_string() + Atom(a) => "atom " + a.to_string() } } @@ -66,48 +66,48 @@ pub impl Show for Edn with output(self, logger) -> Unit { ///| pub fn compare(self : Edn, right : Edn) -> Int { let ret = match (self, right) { - (Edn::Nil, Edn::Nil) => 0 - (Edn::Nil, _) => -1 - (_, Edn::Nil) => 1 - (Edn::Bool(a), Edn::Bool(b)) => a.compare(b) - (Edn::Bool(_), _) => -1 - (_, Edn::Bool(_)) => 1 - (Edn::Number(a), Edn::Number(b)) => a.compare(b) - (Edn::Number(_), _) => -1 - (_, Edn::Number(_)) => 1 - (Edn::Symbol(a), Edn::Symbol(b)) => a.compare(b) - (Edn::Symbol(_), _) => -1 - (_, Edn::Symbol(_)) => 1 - (Edn::Tag(a), Edn::Tag(b)) => a.compare(b) - (Edn::Tag(_), _) => -1 - (_, Edn::Tag(_)) => 1 - (Edn::Str(a), Edn::Str(b)) => a.compare(b) - (Edn::Str(_), _) => -1 - (_, Edn::Str(_)) => 1 - (Edn::Quote(a), Edn::Quote(b)) => a.compare(b) - (Edn::Quote(_), _) => -1 - (_, Edn::Quote(_)) => 1 - (Edn::Tuple(a), Edn::Tuple(b)) => a.compare(b) - (Edn::Tuple(_), _) => -1 - (_, Edn::Tuple(_)) => 1 - (Edn::List(a), Edn::List(b)) => a.compare(b) - (Edn::List(_), _) => -1 - (_, Edn::List(_)) => 1 - (Edn::Buffer(a), Edn::Buffer(b)) => a.compare(b) - (Edn::Buffer(_), _) => -1 - (_, Edn::Buffer(_)) => 1 - (Edn::Set(a), Edn::Set(b)) => a.compare(b) - (Edn::Set(_), _) => -1 - (_, Edn::Set(_)) => 1 - (Edn::Map(a), Edn::Map(b)) => a.compare(b) - (Edn::Map(_), _) => -1 - (_, Edn::Map(_)) => 1 - (Edn::Atom(a), Edn::Atom(b)) => a.compare(b) - (Edn::Atom(_), _) => -1 - (_, Edn::Atom(_)) => 1 - (Edn::Record(a), Edn::Record(b)) => a.compare(b) - // (Edn::Record(_), _) => -1 - // (_, Edn::Record(_)) => 1 + (Nil, Nil) => 0 + (Nil, _) => -1 + (_, Nil) => 1 + (Bool(a), Bool(b)) => a.compare(b) + (Bool(_), _) => -1 + (_, Bool(_)) => 1 + (Number(a), Number(b)) => a.compare(b) + (Number(_), _) => -1 + (_, Number(_)) => 1 + (Symbol(a), Symbol(b)) => a.compare(b) + (Symbol(_), _) => -1 + (_, Symbol(_)) => 1 + (Tag(a), Tag(b)) => a.compare(b) + (Tag(_), _) => -1 + (_, Tag(_)) => 1 + (Str(a), Str(b)) => a.compare(b) + (Str(_), _) => -1 + (_, Str(_)) => 1 + (Quote(a), Quote(b)) => a.compare(b) + (Quote(_), _) => -1 + (_, Quote(_)) => 1 + (Tuple(a), Tuple(b)) => a.compare(b) + (Tuple(_), _) => -1 + (_, Tuple(_)) => 1 + (List(a), List(b)) => a.compare(b) + (List(_), _) => -1 + (_, List(_)) => 1 + (Buffer(a), Buffer(b)) => a.compare(b) + (Buffer(_), _) => -1 + (_, Buffer(_)) => 1 + (Set(a), Set(b)) => a.compare(b) + (Set(_), _) => -1 + (_, Set(_)) => 1 + (Map(a), Map(b)) => a.compare(b) + (Map(_), _) => -1 + (_, Map(_)) => 1 + (Atom(a), Atom(b)) => a.compare(b) + (Atom(_), _) => -1 + (_, Atom(_)) => 1 + (Record(a), Record(b)) => a.compare(b) + // (Record(_), _) => -1 + // (_, Record(_)) => 1 } ret } @@ -137,12 +137,12 @@ pub fn Edn::tuple(tag : Edn, extra : Array[Edn]) -> Edn { ///| pub fn Edn::is_literal(self : Edn) -> Bool { match self { - Edn::Nil => true - Edn::Bool(_) => true - Edn::Number(_) => true - Edn::Symbol(_) => true - Edn::Str(_) => true - Edn::Tag(_) => true + Nil => true + Bool(_) => true + Number(_) => true + Symbol(_) => true + Str(_) => true + Tag(_) => true _ => false } } diff --git a/src/lib/edn_test.mbt b/src/lib/edn_test.mbt index 7944c45..cf8f0f6 100644 --- a/src/lib/edn_test.mbt +++ b/src/lib/edn_test.mbt @@ -5,8 +5,8 @@ test "hello" { } test "atom inside edn" { - let edn = @lib.Edn::Atom(@lib.Edn::Number(1.0)) + let edn = @lib.Edn::Atom(Number(1.0)) assert_eq!(@lib.format!(edn, true).trim("\n"), "atom 1") let parsed = @lib.parse!("atom 1") - assert_eq!(parsed, @lib.Edn::Atom(@lib.Edn::Number(1.0))) + assert_eq!(parsed, @lib.Atom(Number(1.0))) } diff --git a/src/lib/lib.mbt b/src/lib/lib.mbt index 3f69f29..1c66bb2 100644 --- a/src/lib/lib.mbt +++ b/src/lib/lib.mbt @@ -18,9 +18,8 @@ pub fn parse(s : String) -> Edn!EdnCommonError { let xs = @cirru_parser.parse?(s).unwrap() if xs.length() == 1 { match xs[0] { - @cirru_parser.Cirru::Leaf(s) => - raise EdnCommonError("expected expr for data, got leaf: " + s) - @cirru_parser.Cirru::List(_) => extract_cirru_edn!(xs[0]) + Leaf(s) => raise EdnCommonError("expected expr for data, got leaf: " + s) + List(_) => extract_cirru_edn!(xs[0]) } } else { raise EdnCommonError("Expected 1 expr for edn, got length \{xs.length()}") @@ -30,20 +29,20 @@ pub fn parse(s : String) -> Edn!EdnCommonError { ///| fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { match node { - @cirru_parser.Cirru::Leaf(s) => + Leaf(s) => match s { - "nil" => Edn::Nil - "true" => Edn::Bool(true) - "false" => Edn::Bool(false) + "nil" => Nil + "true" => Bool(true) + "false" => Bool(false) "" => raise EdnCommonError("empty string is invalid for edn") s1 => match s1[0] { - '\'' => Edn::Symbol(s1.substring(start=1)) - ':' => Edn::tag(s1.substring(start=1)) - '"' | '|' => Edn::Str(s1.substring(start=1)) + '\'' => Symbol(s1.substring(start=1)) + ':' => tag(s1.substring(start=1)) + '"' | '|' => Str(s1.substring(start=1)) _ => match @strconv.parse_double?(s1.trim(" ")) { - Ok(f) => Edn::Number(f) + Ok(f) => Number(f) Err(e) => match e { @strconv.StrConvError(s) => @@ -52,17 +51,17 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { } } } - @cirru_parser.Cirru::List(xs) => + List(xs) => if xs.is_empty() { raise EdnCommonError("empty expr is invalid for edn") } else { // println("extracting list: " + xs.to_string()) match xs[0] { - @cirru_parser.Cirru::Leaf(s) => + Leaf(s) => match s { "quote" => if xs.length() == 2 { - Edn::Quote(xs[1]) + Quote(xs[1]) } else { raise EdnCommonError("missing edn quote value") } @@ -85,7 +84,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { } "atom" => if xs.length() == 2 { - Edn::Atom(extract_cirru_edn!(xs[1])) + Atom(extract_cirru_edn!(xs[1])) } else { raise EdnCommonError("missing edn atom value") } @@ -105,7 +104,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { } } match tag { - Some(x0) => Edn::Tuple({ tag: x0, extra }) + Some(x0) => Tuple({ tag: x0, extra }) None => raise EdnCommonError("missing edn :: fst value") } } @@ -120,7 +119,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { Err(v) => raise EdnCommonError(v.to_string()) } } - Edn::List(EdnListView(ys)) + List(EdnListView(ys)) } "#{}" => { let ys : @hashset.T[Edn] = @hashset.new() @@ -142,11 +141,11 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { continue } match x { - @cirru_parser.Cirru::Leaf(s) => + Leaf(s) => raise EdnCommonError( "expected a pair, invalid map entry: " + s, ) - @cirru_parser.Cirru::List(ys) => + List(ys) => if ys.length() == 2 { match (extract_cirru_edn?(ys[0]), extract_cirru_edn?(ys[1])) { @@ -168,9 +167,8 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { "%{}" => if xs.length() >= 3 { let name = match xs[1] { - @cirru_parser.Cirru::Leaf(s) => - EdnTag::new(s.trim_start(":")) - @cirru_parser.Cirru::List(e) => + Leaf(s) => EdnTag::new(s.trim_start(":")) + List(e) => raise EdnCommonError( "expected record name in string: " + e.to_string(), ) @@ -181,20 +179,20 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { continue } match x { - @cirru_parser.Cirru::Leaf(s) => + Leaf(s) => raise EdnCommonError( "expected record, invalid record entry: " + s, ) - @cirru_parser.Cirru::List(ys) => + List(ys) => if ys.length() == 2 { match (ys[0], extract_cirru_edn?(ys[1])) { - (@cirru_parser.Cirru::Leaf(s), Ok(v)) => + (Leaf(s), Ok(v)) => entries.push((EdnTag::new(s.trim_start(":")), v)) - (@cirru_parser.Cirru::Leaf(s), Err(e)) => + (Leaf(s), Err(e)) => raise EdnCommonError( "invalid record value for `\{s}`, got: \{e}", ) - (@cirru_parser.Cirru::List(zs), _) => + (List(zs), _) => raise EdnCommonError( "invalid list as record key: \{zs}", ) @@ -220,7 +218,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { continue } match x { - @cirru_parser.Cirru::Leaf(y) => + Leaf(y) => if y.length() == 2 { match @strconv.parse_int?(y) { Ok(b) => ys.push(b.reinterpret_as_uint()) @@ -247,7 +245,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { } a => raise EdnCommonError("invalid operator for edn: " + a) } - @cirru_parser.Cirru::List(a) => + List(a) => raise EdnCommonError("invalid nodes for edn: " + a.to_string()) } } @@ -257,31 +255,30 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError { ///| fn is_comment(node : @cirru_parser.Cirru) -> Bool { match node { - @cirru_parser.Cirru::Leaf(_) => false - @cirru_parser.Cirru::List(xs) => xs[0] == @cirru_parser.Cirru::Leaf(";") + Leaf(_) => false + List(xs) => xs[0] == @cirru_parser.Cirru::Leaf(";") } } ///| fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { match data { - Edn::Nil => @cirru_parser.Cirru::Leaf("nil") - Edn::Bool(v) => @cirru_parser.Cirru::Leaf(v.to_string()) - Edn::Number(n) => @cirru_parser.Cirru::Leaf(n.to_string()) - Edn::Symbol(s) => @cirru_parser.Cirru::Leaf("'\{s}") - Edn::Tag(s) => @cirru_parser.Cirru::Leaf(":" + s._) - Edn::Str(s) => @cirru_parser.Cirru::Leaf("|" + s) - Edn::Quote(v) => - @cirru_parser.Cirru::List([@cirru_parser.Cirru::Leaf("quote"), v]) - Edn::List(xs) => { + Nil => Leaf("nil") + Bool(v) => Leaf(v.to_string()) + Number(n) => Leaf(n.to_string()) + Symbol(s) => Leaf("'\{s}") + Tag(s) => Leaf(":" + s._) + Str(s) => Leaf("|" + s) + Quote(v) => List([Leaf("quote"), v]) + List(xs) => { let ys : Array[@cirru_parser.Cirru] = [] - ys.push(@cirru_parser.Cirru::Leaf("[]")) + ys.push(Leaf("[]")) for x in xs { ys.push(assemble_cirru_node!(x)) } - @cirru_parser.Cirru::List(ys) + List(ys) } - Edn::Set(xs) => { + Set(xs) => { let ys : Array[@cirru_parser.Cirru] = [] ys.push(@cirru_parser.Cirru::Leaf("#{}")) let items = xs._ @@ -291,7 +288,7 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { } @cirru_parser.Cirru::List(ys) } - Edn::Map(xs) => { + Map(xs) => { let ys : Array[@cirru_parser.Cirru] = [] ys.push(@cirru_parser.Cirru::Leaf("{}")) let items = Array::from_iter(xs._.iter()) @@ -310,19 +307,14 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { }) for pair in items.iter() { let (k, v) = pair - ys.push( - @cirru_parser.Cirru::List([ - assemble_cirru_node!(k), - assemble_cirru_node!(v), - ]), - ) + ys.push(List([assemble_cirru_node!(k), assemble_cirru_node!(v)])) } - @cirru_parser.Cirru::List(ys) + List(ys) } - Edn::Record({ tag: name, extra: entries }) => { + Record({ tag: name, extra: entries }) => { let ys : Array[@cirru_parser.Cirru] = [] - ys.push(@cirru_parser.Cirru::Leaf("%{}")) - ys.push(@cirru_parser.Cirru::Leaf(":" + name._)) + ys.push(Leaf("%{}")) + ys.push(Leaf(":" + name._)) let ordered_entries = entries ordered_entries.sort_by(fn(left, right) { let (_a1, a2) = left @@ -335,43 +327,34 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { }) for entry in ordered_entries { let v = entry.1 - ys.push( - @cirru_parser.Cirru::List([ - @cirru_parser.Cirru::Leaf(":" + entry.0._), - assemble_cirru_node!(v), - ]), - ) + ys.push(List([Leaf(":" + entry.0._), assemble_cirru_node!(v)])) } - @cirru_parser.Cirru::List(ys) + List(ys) } - Edn::Tuple({ tag, extra }) => { + Tuple({ tag, extra }) => { let ys : Array[@cirru_parser.Cirru] = [ - @cirru_parser.Cirru::Leaf("::"), + Leaf("::"), assemble_cirru_node!(tag), ] for item in extra { ys.push(assemble_cirru_node!(item)) } - @cirru_parser.Cirru::List(ys) + List(ys) } - Edn::Buffer(buf) => { + Buffer(buf) => { let ys : Array[@cirru_parser.Cirru] = [] - ys.push(@cirru_parser.Cirru::Leaf("buf")) + ys.push(Leaf("buf")) for b in buf._ { - ys.push( - @cirru_parser.Cirru::Leaf( - BigInt::from_int(b.reinterpret_as_int()).to_hex(), - ), - ) + ys.push(Leaf(BigInt::from_int(b.reinterpret_as_int()).to_hex())) } - @cirru_parser.Cirru::List(ys) + List(ys) } - // Edn::AnyRef(..) => unreachable!("AnyRef is not serializable"), - Edn::Atom(v) => { + // AnyRef(..) => unreachable!("AnyRef is not serializable"), + Atom(v) => { let ys : Array[@cirru_parser.Cirru] = [] - ys.push(@cirru_parser.Cirru::Leaf("atom")) + ys.push(Leaf("atom")) ys.push(assemble_cirru_node!(v)) - @cirru_parser.Cirru::List(ys) + List(ys) } } } @@ -380,18 +363,8 @@ fn assemble_cirru_node(data : Edn) -> @cirru_parser.Cirru!EdnCommonError { pub fn format(data : Edn, use_inline : Bool) -> String!EdnCommonError { let options : @cirru_parser.CirruWriterOptions = { use_inline, } let ret = match assemble_cirru_node!(data) { - @cirru_parser.Cirru::Leaf(s) => - @cirru_parser.format?( - [ - @cirru_parser.Cirru::List([ - @cirru_parser.Cirru::Leaf("do"), - @cirru_parser.Cirru::Leaf(s), - ]), - ], - options, - ) - @cirru_parser.Cirru::List(xs) => - @cirru_parser.format?([@cirru_parser.Cirru::List(xs)], options) + Leaf(s) => @cirru_parser.format?([List([Leaf("do"), Leaf(s)])], options) + List(xs) => @cirru_parser.format?([List(xs)], options) } match ret { Ok(s) => s