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
#![allow(non_camel_case_types)]

extern crate libc;
#[macro_use] extern crate bitflags;

use libc::{
    c_char,
    c_double,
    c_int,
    c_uint,
    c_uchar,
    c_void,
    size_t
};

#[repr(C)]
#[derive(Clone, Copy)]
pub enum ucl_error_t {
    UCL_EOK = 0,
    UCL_ESYNTAX,
    UCL_EIO,
    UCL_ESTATE,
    UCL_ENESTED,
    UCL_EMACRO,
    UCL_EINTERNAL,
    UCL_ESSL
}

#[repr(C)]
#[derive(Clone, Copy)]
pub enum ucl_type_t {
    UCL_OBJECT = 0,
    UCL_ARRAY,
    UCL_INT,
    UCL_FLOAT,
    UCL_STRING,
    UCL_BOOLEAN,
    UCL_TIME,
    UCL_USERDATA,
    UCL_NULL
}

#[repr(C)]
#[derive(Clone, Copy)]
pub enum ucl_emitter_t {
    UCL_EMIT_JSON = 0,
    UCL_EMIT_JSON_COMPACT,
    UCL_EMIT_CONFIG,
    UCL_EMIT_YAML,
    UCL_EMIT_MSGPACK,
    UCL_EMIT_MAX
}

bitflags! {
#[repr(C)]
    pub struct ucl_parser_flags_t: c_int {
        const UCL_PARSER_DEFAULT = 0x0;
        const UCL_PARSER_KEY_LOWERCASE = 0x1;
        const UCL_PARSER_ZEROCOPY = 0x2;
        const UCL_PARSER_NO_TIME = 0x4;
        const UCL_PARSER_NO_IMPLICIT_ARRAYS = 0x8;
    }
}

bitflags! {
#[repr(C)]
    pub struct ucl_string_flags_t : c_int {
        const UCL_STRING_RAW = 0x0;
        const UCL_STRING_ESCAPE = 0x1;
        const UCL_STRING_TRIM = 0x2;
        const UCL_STRING_PARSE_BOOLEAN = 0x4;
        const UCL_STRING_PARSE_INT = 0x8;
        const UCL_STRING_PARSE_DOUBLE = 0x10;
        const UCL_STRING_PARSE_TIME = 0x20;
        const UCL_STRING_PARSE_NUMBER = Self::UCL_STRING_PARSE_INT.bits
            | Self::UCL_STRING_PARSE_DOUBLE.bits
            | Self::UCL_STRING_PARSE_TIME.bits;
        const UCL_STRING_PARSE = Self::UCL_STRING_PARSE_BOOLEAN.bits
            | Self::UCL_STRING_PARSE_NUMBER.bits;
        const UCL_STRING_PARSE_BYTES = 0x40;
    }
}

bitflags! {
#[repr(C)]
    pub struct ucl_object_flags_t: c_int {
        const UCL_OBJECT_ALLOCATED_KEY = 0x1;
        const UCL_OBJECT_ALLOCATED_VALUE = 0x2;
        const UCL_OBJECT_NEED_KEY_ESCAPE = 0x4;
        const UCL_OBJECT_EPHEMERAL = 0x8;
        const UCL_OBJECT_MULTILINE = 0x10;
        const UCL_OBJECT_MULTIVALUE = 0x20;
    }
}

#[repr(C)]
pub struct ucl_object_t {
    value: i64,
    pub key: *const c_char,
    pub next: *mut ucl_object_t,
    pub prev: *mut ucl_object_t,
    pub keylen: u32,
    pub len: u32,
    pub rc: u32,
    pub flags: u16,
    pub real_type: u16,
    pub trash_stack: [*const c_char; 2]
}

impl ucl_object_t {
    pub unsafe fn iv(&self) -> i64 { self.value }
    pub unsafe fn sv(&self) -> *const c_char { std::mem::transmute(self.value) }
    pub unsafe fn dv(&self) -> c_double { std::mem::transmute(self.value) }
    pub unsafe fn av(&self) -> *mut c_void { std::mem::transmute(self.value) }
    pub unsafe fn ov(&self) -> *mut c_void { std::mem::transmute(self.value) }
    pub unsafe fn uv(&self) -> *mut c_void { std::mem::transmute(self.value) }
}

pub type ucl_userdata_dtor = extern fn(*mut c_void);
pub type ucl_userdata_emitter = extern fn(*mut c_void) -> *const c_char;
pub type ucl_object_iter_t = *mut c_void;
pub type ucl_macro_handler = extern fn(*const c_uchar, size_t, *const ucl_object_t, *mut c_void) -> bool;
pub type ucl_variable_handler = extern fn(*const c_uchar, size_t, *mut *mut c_uchar, *mut size_t, *mut bool, *mut c_void) -> bool;

#[repr(C)]
pub struct ucl_parser { _private: [u8; 0] }

#[repr(C)]
pub struct ucl_emitter_functions {
    pub ucl_emitter_append_character: Option<extern fn(c_uchar, size_t, *mut c_void) -> c_int>,
    pub ucl_emitter_append_len: Option<extern fn(*const c_uchar, size_t, *mut c_void) -> c_int>,
    pub ucl_emitter_append_int: Option<extern fn(i64, *mut c_void) -> c_int>,
    pub ucl_emitter_append_double: Option<extern fn(c_double, *mut c_void) -> c_int>,
    pub ucl_emitter_free_func: Option<extern fn(*mut c_void)>,
    pub ud: *mut c_void
}

#[repr(C)]
pub struct ucl_emitter_operations {
    ucl_emitter_write_elt: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool, bool),
    ucl_emitter_start_object: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool),
    ucl_emitter_end_object: extern fn(*mut ucl_emitter_context, *const ucl_object_t),
    ucl_emitter_start_array: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool),
    ucl_emitter_end_array: extern fn(*mut ucl_emitter_context, *const ucl_object_t),
}

#[repr(C)]
pub struct ucl_emitter_context {
    name: *const c_char,
    id: c_int,
    func: *const ucl_emitter_functions,
    ops: *const ucl_emitter_operations,
    indent: c_uint,
    top: *const ucl_object_t,
    data: [c_uchar; 1]
}

#[repr(C)]
#[derive(Clone, Copy)]
pub enum ucl_schema_error_code {
    UCL_SCHEMA_OK = 0,
    UCL_SCHEMA_TYPE_MISMATCH,
    UCL_SCHEMA_INVALID_SCHEMA,
    UCL_SCHEMA_MISSING_PROPERTY,
    UCL_SCHEMA_CONSTRAINT,
    UCL_SCHEMA_MISSING_DEPENDENCY,
    UCL_SCHEMA_UNKNOWN
}

#[repr(C)]
pub struct ucl_schema_error {
    pub code: ucl_schema_error_code,
    pub msg: [c_char; 128],
    pub obj: *const ucl_object_t
}

extern {
    // Parser functions
    pub fn ucl_parser_new(flags: c_int) -> *mut ucl_parser;
    pub fn ucl_parser_register_macro(parser: *mut ucl_parser, macro_name: *const c_char, handler: ucl_macro_handler, ud: *mut c_void);
    pub fn ucl_parser_register_variable(parser: *mut ucl_parser, var: *const c_char, value: *const c_char);
    pub fn ucl_parser_add_chunk(parser: *mut ucl_parser, data: *const c_char, len: size_t) -> bool;
    pub fn ucl_parser_add_string(parser: *mut ucl_parser, data: *const c_char, len: size_t) -> bool;
    pub fn ucl_parser_add_file(parser: *mut ucl_parser, filename: *const c_char) -> bool;
    pub fn ucl_parser_get_object(parser: *mut ucl_parser) -> *mut ucl_object_t;
    pub fn ucl_parser_get_error(parser: *mut ucl_parser) -> *const c_char;
    pub fn ucl_parser_free(parser: *mut ucl_parser);
    pub fn ucl_parser_set_filevars(parser: *mut ucl_parser, filename: *const c_char, need_expand: bool) -> bool;
    pub fn ucl_parser_set_default_priority(parser: *mut ucl_parser, prio: c_uint) -> bool;
    pub fn ucl_parser_set_variables_handler(parser: *mut ucl_parser, handler: ucl_variable_handler, ud: *mut c_void);
    pub fn ucl_parser_add_chunk_priority(parser: *mut ucl_parser, data: *const c_uchar, len: size_t, prio: c_uint) -> bool;
    pub fn ucl_parser_add_string_priority(parser: *mut ucl_parser, data: *const c_uchar, len: size_t, prio: c_uint) -> bool;
    pub fn ucl_parser_add_file_priority(parser: *mut ucl_parser, filename: *const c_uchar, prio: c_uint) -> bool;
    pub fn ucl_parser_add_fd(parser: *mut ucl_parser, fd: c_int) -> bool;
    pub fn ucl_parser_add_fd_priority(parser: *mut ucl_parser, fd: c_int, prio: c_uint) -> bool;
    pub fn ucl_parser_clear_error(parser: *mut ucl_parser);
    pub fn ucl_parser_get_error_code(parser: *mut ucl_parser) -> c_int;
    pub fn ucl_parser_get_error_column(parser: *mut ucl_parser) -> c_uint;
    pub fn ucl_parser_get_error_linenum(parser: *mut ucl_parser) -> c_uint;

    // Pubkey
    pub fn ucl_pubkey_add(parser: *mut ucl_parser, key: *const c_char, len: size_t) -> bool;

    // Emit functions
    pub fn ucl_object_emit(obj: *const ucl_object_t, emit_type: ucl_emitter_t) -> *mut c_char;
    pub fn ucl_object_emit_full(obj: *const ucl_object_t, emit_type: ucl_emitter_t, emitter: *mut ucl_emitter_functions, comments: *const ucl_object_t) -> bool;
    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_memory_funcs (
    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_file_funcs (
    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_fd_funcs (
    // UCL_EXTERN void ucl_object_emit_streamline_start_container (
    // UCL_EXTERN void ucl_object_emit_streamline_add_object (
    // UCL_EXTERN void ucl_object_emit_streamline_end_container (
    // UCL_EXTERN void ucl_object_emit_streamline_finish (
    pub fn ucl_object_emit_funcs_free(f: *mut ucl_emitter_functions);
    // UCL_EXTERN struct ucl_emitter_context* ucl_object_emit_streamline_new (

    // Conversion functions
    pub fn ucl_object_toboolean(obj: *const ucl_object_t) -> bool;
    pub fn ucl_object_toboolean_safe(obj: *const ucl_object_t, target: *mut bool) -> bool;
    pub fn ucl_object_todouble(obj: *const ucl_object_t) -> c_double;
    pub fn ucl_object_todouble_safe (obj: *const ucl_object_t, target: *mut c_double) -> bool;
    pub fn ucl_object_toint(obj: *const ucl_object_t) -> i64;
    pub fn ucl_object_toint_safe(obj: *const ucl_object_t, target: *mut i64) -> bool;
    pub fn ucl_object_tolstring(obj: *const ucl_object_t) -> *const c_char;
    pub fn ucl_object_tostring(obj: *const ucl_object_t) -> *const c_char;
    pub fn ucl_object_tostring_forced(obj: *const ucl_object_t) -> *const c_char;
    pub fn ucl_object_tostring_safe(obj: *const ucl_object_t, target: *mut *const c_char) -> bool;
    pub fn ucl_object_tolstring_safe(obj: *const ucl_object_t, target: *mut *const c_char, len: *mut size_t) -> bool;

    // Generation functions
    pub fn ucl_object_new() -> *mut ucl_object_t;
    pub fn ucl_object_new_full(val: ucl_type_t, prio: c_uint) -> *mut ucl_object_t;
    pub fn ucl_object_typed_new(val: ucl_type_t) -> *mut ucl_object_t;
    pub fn ucl_object_new_userdata(dtor: ucl_userdata_dtor, emitter: ucl_userdata_emitter) -> *mut ucl_object_t;
    pub fn ucl_object_fromint(val: i64) -> *mut ucl_object_t;
    pub fn ucl_object_fromdouble(val: c_double) -> *mut ucl_object_t;
    pub fn ucl_object_frombool(val: bool) -> *mut ucl_object_t;
    pub fn ucl_object_fromstring(val: *const c_char) -> *mut ucl_object_t;
    pub fn ucl_object_fromlstring(val: *const c_char, len: size_t) -> *mut ucl_object_t;
    pub fn ucl_object_fromstring_common(val: *const c_char, len: size_t, flags: ucl_string_flags_t) -> *mut ucl_object_t;

    // Utility functions
    pub fn ucl_copy_key_trash(obj: *const ucl_object_t) -> *mut c_char;
    pub fn ucl_copy_value_trash(obj: *const ucl_object_t) -> *mut c_char;
    pub fn ucl_object_copy(other: *const ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_object_type(obj: *const ucl_object_t) -> ucl_type_t;

    // Object manipulation
    // UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
    // UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
    pub fn ucl_object_merge(top: *mut ucl_object_t, elt: *mut ucl_object_t, copy: bool) -> bool;
    // UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top,
    // UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top,
    // UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key,
    pub fn ucl_object_pop_key(top: *mut ucl_object_t, key: *const c_char) -> *mut ucl_object_t;
    // UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,

    // Array manipulation
    // UCL_EXTERN bool ucl_array_append (ucl_object_t *top,
    // UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top,
    // UCL_EXTERN bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt,
    // UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top,
    pub fn ucl_array_size (top: *const ucl_object_t) -> usize;
    pub fn ucl_array_head(top: *const ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_array_tail(top: *const ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_array_pop_last(top: *mut ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_array_pop_first(top: *mut ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_array_find_index(top: *const ucl_object_t, index: usize) -> *const ucl_object_t;
    // UCL_EXTERN unsigned int ucl_array_index_of (ucl_object_t *top,

    // Iteration functions
    pub fn ucl_object_iterate_with_error(obj: *const ucl_object_t, iter: *mut ucl_object_iter_t, expand_values: bool, err_ptr: *mut c_int) -> *const ucl_object_t;
    pub fn ucl_object_iterate_new(obj: *const ucl_object_t) -> ucl_object_iter_t;
    pub fn ucl_object_iterate_reset(it: ucl_object_iter_t, obj: *const ucl_object_t) -> ucl_object_iter_t;
    pub fn ucl_object_iterate_safe(iter: ucl_object_iter_t, expand_values: bool) -> *const ucl_object_t;
    pub fn ucl_object_iterate_free(it: ucl_object_iter_t);

    // UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head,
    pub fn ucl_object_lookup(obj: *const ucl_object_t, key: *const c_char) -> *const ucl_object_t;
    // UCL_EXTERN const ucl_object_t* ucl_object_find_keyl (const ucl_object_t *obj,
    pub fn ucl_object_lookup_path(obj: *const ucl_object_t, path: *const c_char) -> *const ucl_object_t;
    // UCL_EXTERN const ucl_object_t *ucl_lookup_path_char (const ucl_object_t *obj,
    pub fn ucl_object_key (obj: *const ucl_object_t) -> *const c_char;
    pub fn ucl_object_keyl(obj: *const ucl_object_t, len: *mut size_t) -> *const c_char;
    pub fn ucl_object_ref(obj: *const ucl_object_t) -> *mut ucl_object_t;
    pub fn ucl_object_unref(obj: *mut ucl_object_t);
    // UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1,
    // UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
    pub fn ucl_object_get_priority(obj: *const ucl_object_t) -> c_uint;
    // UCL_EXTERN void ucl_object_set_priority (ucl_object_t *obj,
    pub fn ucl_object_validate (schema: *const ucl_object_t, obj: *const ucl_object_t, err: *mut ucl_schema_error) -> bool;
}