Skip to content

Commit

Permalink
upgrade cirru_parser ; clippy updates; bump 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Sep 19, 2021
1 parent b0ca474 commit 2f043c7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 82 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.5"
version = "0.1.6"
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.1"
cirru_parser = "0.1.2"
lazy_static = "1.4.0"
regex = "1.4.5"
45 changes: 21 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ use std::collections::HashSet;

/// parse Cirru code into data
pub fn parse(s: &str) -> Result<Edn, String> {
match cirru_parser::parse(&s) {
Ok(nodes) => match nodes {
Cirru::Leaf(s) => Err(format!("expected expr at top level, got leaf: {}", s)),
Cirru::List(xs) => {
if xs.len() == 1 {
match &xs[0] {
Cirru::Leaf(s) => Err(format!("expected expr for data, got leaf: {}", s)),
Cirru::List(_) => extract_cirru_edn(&xs[0]),
}
} else {
Err(format!("Expected 1 expr for edn, got length {}: {:?} ", xs.len(), xs))
match cirru_parser::parse(s) {
Ok(xs) => {
if xs.len() == 1 {
match &xs[0] {
Cirru::Leaf(s) => Err(format!("expected expr for data, got leaf: {}", s)),
Cirru::List(_) => extract_cirru_edn(&xs[0]),
}
} else {
Err(format!("Expected 1 expr for edn, got length {}: {:?} ", xs.len(), xs))
}
},
}
Err(e) => Err(e),
}
}
Expand All @@ -37,9 +34,9 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
"false" => Ok(Edn::Bool(false)),
"" => Err(String::from("empty string is invalid for edn")),
s1 => match s1.chars().next().unwrap() {
'\'' => Ok(Edn::Symbol(String::from(&s1[1..]))),
':' => Ok(Edn::Keyword(String::from(&s1[1..]))),
'"' | '|' => Ok(Edn::Str(String::from(&s1[1..]))),
'\'' => Ok(Edn::Symbol(s1[1..].to_owned())),
':' => Ok(Edn::Keyword(s1[1..].to_owned())),
'"' | '|' => Ok(Edn::Str(s1[1..].to_owned())),
_ => {
if matches_float(s1) {
let f: f64 = s1.parse().unwrap();
Expand All @@ -58,7 +55,7 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
Cirru::Leaf(s) => match s.as_str() {
"quote" => {
if xs.len() == 2 {
Ok(Edn::Quote(xs[1].clone()))
Ok(Edn::Quote(xs[1].to_owned()))
} else {
Err(String::from("missing edn quote value"))
}
Expand Down Expand Up @@ -120,7 +117,7 @@ fn extract_cirru_edn(node: &Cirru) -> Result<Edn, String> {
}
"%{}" => {
if xs.len() >= 3 {
let name = match xs[1].clone() {
let name = match xs[1].to_owned() {
Cirru::Leaf(s) => s,
Cirru::List(e) => return Err(format!("expected record name in string: {:?}", e)),
};
Expand All @@ -134,7 +131,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)) => {
fields.push(s.clone());
fields.push(s.to_owned());
values.push(v);
}
(Cirru::Leaf(s), Err(e)) => {
Expand Down Expand Up @@ -184,20 +181,20 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
}
Edn::Symbol(s) => {
let mut leaf = String::from("'");
leaf.push_str(&s);
leaf.push_str(s);
Cirru::Leaf(leaf)
}
Edn::Keyword(s) => {
let mut leaf = String::from(":");
leaf.push_str(&s);
leaf.push_str(s);
Cirru::Leaf(leaf)
}
Edn::Str(s) => {
let mut leaf = String::from("|");
leaf.push_str(&s);
leaf.push_str(s);
Cirru::Leaf(leaf)
}
Edn::Quote(v) => Cirru::List(vec![Cirru::Leaf(String::from("quote")), (*v).clone()]),
Edn::Quote(v) => Cirru::List(vec![Cirru::Leaf(String::from("quote")), (*v).to_owned()]),
Edn::List(xs) => {
let mut ys: Vec<Cirru> = vec![Cirru::Leaf(String::from("[]"))];
for x in xs {
Expand All @@ -224,7 +221,7 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
for idx in 0..fields.len() {
let v = &values[idx];
ys.push(Cirru::List(vec![
Cirru::Leaf(fields[idx].clone()),
Cirru::Leaf(fields[idx].to_owned()),
assemble_cirru_node(v),
]));
}
Expand All @@ -237,7 +234,7 @@ fn assemble_cirru_node(data: &Edn) -> Cirru {
/// generate string fro, Edn
pub fn format(data: &Edn, use_inline: bool) -> Result<String, String> {
let options = CirruWriterOptions { use_inline };
match assemble_cirru_node(&data) {
match assemble_cirru_node(data) {
Cirru::Leaf(s) => cirru_parser::format(
&Cirru::List(vec![
(Cirru::List(vec![Cirru::Leaf(String::from("do")), Cirru::Leaf(s)])),
Expand Down
24 changes: 11 additions & 13 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,13 @@ impl Ord for Edn {
(Self::Map(_), _) => Less,
(_, Self::Map(_)) => Greater,

(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,
},
(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 All @@ -223,7 +221,7 @@ impl PartialEq for Edn {
match (self, other) {
(Self::Nil, Self::Nil) => true,
(Self::Bool(a), Self::Bool(b)) => a == b,
(Self::Number(a), Self::Number(b)) => a == b,
(Self::Number(a), Self::Number(b)) => (a - b).abs() < f64::EPSILON,
(Self::Symbol(a), Self::Symbol(b)) => a == b,
(Self::Keyword(a), Self::Keyword(b)) => a == b,
(Self::Str(a), Self::Str(b)) => a == b,
Expand Down Expand Up @@ -350,10 +348,10 @@ impl Edn {
let key: String = k.to_owned();
match self {
Edn::Map(xs) => {
if xs.contains_key(&Edn::Keyword(key.clone())) {
Ok(xs[&Edn::Keyword(key)].clone())
} else if xs.contains_key(&Edn::Str(key.clone())) {
Ok(xs[&Edn::Str(key)].clone())
if xs.contains_key(&Edn::Keyword(key.to_owned())) {
Ok(xs[&Edn::Keyword(key)].to_owned())
} else if xs.contains_key(&Edn::Str(key.to_owned())) {
Ok(xs[&Edn::Str(key)].to_owned())
} else {
Ok(Edn::Nil)
}
Expand Down
56 changes: 13 additions & 43 deletions tests/edn_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ fn edn_parsing() {
assert_eq!(Ok(Edn::Bool(true)), cirru_edn::parse("do true"));
assert_eq!(Ok(Edn::Bool(false)), cirru_edn::parse("do false"));

assert_eq!(
Ok(Edn::Symbol(String::from("a"))),
cirru_edn::parse("do 'a")
);
assert_eq!(
Ok(Edn::Keyword(String::from("k"))),
cirru_edn::parse("do :k")
);
assert_eq!(Ok(Edn::Symbol(String::from("a"))), cirru_edn::parse("do 'a"));
assert_eq!(Ok(Edn::Keyword(String::from("k"))), cirru_edn::parse("do :k"));
assert_eq!(Ok(Edn::Str(String::from("s"))), cirru_edn::parse("do |s"));

assert_eq!(
Expand Down Expand Up @@ -63,22 +57,10 @@ fn edn_formatting() -> Result<(), String> {
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)?,
"\ndo 'a\n"
);
assert_eq!(
cirru_edn::format(&Edn::Keyword(String::from("a")), true)?,
"\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"
);
assert_eq!(cirru_edn::format(&Edn::Symbol(String::from("a")), true)?, "\ndo 'a\n");
assert_eq!(cirru_edn::format(&Edn::Keyword(String::from("a")), true)?, "\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");

Ok(())
}
Expand Down Expand Up @@ -162,10 +144,7 @@ fn demo_parsing() -> Result<(), String> {

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)?),
Ok(v1.clone())
);
assert_eq!(cirru_edn::parse(&cirru_edn::format(&v1, true)?), Ok(v1.clone()));
assert_eq!(v1, v2);

Ok(())
Expand Down Expand Up @@ -205,29 +184,20 @@ fn debug_format() {

#[test]
fn test_reader() -> Result<(), String> {
assert_eq!(Edn::Bool(true).read_bool()?, true);
assert_eq!(
Edn::Str(String::from("a")).read_string()?,
String::from("a")
);
assert_eq!(
Edn::Symbol(String::from("a")).read_symbol_string()?,
String::from("a")
);
assert!(Edn::Bool(true).read_bool()?);
assert_eq!(Edn::Str(String::from("a")).read_string()?, String::from("a"));
assert_eq!(Edn::Symbol(String::from("a")).read_symbol_string()?, String::from("a"));
assert_eq!(
Edn::Keyword(String::from("a")).read_keyword_string()?,
String::from("a")
);
assert_eq!(Edn::Number(1.1).read_number()?, 1.1);
assert_eq!(
Edn::List(vec![Edn::Number(1.0)]).vec_get(0)?,
Edn::Number(1.0)
);
assert!((Edn::Number(1.1).read_number()? - 1.1).abs() < f64::EPSILON);
assert_eq!(Edn::List(vec![Edn::Number(1.0)]).vec_get(0)?, Edn::Number(1.0));
assert_eq!(Edn::List(vec![Edn::Number(1.0)]).vec_get(1)?, Edn::Nil);

let mut dict = HashMap::new();
dict.insert(Edn::Keyword(String::from("k")), Edn::Number(1.1));
assert_eq!(Edn::Map(dict.to_owned()).map_get("k")?.read_number()?, 1.1);
assert!((Edn::Map(dict.to_owned()).map_get("k")?.read_number()? - 1.1).abs() < f64::EPSILON);
assert_eq!(Edn::Map(dict).map_get("k2")?, Edn::Nil);
Ok(())
}

0 comments on commit 2f043c7

Please sign in to comment.