json_template/path/
mod.rs

1//! Path module.
2
3/// The placeholder Path.
4pub struct Path<'a> {
5    path: &'a str
6}
7
8impl<'a> Path<'a> {
9    /// Create a new Path.
10    pub fn new(path: &'a str) -> Self {
11        Self { path }
12    }
13
14    /// Get the value of the Path.
15    pub fn str(&self) -> &str {
16        self.path
17    }
18
19    /// Get the path segments of the placeholder.
20    /// Examples:
21    /// "{time:5}".segments() == ["time:5"].
22    /// "{time:5}.time".segments() == ["time:5", "time"].
23    /// "{file:file.json}.data" == ["file:file.json", "data"]. 
24    pub fn segments(&self) -> Vec<&str> {
25        let mut level = 0;
26        let mut current_segment_start = 0;
27        let mut current_segment_end = 0;
28        let mut segments = Vec::new();
29        for character in self.str().chars() {
30            match character {
31                '{' => level += 1,
32                '}' => level -= 1,
33                '.' => if level == 0 {
34                        segments.push(&self.str()[current_segment_start .. current_segment_end]);
35                        current_segment_start = current_segment_end + 1;
36                },
37                _ => {}
38            }
39            current_segment_end += 1;
40        }
41        segments.push(&self.str()[current_segment_start .. current_segment_end]);
42        segments
43    }
44}