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
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::string;

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum Ast {
    Null(Null),
    Boolean(Boolean),
    Integer(Integer),
    Float(Float),
    String(String),
    Array(Array),
    Object(Object),
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Null {
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Boolean {
    pub value: bool,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Integer {
    pub value: i64,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Float {
    pub value: f64,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct String {
    pub value: string::String,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Array {
    pub elements: Vec<Ast>,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Object {
    pub properties: Vec<Property>,
    pub annotations: Vec<Annotation>,
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Property {
    pub key: string::String,
    pub position: Position,
    pub value: Ast,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct Annotation {
    pub name: string::String,
    pub position: Position,
    pub value: Value,
}

#[derive(Clone, Copy, PartialEq, Debug, Eq, Deserialize, Serialize)]
pub struct Position {
    pub index: usize,
    pub line: usize,
    pub col: usize,
}
impl Default for Position {
    fn default() -> Self {
        Self {
            index: 0,
            line: 1,
            col: 1,
        }
    }
}

impl Position {
    pub fn new(index: usize, line: usize, col: usize) -> Self {
        Position { index, line, col }
    }
}

macro_rules! define_is (
    ($name:ident, $yt:ident) => (
pub fn $name(&self) -> bool {
    match self {
        Ast::$yt($yt { .. }) => true,
        _ => false
    }
}
    );
);

macro_rules! define_as_ref (
    ($name:ident, $t:ty, $yt:ident) => (
pub fn $name(&self) -> Option<$t> {
    match self {
        Ast::$yt(ref v) => Some(v),
        _ => None
    }
}
    );
);

impl Ast {
    define_is!(is_null, Null);
    define_is!(is_boolean, Boolean);
    define_is!(is_integer, Integer);
    define_is!(is_float, Float);
    define_is!(is_string, String);
    define_is!(is_array, Array);
    define_is!(is_object, Object);

    define_as_ref!(as_boolean, &Boolean, Boolean);
    define_as_ref!(as_integer, &Integer, Integer);
    define_as_ref!(as_float, &Float, Float);
    define_as_ref!(as_string, &String, String);
    define_as_ref!(as_array, &Array, Array);
    define_as_ref!(as_object, &Object, Object);

    pub fn key(&self, key: &str) -> Option<&Self> {
        match self {
            Ast::Object(Object {
                properties: value, ..
            }) => value.iter().find(|p| p.key == key).map(|v| &v.value),
            Ast::Array(Array {
                elements: value, ..
            }) => {
                if let Ok(idx) = key.parse::<usize>() {
                    value.get(idx)
                } else {
                    None
                }
            }
            _ => None,
        }
    }
    pub fn retrive(&self, path: &[&str]) -> Option<&Self> {
        path.iter()
            .fold(Some(self), |v, &b| v.and_then(|v| v.key(b)))
    }

    pub fn get_position(&self) -> &Position {
        match self {
            Ast::Null(Null { position, .. }) => position,
            Ast::Boolean(Boolean { position, .. }) => position,
            Ast::Integer(Integer { position, .. }) => position,
            Ast::Float(Float { position, .. }) => position,
            Ast::String(String { position, .. }) => position,
            Ast::Array(Array { position, .. }) => position,
            Ast::Object(Object { position, .. }) => position,
        }
    }
    pub fn get_annotations(&self) -> &Vec<Annotation> {
        match self {
            Ast::Null(Null { annotations, .. }) => annotations,
            Ast::Boolean(Boolean { annotations, .. }) => annotations,
            Ast::Integer(Integer { annotations, .. }) => annotations,
            Ast::Float(Float { annotations, .. }) => annotations,
            Ast::String(String { annotations, .. }) => annotations,
            Ast::Array(Array { annotations, .. }) => annotations,
            Ast::Object(Object { annotations, .. }) => annotations,
        }
    }
    pub fn get_annotations_mut(&mut self) -> &mut Vec<Annotation> {
        match self {
            Ast::Null(Null { annotations, .. }) => annotations,
            Ast::Boolean(Boolean { annotations, .. }) => annotations,
            Ast::Integer(Integer { annotations, .. }) => annotations,
            Ast::Float(Float { annotations, .. }) => annotations,
            Ast::String(String { annotations, .. }) => annotations,
            Ast::Array(Array { annotations, .. }) => annotations,
            Ast::Object(Object { annotations, .. }) => annotations,
        }
    }
}

impl From<&Ast> for Value {
    fn from(node: &Ast) -> Self {
        match node {
            Ast::Null(..) => Value::Null,
            Ast::Boolean(Boolean { value, .. }) => value.to_owned().into(),
            Ast::Integer(Integer { value, .. }) => value.to_owned().into(),
            Ast::Float(Float { value, .. }) => value.to_owned().into(),
            Ast::String(String { value, .. }) => value.to_owned().into(),
            Ast::Array(Array {
                elements: value, ..
            }) => Value::Array(value.into_iter().map(|v| v.into()).collect()),
            Ast::Object(Object {
                properties: value, ..
            }) => Value::Object(
                value
                    .into_iter()
                    .map(|v| (v.key.to_owned(), Value::from(&v.value)))
                    .collect::<Map<string::String, Value>>(),
            ),
        }
    }
}

impl From<Ast> for Value {
    fn from(node: Ast) -> Self {
        match node {
            Ast::Null(..) => Value::Null,
            Ast::Boolean(Boolean { value, .. }) => value.into(),
            Ast::Integer(Integer { value, .. }) => value.into(),
            Ast::Float(Float { value, .. }) => value.into(),
            Ast::String(String { value, .. }) => value.into(),
            Ast::Array(Array {
                elements: value, ..
            }) => Value::Array(value.into_iter().map(|v| v.into()).collect()),
            Ast::Object(Object {
                properties: value, ..
            }) => Value::Object(
                value
                    .into_iter()
                    .map(|v| (v.key, v.value.into()))
                    .collect::<Map<string::String, Value>>(),
            ),
        }
    }
}