From 487fe5260a89806b84704117ce246289d6239bd2 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sat, 15 Oct 2022 15:46:07 -0500 Subject: [PATCH] wip: parsing of values --- src/delegation.rs | 49 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/delegation.rs b/src/delegation.rs index 3920001..4e12112 100644 --- a/src/delegation.rs +++ b/src/delegation.rs @@ -80,11 +80,11 @@ impl FromStr for Operator { } } -#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] +//#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] /// Values are the data associated with a restriction. For now, these can only be a single numbers. -pub enum Value { - Number(u64), -} +//pub enum Value { +// Number(u64), +//} #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] pub struct ConditionQuery { @@ -106,7 +106,7 @@ pub struct Delegation { pub struct Condition { pub(crate) field: Field, pub(crate) operator: Operator, - pub(crate) values: Vec, + pub(crate) values: Vec, } fn str_to_condition(cs: &str) -> Option { @@ -118,12 +118,17 @@ fn str_to_condition(cs: &str) -> Option { let caps = RE.captures(cs)?; let field = caps.get(1)?.as_str().parse::().ok()?; let operator = caps.get(2)?.as_str().parse::().ok()?; - let _vals = caps.get(3)?.as_str(); + // values are just comma separated numbers, but all must be parsed + let rawvals = caps.get(3)?.as_str(); + let values = rawvals + .split_terminator(',') + .map(|n| n.parse::().ok()) + .collect::>>()?; // convert field string into Field Some(Condition { field, operator, - values: vec![], + values, }) } @@ -192,4 +197,34 @@ mod tests { assert_eq!(parsed, kind_cq); Ok(()) } + // parse a full conditional query with a single value + #[test] + fn parse_kind_equals_singleval() -> Result<()> { + // given an empty condition query, produce an empty vector + let kind_cq = ConditionQuery { + conditions: vec![Condition { + field: Field::Kind, + operator: Operator::Equals, + values: vec![1], + }], + }; + let parsed = "kind=1".parse::()?; + assert_eq!(parsed, kind_cq); + Ok(()) + } + // parse a full conditional query with multiple values + #[test] + fn parse_kind_equals_multival() -> Result<()> { + // given an empty condition query, produce an empty vector + let kind_cq = ConditionQuery { + conditions: vec![Condition { + field: Field::Kind, + operator: Operator::Equals, + values: vec![1, 2, 4], + }], + }; + let parsed = "kind=1,2,4".parse::()?; + assert_eq!(parsed, kind_cq); + Ok(()) + } }