protobuf_parse/
protobuf_abs_path.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![doc(hidden)]

use std::fmt;
use std::mem;
use std::ops::Deref;

use protobuf::descriptor::FileDescriptorProto;
use protobuf::reflect::FileDescriptor;
use protobuf::reflect::MessageDescriptor;

use crate::protobuf_ident::ProtobufIdent;
use crate::protobuf_rel_path::ProtobufRelPath;
use crate::ProtobufIdentRef;
use crate::ProtobufRelPathRef;

/// Protobuf absolute name (e. g. `.foo.Bar`).
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
#[doc(hidden)]
pub struct ProtobufAbsPath {
    pub path: String,
}

#[doc(hidden)]
#[derive(Eq, PartialEq, Debug, Hash)]
#[repr(C)]
pub struct ProtobufAbsPathRef(str);

impl Default for ProtobufAbsPath {
    fn default() -> ProtobufAbsPath {
        ProtobufAbsPath::root()
    }
}

impl Deref for ProtobufAbsPathRef {
    type Target = str;

    fn deref(&self) -> &str {
        &self.0
    }
}

impl Deref for ProtobufAbsPath {
    type Target = ProtobufAbsPathRef;

    fn deref(&self) -> &ProtobufAbsPathRef {
        ProtobufAbsPathRef::new(&self.path)
    }
}

impl ProtobufAbsPathRef {
    pub fn is_root(&self) -> bool {
        self.0.is_empty()
    }

    pub fn root() -> &'static ProtobufAbsPathRef {
        Self::new("")
    }

    pub fn new(path: &str) -> &ProtobufAbsPathRef {
        assert!(ProtobufAbsPath::is_abs(path), "{:?} is not absolute", path);
        // SAFETY: repr(transparent)
        unsafe { mem::transmute(path) }
    }

    pub fn remove_prefix(&self, prefix: &ProtobufAbsPathRef) -> Option<&ProtobufRelPathRef> {
        if self.0.starts_with(&prefix.0) {
            let rem = &self.0[prefix.0.len()..];
            if rem.is_empty() {
                return Some(ProtobufRelPathRef::empty());
            }
            if rem.starts_with('.') {
                return Some(ProtobufRelPathRef::new(&rem[1..]));
            }
        }
        None
    }

    pub fn starts_with(&self, that: &ProtobufAbsPathRef) -> bool {
        self.remove_prefix(that).is_some()
    }

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

    pub fn to_owned(&self) -> ProtobufAbsPath {
        ProtobufAbsPath {
            path: self.0.to_owned(),
        }
    }

    pub fn parent(&self) -> Option<&ProtobufAbsPathRef> {
        match self.0.rfind('.') {
            Some(pos) => Some(ProtobufAbsPathRef::new(&self.0[..pos])),
            None => {
                if self.0.is_empty() {
                    None
                } else {
                    Some(ProtobufAbsPathRef::root())
                }
            }
        }
    }

    pub fn self_and_parents(&self) -> Vec<&ProtobufAbsPathRef> {
        let mut tmp = self;

        let mut r: Vec<&ProtobufAbsPathRef> = Vec::new();

        r.push(&self);

        while let Some(parent) = tmp.parent() {
            r.push(parent);
            tmp = parent;
        }

        r
    }
}

impl ProtobufAbsPath {
    pub fn root() -> ProtobufAbsPath {
        ProtobufAbsPathRef::root().to_owned()
    }

    pub fn as_ref(&self) -> &ProtobufAbsPathRef {
        ProtobufAbsPathRef::new(&self.path)
    }

    /// If given name is an fully quialified protobuf name.
    pub fn is_abs(path: &str) -> bool {
        path.is_empty() || (path.starts_with(".") && path != ".")
    }

    pub fn try_new(path: &str) -> Option<ProtobufAbsPath> {
        if ProtobufAbsPath::is_abs(path) {
            Some(ProtobufAbsPath::new(path))
        } else {
            None
        }
    }

    pub fn new<S: Into<String>>(path: S) -> ProtobufAbsPath {
        let path = path.into();
        assert!(
            ProtobufAbsPath::is_abs(&path),
            "path is not absolute: `{}`",
            path
        );
        assert!(!path.ends_with("."), "{}", path);
        ProtobufAbsPath { path }
    }

    pub fn new_from_rel(path: &str) -> ProtobufAbsPath {
        assert!(
            !path.starts_with("."),
            "rel path must not start with dot: {:?}",
            path
        );
        ProtobufAbsPath {
            path: if path.is_empty() {
                String::new()
            } else {
                format!(".{}", path)
            },
        }
    }

    pub fn package_from_file_proto(file: &FileDescriptorProto) -> ProtobufAbsPath {
        Self::new_from_rel(file.package())
    }

    pub fn package_from_file_descriptor(file: &FileDescriptor) -> ProtobufAbsPath {
        Self::package_from_file_proto(file.proto())
    }

    pub fn from_message(message: &MessageDescriptor) -> ProtobufAbsPath {
        Self::new_from_rel(&message.full_name())
    }

    pub fn concat(a: &ProtobufAbsPathRef, b: &ProtobufRelPathRef) -> ProtobufAbsPath {
        let mut a = a.to_owned();
        a.push_relative(b);
        a
    }

    pub fn from_path_without_dot(path: &str) -> ProtobufAbsPath {
        assert!(!path.is_empty());
        assert!(!path.starts_with("."));
        assert!(!path.ends_with("."));
        ProtobufAbsPath::new(format!(".{}", path))
    }

    pub fn from_path_maybe_dot(path: &str) -> ProtobufAbsPath {
        if path.starts_with(".") {
            ProtobufAbsPath::new(path.to_owned())
        } else {
            ProtobufAbsPath::from_path_without_dot(path)
        }
    }

    pub fn push_simple(&mut self, simple: &ProtobufIdentRef) {
        self.path.push('.');
        self.path.push_str(&simple);
    }

    pub fn push_relative(&mut self, relative: &ProtobufRelPathRef) {
        if !relative.is_empty() {
            self.path.push_str(&format!(".{}", relative));
        }
    }

    pub fn remove_suffix(&self, suffix: &ProtobufRelPathRef) -> Option<&ProtobufAbsPathRef> {
        if suffix.is_empty() {
            return Some(ProtobufAbsPathRef::new(&self.path));
        }

        if self.path.ends_with(suffix.as_str()) {
            let rem = &self.path[..self.path.len() - suffix.as_str().len()];
            if rem.is_empty() {
                return Some(ProtobufAbsPathRef::root());
            }
            if rem.ends_with('.') {
                return Some(ProtobufAbsPathRef::new(&rem[..rem.len() - 1]));
            }
        }
        None
    }

    /// Pop the last name component
    pub fn pop(&mut self) -> Option<ProtobufIdent> {
        match self.path.rfind('.') {
            Some(dot) => {
                let ident = ProtobufIdent::new(&self.path[dot + 1..]);
                self.path.truncate(dot);
                Some(ident)
            }
            None => None,
        }
    }

    pub fn to_root_rel(&self) -> ProtobufRelPath {
        if self == &Self::root() {
            ProtobufRelPath::empty()
        } else {
            ProtobufRelPath::new(&self.path[1..])
        }
    }

    pub fn ends_with(&self, that: &ProtobufRelPath) -> bool {
        self.remove_suffix(that).is_some()
    }
}

impl From<&'_ str> for ProtobufAbsPath {
    fn from(s: &str) -> Self {
        ProtobufAbsPath::new(s.to_owned())
    }
}

impl From<String> for ProtobufAbsPath {
    fn from(s: String) -> Self {
        ProtobufAbsPath::new(s)
    }
}

impl fmt::Display for ProtobufAbsPathRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

impl fmt::Display for ProtobufAbsPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", ProtobufAbsPathRef::new(&self.0))
    }
}

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

    #[test]
    fn absolute_path_push_simple() {
        let mut foo = ProtobufAbsPath::new(".foo".to_owned());
        foo.push_simple(ProtobufIdentRef::new("bar"));
        assert_eq!(ProtobufAbsPath::new(".foo.bar".to_owned()), foo);

        let mut foo = ProtobufAbsPath::root();
        foo.push_simple(ProtobufIdentRef::new("bar"));
        assert_eq!(ProtobufAbsPath::new(".bar".to_owned()), foo);
    }

    #[test]
    fn absolute_path_remove_prefix() {
        assert_eq!(
            Some(ProtobufRelPathRef::empty()),
            ProtobufAbsPath::new(".foo".to_owned())
                .remove_prefix(&ProtobufAbsPath::new(".foo".to_owned()))
        );
        assert_eq!(
            Some(ProtobufRelPathRef::new("bar")),
            ProtobufAbsPath::new(".foo.bar".to_owned())
                .remove_prefix(&ProtobufAbsPath::new(".foo".to_owned()))
        );
        assert_eq!(
            Some(ProtobufRelPathRef::new("baz.qux")),
            ProtobufAbsPath::new(".foo.bar.baz.qux".to_owned())
                .remove_prefix(&ProtobufAbsPath::new(".foo.bar".to_owned()))
        );
        assert_eq!(
            None,
            ProtobufAbsPath::new(".foo.barbaz".to_owned())
                .remove_prefix(ProtobufAbsPathRef::new(".foo.bar"))
        );
    }

    #[test]
    fn self_and_parents() {
        assert_eq!(
            vec![
                ProtobufAbsPathRef::new(".ab.cde.fghi"),
                ProtobufAbsPathRef::new(".ab.cde"),
                ProtobufAbsPathRef::new(".ab"),
                ProtobufAbsPathRef::root(),
            ],
            ProtobufAbsPath::new(".ab.cde.fghi".to_owned()).self_and_parents()
        );
    }

    #[test]
    fn ends_with() {
        assert!(ProtobufAbsPath::new(".foo.bar").ends_with(&ProtobufRelPath::new("")));
        assert!(ProtobufAbsPath::new(".foo.bar").ends_with(&ProtobufRelPath::new("bar")));
        assert!(ProtobufAbsPath::new(".foo.bar").ends_with(&ProtobufRelPath::new("foo.bar")));
        assert!(!ProtobufAbsPath::new(".foo.bar").ends_with(&ProtobufRelPath::new("foo.bar.baz")));
    }
}