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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use lazy_static::lazy_static;
use regex::Regex;

use crate::collections::Metadata;
use crate::errors::{Define, Error, Result};

lazy_static! {
    static ref RE_PART: Regex = Regex::new("^[a-z]+[a-z0-9_]*[a-z0-9]+$").unwrap();
}

pub enum PathError {
    Invalid,
}

impl Define for PathError {
    fn define(&self) -> &str {
        match self {
            PathError::Invalid => "path.invalid",
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Path {
    parts: Vec<String>,
    separator: String,
    wildcards: Vec<String>,
}

fn is_part_valid(part: &str, separator: &str, wildcards: &[String]) -> bool {
    if part == separator {
        return true;
    }

    if wildcards.iter().any(|wildcard| part == wildcard) {
        return true;
    }

    RE_PART.is_match(part)
}

impl Path {
    pub fn new<P, S, W>(path: P, separator: S, wildcards: Vec<W>) -> Result<Path>
    where
        P: Into<String>,
        S: Into<String>,
        W: Into<String>,
    {
        let path = path.into();
        let separator = separator.into();
        let wildcards: Vec<String> = wildcards.into_iter().map(Into::into).collect();

        let parts: Vec<String> = path
            .split(&separator)
            .into_iter()
            .map(|part| part.to_lowercase())
            .collect();

        if parts.is_empty() {
            return Err(Error::new(PathError::Invalid, "full path is empty", None));
        }

        Ok(Path {
            parts: parts
                .into_iter()
                .map(|part| {
                    if !is_part_valid(&part, &separator, &wildcards) {
                        return Err(Error::new(
                            PathError::Invalid,
                            "path part has invalid characters",
                            Metadata::with("part", part),
                        ));
                    }

                    Ok(part)
                })
                .collect::<Result<Vec<String>>>()?,
            separator,
            wildcards,
        })
    }

    pub fn parts(&self) -> &[String] {
        &self.parts
    }

    pub fn separator(&self) -> &str {
        &self.separator
    }

    pub fn wildcards(&self) -> &[String] {
        &self.wildcards
    }
}

impl ToString for Path {
    fn to_string(&self) -> String {
        self.parts.join(&self.separator)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_path() {
        // Simple
        let path = Path::new::<_, _, &str>("my.path", ".", Vec::new()).unwrap();
        assert_eq!(path.parts(), &["my", "path"]);
        assert_eq!(path.separator(), ".");
        assert_eq!(path.wildcards(), &[] as &[String; 0]);
        assert_eq!(path.to_string(), "my.path");

        // With wildcards
        let path = Path::new("my.path", ".", vec!["*", ">"]).unwrap();
        assert_eq!(path.parts(), &["my", "path"]);
        assert_eq!(path.separator(), ".");
        assert_eq!(path.wildcards(), &["*", ">"]);
        assert_eq!(path.to_string(), "my.path");

        // Single partt
        let path = Path::new("my_path", "#", vec!["*", ">"]).unwrap();
        assert_eq!(path.parts(), &["my_path"]);
        assert_eq!(path.separator(), "#");
        assert_eq!(path.wildcards(), &["*", ">"]);
        assert_eq!(path.to_string(), "my_path");

        // Invalid character
        assert!(Path::new::<_, _, &str>("my.p@th", ".", Vec::new()).is_err())
    }

    #[test]
    fn equals() {
        let path1 = Path::new::<_, _, &str>("one.two.three", ".", Vec::new()).unwrap();
        let path2 = Path::new::<_, _, &str>("one.three.two", ".", Vec::new()).unwrap();
        let path3 = Path::new::<_, _, &str>("two.one.three", ".", Vec::new()).unwrap();

        assert!(path1 == path1);
        assert!(path1 != path2);
        assert!(path2 != path3);
    }
}