summaryrefslogtreecommitdiffstats
path: root/src/object.rs
blob: e0eae875188803dffe7d2319da9f95675408a72d (plain)
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
use crate::error::*;

pub enum ObjectType {
    Atom,
    Map,
    Array,
}

pub trait Object {
    fn get_type(&self) -> ObjectType;
    fn has_key(&self, key: &str) -> bool;
    fn has_index(&self, idx: usize) -> bool;

    fn at_index<'a>(&'a self, idx: usize) -> Result<Option<&'a Self>>;
    fn at_index_mut<'a>(&'a mut self, idx: usize) -> Result<Option<&'a mut Self>>;
    fn at_key<'a>(&'a self, key: &str) -> Result<Option<&'a Self>>;
    fn at_key_mut<'a>(&'a mut self, key: &str) -> Result<Option<&'a mut Self>>;
}

#[cfg(feature = "backend_toml")]
impl Object for toml::Value {
    fn get_type(&self) -> ObjectType {
        match self {
            toml::Value::Boolean(_)
                | toml::Value::Datetime(_)
                | toml::Value::Float(_)
                | toml::Value::Integer(_)
                | toml::Value::String(_) => ObjectType::Atom,

            toml::Value::Array(_) => ObjectType::Array,
            toml::Value::Table(_) => ObjectType::Map,
        }
    }

    fn has_key(&self, key: &str) -> bool {
        match self {
            toml::Value::Table(t) => t.contains_key(key),
            _ => false
        }
    }

    fn has_index(&self, idx: usize) -> bool {
        match self {
            toml::Value::Array(a) => a.len() > idx,
            _ => false
        }
    }

    fn at_index<'a>(&'a self, idx: usize) -> Result<Option<&'a Self>> {
        match self {
            toml::Value::Array(a) => Ok(a.get(idx)),
            toml::Value::Table(_) => Err(crate::error::Error::NoIndexInTable(idx)),
            _ => Err(crate::error::Error::QueryingValueAsArray(idx)),
        }
    }

    fn at_index_mut<'a>(&'a mut self, idx: usize) -> Result<Option<&'a mut Self>> {
        match self {
            toml::Value::Array(a) => Ok(a.get_mut(idx)),
            toml::Value::Table(_) => Err(crate::error::Error::NoIndexInTable(idx)),
            _ => Err(crate::error::Error::QueryingValueAsArray(idx)),
        }
    }

    fn at_key<'a>(&'a self, key: &str) -> Result<Option<&'a Self>> {
        match self {
            toml::Value::Table(t) => Ok(t.get(key)),
            toml::Value::Array(_) => Err(crate::error::Error::NoIdentifierInArray(key.to_string())),
            _ => Err(crate::error::Error::QueryingValueAsTable(key.to_string())),
        }
    }

    fn at_key_mut<'a>(&'a mut self, key: &str) -> Result<Option<&'a mut Self>> {
        match self {
            toml::Value::Table(t) => Ok(t.get_mut(key)),
            toml::Value::Array(_) => Err(crate::error::Error::NoIdentifierInArray(key.to_string())),
            _ => Err(crate::error::Error::QueryingValueAsTable(key.to_string())),
        }
    }
}


#[cfg(feature = "backend_serde_json")]
impl Object for serde_json::Value {
    fn get_type(&self) -> ObjectType {
        match self {
            serde_json::Value::Null
                | serde_json::Value::Bool(_)
                | serde_json::Value::Number(_)
                | serde_json::Value::String(_) => ObjectType::Atom,

            serde_json::Value::Array(_) => ObjectType::Array,
            serde_json::Value::Object(_) => ObjectType::Map,
        }
    }

    fn has_key(&self, key: &str) -> bool {
        match self {
            serde_json::Value::Object(t) => t.contains_key(key),
            _ => false
        }
    }

    fn has_index(&self, idx: usize) -> bool {
        match self {
            serde_json::Value::Array(a) => a.len() > idx,
            _ => false
        }
    }

    fn at_index<'a>(&'a self, idx: usize) -> Result<Option<&'a Self>> {
        match self {
            serde_json::Value::Array(a) => Ok(a.get(idx)),
            serde_json::Value::Object(_) => Err(crate::error::Error::NoIndexInTable(idx)),
            _ => Err(crate::error::Error::QueryingValueAsArray(idx)),
        }
    }

    fn at_index_mut<'a>(&'a mut self, idx: usize) -> Result<Option<&'a mut Self>> {
        match self {
            serde_json::Value::Array(a) => Ok(a.get_mut(idx)),
            serde_json::Value::Object(_) => Err(crate::error::Error::NoIndexInTable(idx)),
            _ => Err(crate::error::Error::QueryingValueAsArray(idx)),
        }
    }

    fn at_key<'a>(&'a self, key: &str) -> Result<Option<&'a Self>> {
        match self {
            serde_json::Value::Object(t) => Ok(t.get(key)),
            serde_json::Value::Array(_) => Err(crate::error::Error::NoIdentifierInArray(key.to_string())),
            _ => Err(crate::error::Error::QueryingValueAsTable(key.to_string())),
        }
    }

    fn at_key_mut<'a>(&'a mut self, key: &str) -> Result<Option<&'a mut Self>> {
        match self {
            serde_json::Value::Object(t) => Ok(t.get_mut(key)),
            serde_json::Value::Array(_) => Err(crate::error::Error::NoIdentifierInArray(key.to_string())),
            _ => Err(crate::error::Error::QueryingValueAsTable(key.to_string())),
        }
    }
}