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
use std::fmt;
use std::fmt::{Display, Formatter};
use serde_json::value::Value;
use std::collections::BTreeMap;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Ty {
    Null,
    Boolean,
    String,
    Number,
    Object,
    Array
}

pub type Array = Vec<Value>;
pub type Object = BTreeMap<String, Value>;

pub trait TyOf {
    fn ty(&self) -> Ty;
}

impl TyOf for Value {
    fn ty(&self) -> Ty {
        match *self {
            Value::Null      => Ty::Null,
            Value::String(_) => Ty::String,
            Value::Object(_) => Ty::Object,
            Value::I64(_)    => Ty::Number,
            Value::U64(_)    => Ty::Number,
            Value::F64(_)    => Ty::Number,
            Value::Array(_)  => Ty::Array,
            Value::Bool(_)   => Ty::Boolean
        }
    }
}

impl Display for Ty {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        fmt.write_str(match *self {
            Ty::Null    => "null",
            Ty::String  => "string",
            Ty::Object  => "object",
            Ty::Number  => "number",
            Ty::Array   => "array",
            Ty::Boolean => "boolean"
        })
    }
}