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
use std::rc::Rc;

#[derive(Debug, Clone, PartialEq)]
pub enum Data {
    Int(i64),
    Float(f64),
    Bytes(Rc<Vec<u8>>),
    String(Rc<String>),
    Seq(Rc<Vec<Data>>),
}

impl Data {
    /// Data from a sequence
    pub fn fseq(vec: Vec<Data>) -> Data {
        Data::Seq(vec.into())
    }

    /// Data from bytes
    pub fn fbytes(bytes: Vec<u8>) -> Data {
        Data::Bytes(bytes.into())
    }

    pub fn i64(&self) -> Option<i64> {
        if let Data::Int(i) = self {
            Some(*i)
        } else {
            None
        }
    }
    pub fn f64(&self) -> Option<f64> {
        if let Data::Float(f) = self {
            Some(*f)
        } else {
            None
        }
    }
    pub fn bytes(&self) -> Option<&Vec<u8>> {
        if let Data::Bytes(bytes) = self {
            Some(bytes)
        } else {
            None
        }
    }
    pub fn str(&self) -> Option<&str> {
        if let Data::String(s) = self {
            Some(s)
        } else {
            None
        }
    }
    pub fn string(&self) -> Option<&Rc<String>> {
        if let Data::String(s) = self {
            Some(s)
        } else {
            None
        }
    }
    pub fn seq(&self) -> Option<&Rc<Vec<Data>>> {
        if let Data::Seq(s) = self {
            Some(s)
        } else {
            None
        }
    }
    pub fn u8(&self) -> Option<u8> {
        self.i64().map(|i| i as u64 as u8)
    }
    pub fn u16(&self) -> Option<u16> {
        self.i64().map(|i| i as u64 as u16)
    }
    pub fn u32(&self) -> Option<u32> {
        self.i64().map(|i| i as u64 as u32)
    }
    pub fn u64(&self) -> Option<u64> {
        self.i64().map(|i| i as u64 as u64)
    }
    pub fn i8(&self) -> Option<i8> {
        self.i64().map(|i| i as i8)
    }
    pub fn i16(&self) -> Option<i16> {
        self.i64().map(|i| i as i16)
    }
    pub fn i32(&self) -> Option<i32> {
        self.i64().map(|i| i as i32)
    }
    pub fn f32(&self) -> Option<f32> {
        self.f64().map(|i| i as f32)
    }
}

impl From<i64> for Data {
    fn from(x: i64) -> Data {
        Data::Int(x)
    }
}

impl From<f64> for Data {
    fn from(x: f64) -> Data {
        Data::Float(x)
    }
}

impl From<Rc<Vec<u8>>> for Data {
    fn from(x: Rc<Vec<u8>>) -> Data {
        Data::Bytes(x)
    }
}

impl From<&Rc<Vec<u8>>> for Data {
    fn from(x: &Rc<Vec<u8>>) -> Data {
        Data::Bytes(x.clone())
    }
}

impl From<&[u8]> for Data {
    fn from(x: &[u8]) -> Data {
        Data::Bytes(x.to_vec().into())
    }
}

impl From<Rc<String>> for Data {
    fn from(x: Rc<String>) -> Data {
        Data::String(x)
    }
}

impl From<&Rc<String>> for Data {
    fn from(x: &Rc<String>) -> Data {
        Data::String(x.clone())
    }
}

impl From<String> for Data {
    fn from(x: String) -> Data {
        Data::String(x.into())
    }
}

impl From<&str> for Data {
    fn from(x: &str) -> Data {
        Data::String(x.to_owned().into())
    }
}

impl From<Rc<Vec<Data>>> for Data {
    fn from(x: Rc<Vec<Data>>) -> Data {
        Data::Seq(x)
    }
}

impl From<&Rc<Vec<Data>>> for Data {
    fn from(x: &Rc<Vec<Data>>) -> Data {
        Data::Seq(x.clone())
    }
}

impl From<Vec<Data>> for Data {
    fn from(x: Vec<Data>) -> Data {
        Data::Seq(x.into())
    }
}

impl From<&[Data]> for Data {
    fn from(x: &[Data]) -> Data {
        Data::Seq(x.to_vec().into())
    }
}