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
//! Path specifications

use std::vec;

/// A part of a step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RpPathPart {
    Variable(String),
    Segment(String),
}

/// A step in a path specification.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RpPathStep {
    pub parts: Vec<RpPathPart>,
}

/// A path specification.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RpPathSpec {
    pub steps: Vec<RpPathStep>,
}

#[derive(Debug)]
pub struct Vars<'a> {
    iter: vec::IntoIter<&'a str>,
}

impl<'a> Iterator for Vars<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl RpPathSpec {
    /// List all variables in the path spec.
    pub fn vars(&self) -> Vars {
        let mut vars = Vec::new();

        for step in &self.steps {
            for part in &step.parts {
                if let RpPathPart::Variable(ref var) = *part {
                    vars.push(var.as_str());
                }
            }
        }

        Vars {
            iter: vars.into_iter(),
        }
    }
}