atc_router/
context.rs

1use crate::ast::Value;
2use crate::schema::Schema;
3use fnv::FnvHashMap;
4use uuid::Uuid;
5
6pub struct Match {
7    pub uuid: Uuid,
8    pub matches: FnvHashMap<String, Value>,
9    pub captures: FnvHashMap<String, String>,
10}
11
12impl Match {
13    pub fn new() -> Self {
14        Match {
15            uuid: Uuid::default(),
16            matches: FnvHashMap::default(),
17            captures: FnvHashMap::default(),
18        }
19    }
20}
21
22impl Default for Match {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28pub struct Context<'a> {
29    schema: &'a Schema,
30    values: FnvHashMap<String, Vec<Value>>,
31    pub result: Option<Match>,
32}
33
34impl<'a> Context<'a> {
35    pub fn new(schema: &'a Schema) -> Self {
36        Context {
37            schema,
38            values: FnvHashMap::with_hasher(Default::default()),
39            result: None,
40        }
41    }
42
43    pub fn add_value(&mut self, field: &str, value: Value) {
44        if &value.my_type() != self.schema.type_of(field).unwrap() {
45            panic!("value provided does not match schema");
46        }
47
48        self.values
49            .entry(field.to_string())
50            .or_default()
51            .push(value);
52    }
53
54    pub fn value_of(&self, field: &str) -> Option<&[Value]> {
55        self.values.get(field).map(|v| v.as_slice())
56    }
57
58    pub fn reset(&mut self) {
59        self.values.clear();
60        self.result = None;
61    }
62}