mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 23:19:07 -05:00
wip: parse operator
This commit is contained in:
parent
6fea2d2ddf
commit
9833c91470
|
@ -63,6 +63,22 @@ pub enum Operator {
|
||||||
Equals,
|
Equals,
|
||||||
NotEquals,
|
NotEquals,
|
||||||
}
|
}
|
||||||
|
impl FromStr for Operator {
|
||||||
|
type Err = Error;
|
||||||
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
|
if value == "<" {
|
||||||
|
Ok(Operator::LessThan)
|
||||||
|
} else if value == ">" {
|
||||||
|
Ok(Operator::GreaterThan)
|
||||||
|
} else if value == "=" {
|
||||||
|
Ok(Operator::Equals)
|
||||||
|
} else if value == "!" {
|
||||||
|
Ok(Operator::NotEquals)
|
||||||
|
} else {
|
||||||
|
Err(Error::DelegationParseError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[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.
|
/// Values are the data associated with a restriction. For now, these can only be a single numbers.
|
||||||
|
@ -96,18 +112,17 @@ pub struct Condition {
|
||||||
fn str_to_condition(cs: &str) -> Option<Condition> {
|
fn str_to_condition(cs: &str) -> Option<Condition> {
|
||||||
// a condition is a string (alphanum+underscore), an operator (<>=!), and values (num+comma)
|
// a condition is a string (alphanum+underscore), an operator (<>=!), and values (num+comma)
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RE: Regex = Regex::new("([[:word:]])([<>=!]+)([,[[:digit:]]]+)").unwrap();
|
static ref RE: Regex = Regex::new("([[:word:]]+)([<>=!]+)([,[[:digit:]]]*)").unwrap();
|
||||||
}
|
}
|
||||||
// match against the regex
|
// match against the regex
|
||||||
let caps = RE.captures(cs)?;
|
let caps = RE.captures(cs)?;
|
||||||
let field = caps.get(1)?.as_str().parse::<Field>().ok()?;
|
let field = caps.get(1)?.as_str().parse::<Field>().ok()?;
|
||||||
let _op = caps.get(2)?.as_str();
|
let operator = caps.get(2)?.as_str().parse::<Operator>().ok()?;
|
||||||
let _vals = caps.get(3)?.as_str();
|
let _vals = caps.get(3)?.as_str();
|
||||||
// convert field string into Field
|
// convert field string into Field
|
||||||
|
|
||||||
Some(Condition {
|
Some(Condition {
|
||||||
field,
|
field,
|
||||||
operator: Operator::GreaterThan,
|
operator,
|
||||||
values: vec![],
|
values: vec![],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -162,19 +177,19 @@ mod tests {
|
||||||
assert!(field.is_err());
|
assert!(field.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse fields
|
// parse a full conditional query with an empty array
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_kind_field() -> Result<()> {
|
fn parse_kind_equals_empty() -> Result<()> {
|
||||||
// given an empty condition query, produce an empty vector
|
// given an empty condition query, produce an empty vector
|
||||||
let _kind_cq = ConditionQuery {
|
let kind_cq = ConditionQuery {
|
||||||
conditions: vec![Condition {
|
conditions: vec![Condition {
|
||||||
field: Field::Kind,
|
field: Field::Kind,
|
||||||
operator: Operator::GreaterThan,
|
operator: Operator::Equals,
|
||||||
values: vec![],
|
values: vec![],
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
//let parsed = "kind=1".parse::<ConditionQuery>()?;
|
let parsed = "kind=".parse::<ConditionQuery>()?;
|
||||||
//assert_eq!(parsed, kind_cq);
|
assert_eq!(parsed, kind_cq);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user