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
use libucl_sys::*;
pub use self::types::Type;
pub use self::builder::Builder;
pub use self::emitter::Emitter;
use utils;
use std::convert::From;
use std::fmt;
pub mod types;
pub mod builder;
pub mod emitter;
#[cfg(test)]
mod test;
pub struct Object {
obj: *const ucl_object_t,
typ: Type
}
impl Object {
fn from_cptr(obj: *const ucl_object_t) -> Option<Self> {
if !obj.is_null() {
Some(Object {
obj: obj,
typ: Type::from(unsafe { ucl_object_type(obj) })
})
} else {
None
}
}
pub fn key(&self) -> Option<String> {
utils::to_str(unsafe { ucl_object_key(self.obj) })
}
pub fn get_type(&self) -> Type {
self.typ
}
pub fn as_int(&self) -> Option<i64> {
use libucl_sys::ucl_object_toint_safe;
if self.get_type() != Type::Int { return None }
unsafe {
let out: *mut i64 = &mut 0i64;
let res = ucl_object_toint_safe(self.obj, out);
if res && !out.is_null() {
Some(*out)
} else {
None
}
}
}
pub fn as_float(&self) -> Option<f64> {
use libucl_sys::ucl_object_todouble_safe;
if self.get_type() != Type::Float { return None }
unsafe {
let out: *mut f64 = &mut 0f64;
let res = ucl_object_todouble_safe(self.obj, out);
if res && !out.is_null() {
Some(*out)
} else {
None
}
}
}
pub fn as_bool(&self) -> Option<bool> {
use libucl_sys::ucl_object_toboolean_safe;
if self.get_type() != Type::Boolean { return None }
unsafe {
let out: *mut bool = &mut true;
let res = ucl_object_toboolean_safe(self.obj, out);
if res && !out.is_null() {
Some(*out)
} else {
None
}
}
}
pub fn as_string(&self) -> Option<String> {
use libucl_sys::ucl_object_tostring;
if self.get_type() != Type::String { return None }
unsafe {
let out = ucl_object_tostring(self.obj);
utils::to_str(out)
}
}
pub fn fetch<T: AsRef<str>>(&self, key: T) -> Option<Object> {
use libucl_sys::ucl_object_find_key;
if self.get_type() != Type::Object { return None }
unsafe {
let out = ucl_object_find_key(self.obj, utils::to_c_str(key.as_ref()));
Object::from_cptr(out)
}
}
pub fn fetch_path<T: AsRef<str>>(&self, path: T) -> Option<Object> {
use libucl_sys::ucl_lookup_path;
if self.get_type() != Type::Object { return None }
unsafe {
let out = ucl_lookup_path(self.obj, utils::to_c_str(path.as_ref()));
Object::from_cptr(out)
}
}
}
impl AsRef<Object> for Object {
fn as_ref(&self) -> &Self { self }
}
impl fmt::Debug for Object {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let string = Emitter::JSON.emit(&self);
if string.is_some() {
fmt.write_str(&string.unwrap())
} else {
Err(fmt::Error)
}
}
}