Skip to content

Commit

Permalink
from trait impl so i can have easier context hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 17, 2024
1 parent 06e100d commit 36888ea
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bool_expr_parser_nom"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
12 changes: 12 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::str::FromStr;

use chrono::NaiveDate;

use crate::parse::parse_atom;

#[derive(Debug, Clone)]
pub enum Atom {
String(String),
Expand Down Expand Up @@ -63,6 +65,16 @@ impl fmt::Display for Atom {
}
}

impl<'a> From<&'a str> for Atom {
fn from(val: &'a str) -> Self {
let res = parse_atom(val);
if let Ok((i, out)) = res {
return out;
}
Atom::String(val.into())
}
}

#[derive(Debug, Clone, PartialEq)]
pub enum ComparisonOp {
Eq,
Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn parse_variable(i: &str) -> IResult<&str, Atom> {
map(parser, |v: &str| Atom::Variable(v.to_string()))(i)
}

fn parse_atom(i: &str) -> IResult<&str, Atom> {
pub fn parse_atom(i: &str) -> IResult<&str, Atom> {
alt((
parse_date,
parse_string,
Expand Down
12 changes: 11 additions & 1 deletion tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ use chrono::NaiveDate;
/// library tests for public functions to parse and then evaluate
#[test]
fn test_parsing() {}
fn test_parsing() {
let (i, expr) = parse("!(a=b and c=d) and z=3").unwrap();
assert_eq!(
true,
eval(
&expr,
&HashMap::from([("a", "d".into()), ("c", "b".into()), ("z", "3".into()),])
)
.unwrap()
);
}

#[test]
fn test_evaluation() {
Expand Down

0 comments on commit 36888ea

Please sign in to comment.