1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
pub type QueryType = (String, String);
pub struct Query {
    pub vec: Vec<QueryType>
}


impl Query {
    pub fn empty() -> Query {
        Query { vec: vec!() }
    }

    pub fn from(vec: Vec<(&str, &str)>) -> Query {
        let mut new = Vec::with_capacity(vec.len());
        for q in vec {
            new.push((q.0.to_string(), q.1.to_string()))
        }
        Query { vec: new }
    }

    pub fn get(self, by: &str) -> Option<String> {
        self.vec.iter().find_map(|pair| {
            if pair.clone().0 == by {
                Some(pair.clone().1)
            } else { None }
        })
    }

    pub fn get_all(&self, p0: &str) -> Vec<QueryType> {
        self.vec.iter().filter(|q|{
            q.clone().0 == p0
        }).map(|t| (t.clone().0, t.clone().1))
            .collect::<Vec<QueryType>>()
    }

    pub fn add(&self, pair: (&str, &str)) -> Query {
        let mut new = vec!();
        for q in &self.vec {
            new.push(q.clone())
        }
        new.push((pair.0.to_string(), pair.1.to_string()));
        Query { vec: new }
    }

    pub fn replace(&self, pair: (&str, &str)) -> Query {
        let mut new = vec!();
        let mut seen = false;
        for q in &self.vec {
            if q.0 == pair.0 && seen == false {
                new.push((pair.0.to_string(), pair.1.to_string()));
                seen = true
            }
            if q.0 != pair.0 {
                new.push(q.clone())
            }
        }
        if seen == false {
            new.push((pair.0.to_string(), pair.1.to_string()));
        }
        Query { vec: new }
    }
}