summaryrefslogtreecommitdiff
path: root/server/src/path.rs
blob: 17aa263f6aa6bde6341f54dd6d668504781213aa (plain)
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
// Invariants:
// - 'orig' is a valid path, and in particular, the '/'-split components are non-empty;
// - 'comps' describes a non-empty prefix of 'orig'.
#[derive(Debug)]
pub struct Path<'a> {
    orig: &'a str,
    comps: Vec<(usize, &'a str)>,  // (offset, component)
}

impl<'a> Path<'a> {
    /// Returns the split path if the path is valid. A valid path has the following requirements:
    /// * It does not contain unicode control characters;
    /// * When split on '/', the resulting components are all non-empty and neither start nor end
    ///   with unicode whitespace characters.
    pub fn split(s: &str) -> Option<Path> {
        let mut comps = Vec::new();
        let mut start = 0;  // of current component
        for (i, c) in s.char_indices() {
            match c {
                '/' if i == start => return None,  // empty component
                '/' => {
                    let comp = &s[start..i];
                    if comp.starts_with(|c2: char| c2.is_whitespace()) ||
                            comp.ends_with(|c2: char| c2.is_whitespace()) {
                        return None;
                    }
                    comps.push((start, comp));
                    start = i + 1;
                },
                _ if c.is_control() => return None,
                _ => {},  // include in current component
            }
        }

        // check and add the last component
        if start == s.len() {
            return None;  // slash at end of input
        }
        comps.push((start, &s[start..s.len()]));

        Some(Path { orig: s, comps })
    }

    pub fn without_last(&self) -> Option<(Path, &str)> {
        let n = self.comps.len();
        if n > 1 {
            Some((Path { orig: self.orig, comps: self.comps[0 .. n - 1].into() }, self.comps[n - 1].1))
        } else {
            None
        }
    }

    pub fn join(&self) -> &str {
        if self.comps.len() == 0 {
            ""
        } else {
            let (lastoff, lastcomp) = self.comps[self.comps.len() - 1];
            &self.orig[0 .. lastoff + lastcomp.len()]
        }
    }
}