Skip to content

Commit

Permalink
upgrade parser; refactor format; bump 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Apr 30, 2021
1 parent 8355c7d commit 35f5fcd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_edn"
version = "0.1.1"
version = "0.1.2"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,6 +13,6 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cirru_parser = "0.1.0"
cirru_parser = "0.1.1"
lazy_static = "1.4.0"
regex = "1.4.5"
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, /* use_inline */ true); // String.
cirru_edn::format(data, /* use_inline */ true); // Result<String, 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 @@ -160,7 +160,7 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
}

lazy_static! {
static ref RE_FLOAT: Regex = Regex::new("^-?[\\d]+(\\.[\\d]+)?$").unwrap(); // TODO special cases not handled
static ref RE_FLOAT: Regex = Regex::new("^-?[\\d]+(\\.[\\d]+)?$").unwrap(); // TODO special cases not handled
}

fn matches_float(x: &str) -> bool {
Expand Down Expand Up @@ -239,7 +239,7 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
}

/// generate string fro, Edn
pub fn format(data: &Edn, use_inline: bool) -> String {
pub fn format(data: &Edn, use_inline: bool) -> Result<String, String> {
let options = CirruWriterOptions { use_inline };
match assemble_cirru_node(&data) {
Cirru::Leaf(s) => cirru_parser::format(
Expand Down
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#[macro_use]
extern crate lazy_static;

use cirru_edn;
use std::fs;
use std::io::Error;

Expand All @@ -11,7 +7,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, true));
println!("{}", cirru_edn::format(&d, true).unwrap());

Ok(())
}
19 changes: 13 additions & 6 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,29 @@ impl Ord for Edn {
(_, Self::List(_)) => Greater,

(Self::Set(a), Self::Set(b)) => match a.len().cmp(&b.len()) {
Equal => {
unreachable!("TODO sets are not cmp ed") // TODO
}
Equal => unreachable!("TODO sets are not cmp ed"), // TODO
a => a,
},
(Self::Set(_), _) => Less,
(_, Self::Set(_)) => Greater,

(Self::Map(a), Self::Map(b)) => {
unreachable!(format!("TODO maps are not cmp ed {:?} {:?}", a, b)) // TODO
match a.len().cmp(&b.len()) {
Equal => unreachable!(format!("TODO maps are not cmp ed {:?} {:?}", a, b)), // TODO
a => a,
}
}
(Self::Map(_), _) => Less,
(_, Self::Map(_)) => Greater,

(Self::Record(_name1, _fields1, _values1), Self::Record(_name2, _fields2, _values2)) => {
unreachable!("TODO records are not cmp ed") // TODO
(Self::Record(name1, fields1, values1), Self::Record(name2, fields2, values2)) => {
match name1.cmp(name2) {
Equal => match fields1.cmp(&fields2) {
Equal => values1.cmp(&values2),
a => a,
},
a => a,
}
}
}
}
Expand Down
41 changes: 24 additions & 17 deletions tests/edn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,37 @@ fn set_parsing() {
}

#[test]
fn edn_formatting() {
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");
fn edn_formatting() -> Result<(), String> {
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), 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::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")), true),
cirru_edn::format(&Edn::Symbol(String::from("a")), true)?,
"\ndo 'a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Keyword(String::from("a")), true),
cirru_edn::format(&Edn::Keyword(String::from("a")), true)?,
"\ndo :a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Str(String::from("a")), true),
cirru_edn::format(&Edn::Str(String::from("a")), true)?,
"\ndo |a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Str(String::from("a")), true),
cirru_edn::format(&Edn::Str(String::from("a")), true)?,
"\ndo |a\n"
);

Ok(())
}

#[test]
fn list_writing() {
fn list_writing() -> Result<(), String> {
assert_eq!(
cirru_edn::format(
&Edn::List(vec![
Expand All @@ -91,23 +93,26 @@ fn list_writing() {
Edn::List(vec![Edn::Number(3.0)])
]),
true
),
)?,
"\n[] 1 2 $ [] 3\n"
);

Ok(())
}

#[test]
fn set_writing() {
fn set_writing() -> Result<(), String> {
let mut v = HashSet::new();
v.insert(Edn::Number(1.0));
v.insert(Edn::List(vec![Edn::Number(3.0)]));

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

assert!(r == r1 || r == r2);
Ok(())
}

const RECORD_DEMO: &str = r#"
Expand Down Expand Up @@ -138,7 +143,7 @@ const DICT_DEMO2: &str = r#"
"#;

#[test]
fn demo_parsing() {
fn demo_parsing() -> Result<(), String> {
// println!("{:?}", cirru_edn::parse(RECORD_DEMO));
// println!("{:?}", cirru_edn::parse(DICT_DEMO));

Expand All @@ -158,10 +163,12 @@ 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, true)),
cirru_edn::parse(&cirru_edn::format(&v1, true)?),
Ok(v1.clone())
);
assert_eq!(v1, v2);

Ok(())
}

#[test]
Expand Down

0 comments on commit 35f5fcd

Please sign in to comment.