Skip to content

Commit

Permalink
change format for record, use keywords; tag 0.2.0-a1
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 6, 2021
1 parent ef8eee8 commit 856df40
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_edn"
version = "0.1.12"
version = "0.2.0-a1"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
"%{}" => {
if xs.len() >= 3 {
let name = match xs[1].to_owned() {
Cirru::Leaf(s) => s,
Cirru::Leaf(s) => s.strip_prefix(':').unwrap_or(&s).to_owned(),
Cirru::List(e) => return Err(format!("expected record name in string: {:?}", e)),
};
let mut entries: Vec<(String, Edn)> = vec![];
Expand All @@ -136,7 +136,7 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
if ys.len() == 2 {
match (&ys[0], extract_cirru_edn(&ys[1])) {
(Cirru::Leaf(s), Ok(v)) => {
entries.push((s.to_owned(), v));
entries.push((s.strip_prefix(':').unwrap_or(s).to_owned(), v));
}
(Cirru::Leaf(s), Err(e)) => {
return Err(format!("invalid record value for `{}`, got: {}", s, e))
Expand Down Expand Up @@ -213,11 +213,11 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
Cirru::List(ys)
}
Edn::Record(name, entries) => {
let mut ys: Vec<Cirru> = vec![Cirru::Leaf(String::from("%{}")), Cirru::Leaf(String::from(name))];
for idx in 0..entries.len() {
let v = &entries[idx].1;
let mut ys: Vec<Cirru> = vec![Cirru::Leaf(String::from("%{}")), Cirru::Leaf(format!(":{}", name))];
for entry in entries {
let v = &entry.1;
ys.push(Cirru::List(vec![
Cirru::Leaf(entries[idx].0.to_owned()),
Cirru::Leaf(format!(":{}", entry.0)),
assemble_cirru_node(v),
]));
}
Expand Down
6 changes: 3 additions & 3 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ impl fmt::Display for Edn {
f.write_str(")")
}
Self::Record(name, entries) => {
f.write_str(&format!("(%{{}} {}", name))?;
f.write_str(&format!("(%{{}} {}", Edn::Keyword(name.to_owned())))?;

for idx in 0..entries.len() {
f.write_str(&format!("({} {})", entries[idx].0, entries[idx].1))?;
for entry in entries {
f.write_str(&format!("({} {})", Edn::Keyword(entry.0.to_owned()), entry.1))?;
}

f.write_str(")")
Expand Down
24 changes: 21 additions & 3 deletions tests/edn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ fn set_writing() -> Result<(), String> {
}

const RECORD_DEMO: &str = r#"
%{} Demo (a 1.0)
b 2.0
c $ [] 1.0 2.0 3.0
%{} :Demo (:a 1)
:b 2
:c $ [] 1 2 3
"#;

const DICT_DEMO: &str = r#"
Expand Down Expand Up @@ -165,6 +165,24 @@ fn demo_parsing() -> Result<(), String> {
assert_eq!(cirru_edn::parse(&cirru_edn::format(&v1, true)?), Ok(v1.clone()));
assert_eq!(v1, v2);

assert_eq!(
cirru_edn::format(
&Edn::Record(
String::from("Demo"),
vec![
(String::from("a"), Edn::Number(1.0),),
(String::from("b"), Edn::Number(2.0)),
(
String::from("c"),
Edn::List(vec![Edn::Number(1.0), Edn::Number(2.0), Edn::Number(3.0)])
)
],
),
false
),
Ok(String::from(RECORD_DEMO))
);

Ok(())
}

Expand Down

0 comments on commit 856df40

Please sign in to comment.