protobuf_parse/
protobuf_ident.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
#![doc(hidden)]

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

/// Identifier in `.proto` file
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
#[doc(hidden)]
pub struct ProtobufIdent(String);

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

impl Deref for ProtobufIdentRef {
    type Target = str;

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

impl Deref for ProtobufIdent {
    type Target = ProtobufIdentRef;

    fn deref(&self) -> &ProtobufIdentRef {
        ProtobufIdentRef::new(&self.0)
    }
}

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

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

impl Into<String> for ProtobufIdent {
    fn into(self) -> String {
        self.0
    }
}

impl fmt::Display for ProtobufIdent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.get(), f)
    }
}

impl ProtobufIdentRef {
    pub fn new<'a>(ident: &'a str) -> &'a ProtobufIdentRef {
        assert!(!ident.is_empty());
        // SAFETY: ProtobufIdentRef is repr(transparent)
        unsafe { mem::transmute(ident) }
    }

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

    pub fn to_owned(&self) -> ProtobufIdent {
        ProtobufIdent(self.0.to_owned())
    }
}

impl ProtobufIdent {
    pub fn as_ref(&self) -> &ProtobufIdentRef {
        ProtobufIdentRef::new(&self.0)
    }

    pub fn new(s: &str) -> ProtobufIdent {
        assert!(!s.is_empty());
        assert!(!s.contains("/"));
        assert!(!s.contains("."));
        assert!(!s.contains(":"));
        assert!(!s.contains("("));
        assert!(!s.contains(")"));
        ProtobufIdent(s.to_owned())
    }

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

    pub fn into_string(self) -> String {
        self.0
    }
}