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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use std::convert::From;
use std::ffi::CString;
use std::fmt;

use libc::{
    c_double,
    c_uchar,
    c_void,
};

use error::{UclSchemaError, UclSchemaErrorType};
use libucl_bind::*;
use utils;

pub use self::builder::Builder;
pub use self::emitter::Emitter;
pub use self::types::Type;

pub mod types;
pub mod builder;
pub mod emitter;

#[cfg(test)]
mod test;

// Helper functions
extern fn append_char(c: c_uchar, _num_chars: usize, ptr: *mut c_void) -> libc::c_int {
    assert!(!ptr.is_null());

    unsafe {
        let tmp = ptr as *mut String;
        (*tmp).push(c as char);
    }

    0
}

extern fn append_len(c: *const c_uchar, len: usize, ptr: *mut c_void) -> libc::c_int {
    assert!(!c.is_null());
    assert!(!ptr.is_null());

    unsafe {
        let out = ptr as *mut String;
        let slice = std::slice::from_raw_parts(c, len);
        (*out).push_str(std::str::from_utf8(slice).unwrap());
    }

    0
}

extern fn append_int(i: i64, ptr: *mut c_void) -> libc::c_int {
    assert!(!ptr.is_null());

    unsafe {
        let tmp = ptr as *mut String;
        let tmp_str = i.to_string();
        (*tmp).push_str(tmp_str.as_str());
    }

    0
}

extern fn append_double(d: c_double, ptr: *mut c_void) -> libc::c_int {
    assert!(!ptr.is_null());

    unsafe {
        let tmp = ptr as *mut String;
        let tmp_str = d.to_string();
        (*tmp).push_str(tmp_str.as_str());
    }

    0
}

/// File element object.
///
/// This structure is immutable typed reference to object inside parsed tree. It can be one of
/// `Type` elements and can be cast only to given type.
pub struct Object {
    obj: *mut ucl_object_t,
    it: ucl_object_iter_t,
    typ: Type
}

impl Object {
    /// Create new `Object` from const raw pointer. Internal use only.
    fn from_cptr(obj: *const ucl_object_t) -> Option<Self> {
        if !obj.is_null() {
            Some(Object {
                obj: unsafe { ucl_object_ref (obj) },
                it: std::ptr::null_mut(),
                typ: Type::from(unsafe { ucl_object_type(obj) })
            })
        } else {
            None
        }
    }

    #[allow(dead_code)]
    /// Create new `Object` from mut raw pointer and take ownership. Internal use only.
    fn from_mut_cptr(obj: *mut ucl_object_t) -> Option<Self> {
        if !obj.is_null() {
            Some(Object {
                obj: obj,
                it: std::ptr::null_mut(),
                typ: Type::from(unsafe { ucl_object_type(obj) })
            })
        } else {
            None
        }
    }

    fn default_emit_funcs() -> ucl_emitter_functions {
        ucl_emitter_functions {
            ucl_emitter_append_character: Some(append_char),
            ucl_emitter_append_len: Some(append_len),
            ucl_emitter_append_int: Some(append_int),
            ucl_emitter_append_double: Some(append_double),
            ucl_emitter_free_func: None,
            ud: std::ptr::null_mut(),
        }
    }

    // pub fn priority(&self) -> usize {
    //     unsafe { ucl_object_get_priority(self.obj) as usize }
    // }

    pub fn dump_into(&self, emmiter: Emitter) -> String {
        match emmiter.emit(self){
            Some(s) => s,
            None => String::from("")
        }
    }

    pub fn dump(&self) -> String {
        let out: Box<String> = Box::new(String::new());
        let mut emit = Self::default_emit_funcs();

        unsafe {
            emit.ud = std::mem::transmute::<Box<String>, *mut c_void>(out);
            ucl_object_emit_full(self.obj, ucl_emitter_t::UCL_EMIT_JSON, &mut emit, std::ptr::null());
            let out_final: Box<String> = std::mem::transmute(emit.ud);
            return *out_final
        }
    }

    pub fn size(&self) -> usize {
        if self.typ == Type::Array {
            return unsafe { ucl_array_size(self.obj) }
        }

        0
    }

    pub fn at(&self, i: usize) -> Option<Object> {
        if self.typ == Type::Array {
            unsafe {
                let out = ucl_array_find_index(self.obj, i);

                return Object::from_cptr(out)
            }
        }

        None
    }

    pub fn iter_reset(&mut self) {
        if !self.it.is_null() {
            self.it = unsafe { ucl_object_iterate_reset(self.it, self.obj) }
        }
    }

    /// Return key assigned to object.
    pub fn key(&self) -> Option<String> {
        utils::to_str(unsafe { ucl_object_key(self.obj) })
    }

    /// Return type of object.
    pub fn get_type(&self) -> Type {
        self.typ
    }

    /// Return `i64` value
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::object::Builder::from(10).build();
    /// assert_eq!(obj.as_int(), Some(10));
    ///
    /// let obj = libucl::object::Builder::from("test_string").build();
    /// assert_eq!(obj.as_int(), None);
    /// ```
    pub fn as_int(&self) -> Option<i64> {

        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
            }
        }
    }

    /// Return `f64` value
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::object::Builder::from(10f64).build();
    /// assert_eq!(obj.as_float(), Some(10.0));
    ///
    /// let obj = libucl::object::Builder::from("test_string").build();
    /// assert_eq!(obj.as_float(), None);
    /// ```
    pub fn as_float(&self) -> Option<f64> {

        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
            }
        }
    }

    /// Return boolean value
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::object::Builder::from(true).build();
    /// assert_eq!(obj.as_bool(), Some(true));
    ///
    /// let obj = libucl::object::Builder::from(10).build();
    /// assert_eq!(obj.as_bool(), None);
    /// ```
    pub fn as_bool(&self) -> Option<bool> {

        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
            }
        }
    }

    /// Return string value
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::object::Builder::from("test_string").build();
    /// assert_eq!(obj.as_string(), Some("test_string".to_string()));
    ///
    /// let obj = libucl::object::Builder::from(10).build();
    /// assert_eq!(obj.as_string(), None);
    /// ```
    pub fn as_string(&self) -> Option<String> {

        if self.get_type() != Type::String { return None }
        unsafe {
            let out = ucl_object_tostring(self.obj);

            utils::to_str(out)
        }
    }

    /// Fetch object under key
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::Parser::new().parse("a = b;").unwrap();
    /// assert_eq!(obj.fetch("a").unwrap().as_string(), Some("b".to_string()));
    /// ```
    pub fn fetch<T: AsRef<str>>(&self, key: T) -> Option<Object> {
        //use libucl_sys::ucl_object_lookup;

        if self.get_type() != Type::Object { return None }

        let k = CString::new(key.as_ref()).unwrap();
        unsafe {
            let out = ucl_object_lookup(self.obj, k.as_ptr());

            Object::from_cptr(out)
        }
    }

    /// Fetch object at the end of path delimeted by `.` (dot)
    ///
    /// # Examples
    ///
    /// ```rust
    /// let obj = libucl::Parser::new().parse("a = { b = c; }").unwrap();
    /// assert_eq!(obj.fetch_path("a.b").unwrap().as_string(), Some("c".to_string()));
    /// ```
    pub fn fetch_path<T: AsRef<str>>(&self, path: T) -> Option<Object> {
        if self.get_type() != Type::Object { return None }

        let p = CString::new(path.as_ref()).unwrap();
        unsafe {
            let out = ucl_object_lookup_path(self.obj, p.as_ptr());

            Object::from_cptr(out)
        }
    }

    pub fn validate_with_schema(&self, schema: &Object) -> Result<(), UclSchemaError> {
        unsafe {
            let mut err = ucl_schema_error {
                code: ucl_schema_error_code::UCL_SCHEMA_OK,
                msg: ['\0' as i8; 128],
                obj: std::ptr::null_mut(),
            };
            if ucl_object_validate(schema.obj,self.obj, &mut err) {
                return Ok(())
            }
            Err(UclSchemaErrorType::from_code(err.code as i32, String::from("")))
        }
    }
}

impl Iterator for Object {
    type Item = super::Object;

    fn next(&mut self) -> Option<Self::Item> {

        if self.it.is_null() {
           self.it = unsafe { ucl_object_iterate_new(self.obj) }
        }

        if self.typ != Type::Array {
            return None
        }

        let cur = unsafe { ucl_object_iterate_safe (self.it, true) };
        if cur.is_null() {
            return None
        }

        super::Object::from_cptr(cur)
    }
}

impl Drop for Object {
    fn drop(&mut self) {
        unsafe {
            if !self.it.is_null() { ucl_object_iterate_free(self.it); }
            if !self.obj.is_null() { ucl_object_unref(self.obj); }
        }
    }
}

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)
        }
    }
}