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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#![doc(hidden)]

use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::ops::Deref;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;

#[derive(Debug, thiserror::Error)]
enum Error {
    #[error("path is empty")]
    Empty,
    #[error("backslashes in path: {0:?}")]
    Backslashes(String),
    #[error("path contains empty components: {0:?}")]
    EmptyComponent(String),
    #[error("dot in path: {0:?}")]
    Dot(String),
    #[error("dot-dot in path: {0:?}")]
    DotDot(String),
    #[error("path is absolute: `{}`", _0.display())]
    Absolute(PathBuf),
    #[error("non-UTF-8 component in path: `{}`", _0.display())]
    NotUtf8(PathBuf),
}

/// Protobuf file relative normalized file path.
#[repr(transparent)]
#[derive(Eq, PartialEq, Hash, Debug)]
pub struct ProtoPath {
    path: str,
}

/// Protobuf file relative normalized file path.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ProtoPathBuf {
    path: String,
}

impl Hash for ProtoPathBuf {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.as_path().hash(state);
    }
}

impl Borrow<ProtoPath> for ProtoPathBuf {
    fn borrow(&self) -> &ProtoPath {
        self.as_path()
    }
}

impl Deref for ProtoPathBuf {
    type Target = ProtoPath;

    fn deref(&self) -> &ProtoPath {
        self.as_path()
    }
}

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

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

impl PartialEq<str> for ProtoPath {
    fn eq(&self, other: &str) -> bool {
        &self.path == other
    }
}

impl PartialEq<str> for ProtoPathBuf {
    fn eq(&self, other: &str) -> bool {
        &self.path == other
    }
}

impl ProtoPath {
    fn unchecked_new(path: &str) -> &ProtoPath {
        unsafe { &*(path as *const str as *const ProtoPath) }
    }

    pub fn new(path: &str) -> anyhow::Result<&ProtoPath> {
        if path.is_empty() {
            return Err(Error::Empty.into());
        }
        if path.contains('\\') {
            return Err(Error::Backslashes(path.to_owned()).into());
        }
        for component in path.split('/') {
            if component.is_empty() {
                return Err(Error::EmptyComponent(path.to_owned()).into());
            }
            if component == "." {
                return Err(Error::Dot(path.to_owned()).into());
            }
            if component == ".." {
                return Err(Error::DotDot(path.to_owned()).into());
            }
        }
        Ok(Self::unchecked_new(path))
    }

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

    pub fn to_path(&self) -> &Path {
        Path::new(&self.path)
    }

    pub fn to_proto_path_buf(&self) -> ProtoPathBuf {
        ProtoPathBuf {
            path: self.path.to_owned(),
        }
    }
}

impl ProtoPathBuf {
    pub fn as_path(&self) -> &ProtoPath {
        ProtoPath::unchecked_new(&self.path)
    }

    pub fn new(path: String) -> anyhow::Result<ProtoPathBuf> {
        ProtoPath::new(&path)?;
        Ok(ProtoPathBuf { path })
    }

    pub fn from_path(path: &Path) -> anyhow::Result<ProtoPathBuf> {
        let mut path_str = String::new();
        for component in path.components() {
            match component {
                Component::Prefix(..) => return Err(Error::Absolute(path.to_owned()).into()),
                Component::RootDir => return Err(Error::Absolute(path.to_owned()).into()),
                Component::CurDir if path_str.is_empty() => {}
                Component::CurDir => return Err(Error::Dot(path.display().to_string()).into()),
                Component::ParentDir => {
                    return Err(Error::DotDot(path.display().to_string()).into())
                }
                Component::Normal(c) => {
                    if !path_str.is_empty() {
                        path_str.push('/');
                    }
                    path_str.push_str(c.to_str().ok_or_else(|| Error::NotUtf8(path.to_owned()))?);
                }
            }
        }
        Ok(ProtoPathBuf { path: path_str })
    }
}