File_system/Fundamentals/Path/
Components.rs

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
use std::str::Split;

use super::{Path_type, Separator};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Component_type<'a> {
    Root,
    Current,
    Parent,
    Normal(&'a str),
}

impl<'a> From<&'a str> for Component_type<'a> {
    fn from(item: &'a str) -> Self {
        match item {
            "" => Component_type::Root,
            "/" => Component_type::Root,
            "." => Component_type::Current,
            ".." => Component_type::Parent,
            _ => Component_type::Normal(item),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Components_type<'a>(Split<'a, char>);

impl<'a> Components_type<'a> {
    pub fn New(Path: &Path_type) -> Components_type {
        Components_type(Path.As_str().split(Separator))
    }

    pub fn Get_common_components(self, Other: Components_type<'a>) -> usize {
        self.zip(Other).take_while(|(a, b)| a == b).count()
    }
}

impl<'a> Iterator for Components_type<'a> {
    type Item = Component_type<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(Component_type::from)
    }
}

impl DoubleEndedIterator for Components_type<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(Component_type::from)
    }
}

#[cfg(test)]
mod Tests {

    use super::*;

    #[test]
    fn Test_components() {
        assert_eq!(
            Components_type::New(Path_type::From_str("/a/b/c")).collect::<Vec<_>>(),
            vec![
                Component_type::Root,
                Component_type::Normal("a"),
                Component_type::Normal("b"),
                Component_type::Normal("c")
            ]
        );

        assert_eq!(
            Components_type::New(Path_type::From_str("/a/./b/c")).collect::<Vec<_>>(),
            vec![
                Component_type::Root,
                Component_type::Normal("a"),
                Component_type::Current,
                Component_type::Normal("b"),
                Component_type::Normal("c")
            ]
        );

        assert_eq!(
            Components_type::New(Path_type::From_str("a/b/c")).collect::<Vec<_>>(),
            vec![
                Component_type::Normal("a"),
                Component_type::Normal("b"),
                Component_type::Normal("c")
            ]
        );

        assert_eq!(
            Components_type::New(Path_type::From_str("a/./../b/c")).collect::<Vec<_>>(),
            vec![
                Component_type::Normal("a"),
                Component_type::Current,
                Component_type::Parent,
                Component_type::Normal("b"),
                Component_type::Normal("c")
            ]
        );
    }
}