Skip to content

Commit

Permalink
getting rid of unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 23, 2024
1 parent 5315b98 commit dc66d97
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 56 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 = "flagfile"
version = "0.3.7"
version = "0.3.8"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
10 changes: 4 additions & 6 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::fmt;
use std::str::FromStr;

use chrono::NaiveDate;
use core::fmt;

use crate::parse::parse_atom;

Expand Down Expand Up @@ -50,7 +48,7 @@ impl PartialOrd for Atom {
},
Atom::Date(v) => match other {
Atom::Date(v2) => v.partial_cmp(v2),
/// TODO: if compare to number it might be unixtimestamp
// TODO: if compare to number it might be unixtimestamp
_ => None,
},
_ => None,
Expand All @@ -75,7 +73,7 @@ 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 {
if let Ok((_i, out)) = res {
return out;
}
Atom::String(val.into())
Expand Down Expand Up @@ -107,7 +105,7 @@ impl ComparisonOp {
}
impl fmt::Display for ComparisonOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self) {
match self {
ComparisonOp::Eq => write!(f, "=="),
ComparisonOp::More => write!(f, ">"),
ComparisonOp::Less => write!(f, "<"),
Expand Down
23 changes: 11 additions & 12 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ fn get_variable_value_from_context<'a>(
}

pub fn eval<'a>(expr: &AstNode, context: &Context) -> Result<bool, &'a str> {
let mut result = false;
result = match expr {
let result = match expr {
// true || false
AstNode::Constant(var) => {
let mut result = false;
if let Atom::Boolean(v) = var {
result = *v;
}
if let Atom::Variable(v) = var {
if let Atom::Variable(_v) = var {
let context_val = get_variable_value_from_context(expr, context);
if let Some(Atom::Boolean(inner)) = context_val {
result = inner;
Expand Down Expand Up @@ -126,7 +125,7 @@ mod tests {

#[test]
fn logic_test() {
let (i, expr) = parse("x=1 and y=2").unwrap();
let (_i, expr) = parse("x=1 and y=2").unwrap();
assert_eq!(
true,
eval(
Expand All @@ -136,7 +135,7 @@ mod tests {
.unwrap()
);

let (i, expr) = parse("x=1 || y=2").unwrap();
let (_i, expr) = parse("x=1 || y=2").unwrap();
assert_eq!(
true,
eval(
Expand All @@ -146,7 +145,7 @@ mod tests {
.unwrap()
);

let (i, expr) = parse("countryCode==LT && city='Palanga'").unwrap();
let (_i, expr) = parse("countryCode==LT && city='Palanga'").unwrap();
assert_eq!(
true,
eval(
Expand All @@ -162,7 +161,7 @@ mod tests {

#[test]
fn testing_function_calls() {
let (i, expr) = parse("lower(countryCode)==lt && upper(city)='PALANGA'").unwrap();
let (_i, expr) = parse("lower(countryCode)==lt && upper(city)='PALANGA'").unwrap();
assert_eq!(
true,
eval(
Expand All @@ -178,7 +177,7 @@ mod tests {

#[test]
fn simple_scope_test() {
let (i, expr) = parse("!(country=LT)").unwrap();
let (_i, expr) = parse("!(country=LT)").unwrap();
assert_eq!(
false,
eval(
Expand All @@ -189,7 +188,7 @@ mod tests {
);

// scope inside scope
let (i, expr) = parse("(not (country == Lithuania))").unwrap();
let (_i, expr) = parse("(not (country == Lithuania))").unwrap();
assert_eq!(
false,
eval(
Expand All @@ -199,7 +198,7 @@ mod tests {
.unwrap()
);

let (i, expr) = parse("((lower(country) == netherlands))").unwrap();
let (_i, expr) = parse("((lower(country) == netherlands))").unwrap();
assert_eq!(
true,
eval(
Expand All @@ -216,7 +215,7 @@ mod tests {
("x", Atom::Number(10)),
("y", Atom::String("tree".to_string())),
]);
let (i, expr) = parse("y in ('one', 'two', 'tree')").unwrap();
let (_i, expr) = parse("y in ('one', 'two', 'tree')").unwrap();
let res = eval(&expr, &context).unwrap();
assert_eq!(res, true);

Expand Down Expand Up @@ -317,7 +316,7 @@ mod tests {

#[test]
fn testing_date_comparison_evaluation() {
let (i, expr) = parse("created > 2024-02-02 and created <= 2024-02-13").unwrap();
let (_i, expr) = parse("created > 2024-02-02 and created <= 2024-02-13").unwrap();
assert_eq!(
true,
eval(&expr, &HashMap::from([("created", "2024-02-12".into())])).unwrap()
Expand Down
13 changes: 1 addition & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
use nom::{
branch::alt,
bytes::complete::{tag, tag_no_case, take_until},
character::complete::{alpha1, alphanumeric1, char, line_ending, multispace0, one_of, space0},
combinator::{eof, map, map_res, recognize},
complete::take,
error::VerboseError,
multi::{many0, many0_count, many_till},
sequence::{delimited, pair, preceded, separated_pair, tuple},
Err, IResult,
};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod ast;
Expand All @@ -18,7 +7,7 @@ pub mod parse_flagfile;

#[wasm_bindgen]
pub fn parse_wasm(i: &str) -> String {
let Ok((i, tree)) = parse::parse(i) else {
let Ok((_i, tree)) = parse::parse(i) else {
todo!()
};
let b = format!("{:?}", tree);
Expand Down
36 changes: 16 additions & 20 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use chrono::NaiveDate;
use nom::{
branch::alt,
bytes::complete::{tag, tag_no_case, take_until, take_while_m_n},
character::{
complete::{alpha1, alphanumeric1, char, digit1, multispace0},
is_digit,
},
combinator::{cut, map, map_res, opt, recognize},
bytes::complete::{tag, tag_no_case, take_until},
character::complete::{alpha1, alphanumeric1, char, digit1, multispace0},
combinator::{cut, map, opt, recognize},
error::ParseError,
multi::{many0, many0_count, many_m_n, separated_list0},
number::complete::double,
sequence::{delimited, pair, preceded, tuple},
multi::{many0, many0_count, separated_list0},
sequence::{delimited, pair, tuple},
IResult,
};

Expand Down Expand Up @@ -181,7 +177,7 @@ fn parse_compare_or_array_expr(i: &str) -> IResult<&str, AstNode> {
}

fn parse_logic_expr(i: &str) -> IResult<&str, AstNode> {
/// a=b AND b not in (1,2,3)
// a=b AND b not in (1,2,3)
let parser = tuple((
alt((parse_compare_or_array_expr, parse_parenthesized_expr)),
ws(parse_logic_op),
Expand Down Expand Up @@ -313,7 +309,7 @@ mod tests {

#[test]
fn test_comparison_op() {
let (i, v) = parse_comparison_op("<>").unwrap();
let (_i, v) = parse_comparison_op("<>").unwrap();
assert_eq!(v, ComparisonOp::NotEq);

let (i, v) = parse_comparison_op("==").unwrap();
Expand All @@ -323,33 +319,33 @@ mod tests {

#[test]
fn test_logic_op() {
let (i, v) = parse_logic_op("&& this").unwrap();
let (_i, v) = parse_logic_op("&& this").unwrap();
assert_eq!(v, LogicOp::And);
}

#[test]
fn test_compare_expr() {
let (i, v) = parse_compare_expr("_demo >= 10").unwrap();
let (i, _v) = parse_compare_expr("_demo >= 10").unwrap();
assert_eq!(i, "");
}

#[test]
fn test_logic_expr() {
let (i, v) =
let (i, _v) =
parse_logic_expr("_demo >= 10 && demo == \"something more than that\"").unwrap();
assert_eq!(i, "");
}

#[test]
fn test_parse_list() {
let (i, v) = parse_list("(1,2, 34, \"demo\", -10, -3.14)").unwrap();
let (i, _v) = parse_list("(1,2, 34, \"demo\", -10, -3.14)").unwrap();
assert_eq!(i, "");
}

#[test]
fn test_logic_expresion_with_list() {
let e = "a = 2 and b in (1,2.2, \"demo\")";
let (i, v) = parse_logic_expr(e).unwrap();
let (i, _v) = parse_logic_expr(e).unwrap();
assert_eq!(i, "");
}

Expand All @@ -359,13 +355,13 @@ mod tests {
parse_logic_expr("a=3 && c = 3 || d not in (2,4,5)").is_ok(),
true
);
let (i, v) = parse_expr("a=3 && c = 3 || d not in (2,4,5) and this<>34.43").unwrap();
let (i, _v) = parse_expr("a=3 && c = 3 || d not in (2,4,5) and this<>34.43").unwrap();
assert_eq!(i, "");
}

#[test]
fn test_list_bug() {
/// this should not be allowed as array should have either in () or not in ()
// this should not be allowed as array should have either in () or not in ()
let a = "a == 2 and b >= (1,2,3)";
let res = parse_logic_expr(a);
assert_eq!(res.is_err(), true);
Expand All @@ -390,7 +386,7 @@ mod tests {
let a = "a='demo demo'";
let res = parse_compare_expr(a);
assert_eq!(res.is_ok(), true);
if let Ok((i, v)) = res {
if let Ok((i, _v)) = res {
assert_eq!(i, "");
}
}
Expand All @@ -414,7 +410,7 @@ mod tests {
g in (4,5,6) and z == "demo car" or
model in (ms,mx,m3,my) and !(created >= 2024-01-01
and demo == false) and ((a=2) and not (c=3))"###;
let (i, v) = parse(expression).unwrap();
let (i, _v) = parse(expression).unwrap();
assert_eq!(i, "");
}
}
4 changes: 2 additions & 2 deletions src/parse_flagfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use nom::{
branch::alt,
bytes::complete::{is_not, tag, take_until},
character::complete::{alpha1, alphanumeric1, char},
combinator::{map, opt, recognize, value},
character::complete::alphanumeric1,
combinator::{map, recognize, value},
multi::{many0, many0_count, many1},
sequence::{delimited, pair, preceded, terminated, tuple},
IResult,
Expand Down
3 changes: 1 addition & 2 deletions tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use flagfile::ast::Atom;
use flagfile::eval::Context;
use flagfile::{self, eval::eval, parse::parse};
use chrono::NaiveDate;

#[test]
fn test_hashmap_into() {
Expand All @@ -14,7 +13,7 @@ fn test_hashmap_into() {

#[test]
fn test_parsing() {
let (i, expr) = parse("!(a=b and c=d) and z=3").unwrap();
let (_i, expr) = parse("!(a=b and c=d) and z=3").unwrap();
assert_eq!(
true,
eval(
Expand Down

0 comments on commit dc66d97

Please sign in to comment.