summaryrefslogtreecommitdiff
path: root/mingling/src/asset/node.rs
blob: c8b760026c91a43268f2d4a58e3e72bc38dcf7ed (plain) (blame)
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
use just_fmt::kebab_case;

#[derive(Debug, Default)]
pub struct Node {
    node: Vec<String>,
}

impl Node {
    pub fn join(self, node: impl Into<String>) -> Node {
        let mut new_node = self.node;
        new_node.push(node.into());
        Node { node: new_node }
    }
}

impl From<&str> for Node {
    fn from(s: &str) -> Self {
        let node = s.split('.').map(|part| kebab_case!(part)).collect();
        Node { node }
    }
}

impl From<String> for Node {
    fn from(s: String) -> Self {
        let node = s.split('.').map(|part| kebab_case!(part)).collect();
        Node { node }
    }
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        self.node == other.node
    }
}

impl Eq for Node {}

impl PartialOrd for Node {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Node {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.node.cmp(&other.node)
    }
}

impl std::fmt::Display for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.node.join("."))
    }
}