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
use std::{borrow, fmt, hash, ops};

use bytestring::ByteString;

mod symbol;
mod variant;

pub use self::symbol::{StaticSymbol, Symbol};
pub use self::variant::{Variant, VariantMap, VecStringMap, VecSymbolMap};

#[derive(Debug, PartialEq, Eq, Clone, Hash, Display)]
pub enum Descriptor {
    Ulong(u64),
    Symbol(Symbol),
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, From)]
pub struct Multiple<T>(pub Vec<T>);

impl<T> Multiple<T> {
    pub fn len(&self) -> usize {
        self.0.len()
    }

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

    pub fn iter(&self) -> ::std::slice::Iter<T> {
        self.0.iter()
    }
}

impl<T> Default for Multiple<T> {
    fn default() -> Multiple<T> {
        Multiple(Vec::new())
    }
}

impl<T> ops::Deref for Multiple<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> ops::DerefMut for Multiple<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct List(pub Vec<Variant>);

impl List {
    pub fn len(&self) -> usize {
        self.0.len()
    }

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

    pub fn iter(&self) -> ::std::slice::Iter<Variant> {
        self.0.iter()
    }
}

#[derive(Display, Clone, Eq, Ord, PartialOrd)]
pub enum Str {
    String(String),
    ByteStr(ByteString),
    Static(&'static str),
}

impl Str {
    pub fn from_str(s: &str) -> Str {
        Str::ByteStr(ByteString::from(s))
    }

    pub fn as_bytes(&self) -> &[u8] {
        match self {
            Str::String(s) => s.as_ref(),
            Str::ByteStr(s) => s.as_ref(),
            Str::Static(s) => s.as_bytes(),
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Str::String(s) => s.as_str(),
            Str::ByteStr(s) => s.as_ref(),
            Str::Static(s) => s,
        }
    }

    pub fn to_bytes_str(&self) -> ByteString {
        match self {
            Str::String(s) => ByteString::from(s.as_str()),
            Str::ByteStr(s) => s.clone(),
            Str::Static(s) => ByteString::from_static(s),
        }
    }

    pub fn len(&self) -> usize {
        match self {
            Str::String(s) => s.len(),
            Str::ByteStr(s) => s.len(),
            Str::Static(s) => s.len(),
        }
    }
}

impl From<&'static str> for Str {
    fn from(s: &'static str) -> Str {
        Str::Static(s)
    }
}

impl From<ByteString> for Str {
    fn from(s: ByteString) -> Str {
        Str::ByteStr(s)
    }
}

impl From<String> for Str {
    fn from(s: String) -> Str {
        Str::String(s)
    }
}

impl hash::Hash for Str {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        match self {
            Str::String(s) => (&*s).hash(state),
            Str::ByteStr(s) => (&*s).hash(state),
            Str::Static(s) => s.hash(state),
        }
    }
}

impl borrow::Borrow<str> for Str {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl PartialEq<Str> for Str {
    fn eq(&self, other: &Str) -> bool {
        match self {
            Str::String(s) => match other {
                Str::String(o) => s == o,
                Str::ByteStr(o) => o == s.as_str(),
                Str::Static(o) => s == *o,
            },
            Str::ByteStr(s) => match other {
                Str::String(o) => s == o.as_str(),
                Str::ByteStr(o) => s == o,
                Str::Static(o) => s == *o,
            },
            Str::Static(s) => match other {
                Str::String(o) => o == s,
                Str::ByteStr(o) => o == *s,
                Str::Static(o) => s == o,
            },
        }
    }
}

impl PartialEq<str> for Str {
    fn eq(&self, other: &str) -> bool {
        match self {
            Str::String(ref s) => s == other,
            Str::ByteStr(ref s) => {
                // workaround for possible compiler bug
                let t: &str = &*s;
                t == other
            }
            Str::Static(s) => *s == other,
        }
    }
}

impl fmt::Debug for Str {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Str::String(s) => write!(f, "ST:\"{}\"", s),
            Str::ByteStr(s) => write!(f, "B:\"{}\"", &*s),
            Str::Static(s) => write!(f, "S:\"{}\"", s),
        }
    }
}