Skip to content

Commit

Permalink
Merge pull request #5 from Cirru/use-inline
Browse files Browse the repository at this point in the history
expose use_inline option
  • Loading branch information
TCXX authored Apr 25, 2021
2 parents 9d19d8d + 33f4488 commit 8355c7d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 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.0"
version = "0.1.1"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cirru_edn;

cirru_edn::parse("[] 1 2 true"); // Result<Edn, String>

cirru_edn::format(data); // String
cirru_edn::format(data, /* use_inline */ true); // String.
```

### License
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
}

/// generate string fro, Edn
pub fn format(data: &Edn) -> String {
let options = CirruWriterOptions { use_inline: true };
pub fn format(data: &Edn, use_inline: bool) -> String {
let options = CirruWriterOptions { use_inline };
match assemble_cirru_node(&data) {
Cirru::Leaf(s) => cirru_parser::format(
&Cirru::List(vec![
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> Result<(), Error> {
let content = fs::read_to_string(large_file_path)?;
let d = cirru_edn::parse(&content).unwrap();

println!("{}", cirru_edn::format(&d));
println!("{}", cirru_edn::format(&d, true));

Ok(())
}
46 changes: 29 additions & 17 deletions tests/edn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,43 @@ fn set_parsing() {

#[test]
fn edn_formatting() {
assert_eq!(cirru_edn::format(&Edn::Nil), "\ndo nil\n");
assert_eq!(cirru_edn::format(&Edn::Bool(true)), "\ndo true\n");
assert_eq!(cirru_edn::format(&Edn::Bool(false)), "\ndo false\n");
assert_eq!(cirru_edn::format(&Edn::Nil, true), "\ndo nil\n");
assert_eq!(cirru_edn::format(&Edn::Bool(true), true), "\ndo true\n");
assert_eq!(cirru_edn::format(&Edn::Bool(false), true), "\ndo false\n");

assert_eq!(cirru_edn::format(&Edn::Number(1.0)), "\ndo 1\n");
assert_eq!(cirru_edn::format(&Edn::Number(1.1)), "\ndo 1.1\n");
assert_eq!(cirru_edn::format(&Edn::Number(-1.1)), "\ndo -1.1\n");
assert_eq!(cirru_edn::format(&Edn::Number(1.0), true), "\ndo 1\n");
assert_eq!(cirru_edn::format(&Edn::Number(1.1), true), "\ndo 1.1\n");
assert_eq!(cirru_edn::format(&Edn::Number(-1.1), true), "\ndo -1.1\n");

assert_eq!(
cirru_edn::format(&Edn::Symbol(String::from("a"))),
cirru_edn::format(&Edn::Symbol(String::from("a")), true),
"\ndo 'a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Keyword(String::from("a"))),
cirru_edn::format(&Edn::Keyword(String::from("a")), true),
"\ndo :a\n"
);
assert_eq!(cirru_edn::format(&Edn::Str(String::from("a"))), "\ndo |a\n");
assert_eq!(cirru_edn::format(&Edn::Str(String::from("a"))), "\ndo |a\n");
assert_eq!(
cirru_edn::format(&Edn::Str(String::from("a")), true),
"\ndo |a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Str(String::from("a")), true),
"\ndo |a\n"
);
}

#[test]
fn list_writing() {
assert_eq!(
cirru_edn::format(&Edn::List(vec![
Edn::Number(1.0),
Edn::Number(2.0),
Edn::List(vec![Edn::Number(3.0)])
])),
cirru_edn::format(
&Edn::List(vec![
Edn::Number(1.0),
Edn::Number(2.0),
Edn::List(vec![Edn::Number(3.0)])
]),
true
),
"\n[] 1 2 $ [] 3\n"
);
}
Expand All @@ -94,7 +103,7 @@ fn set_writing() {
v.insert(Edn::List(vec![Edn::Number(3.0)]));

// TODO order is not stable
let r = cirru_edn::format(&Edn::Set(v));
let r = cirru_edn::format(&Edn::Set(v), true);
let r1 = "\n#{} ([] 3) 1\n";
let r2 = "\n#{} 1 $ [] 3\n";

Expand Down Expand Up @@ -148,7 +157,10 @@ fn demo_parsing() {

let v1 = cirru_edn::parse(DICT_DEMO).unwrap();
let v2 = cirru_edn::parse(DICT_DEMO2).unwrap();
assert_eq!(cirru_edn::parse(&cirru_edn::format(&v1)), Ok(v1.clone()));
assert_eq!(
cirru_edn::parse(&cirru_edn::format(&v1, true)),
Ok(v1.clone())
);
assert_eq!(v1, v2);
}

Expand Down

0 comments on commit 8355c7d

Please sign in to comment.