libucl_bind/
lib.rs

1#![allow(non_camel_case_types)]
2
3extern crate libc;
4#[macro_use] extern crate bitflags;
5
6use libc::{
7    c_char,
8    c_double,
9    c_int,
10    c_uint,
11    c_uchar,
12    c_void,
13    size_t
14};
15
16#[repr(C)]
17#[derive(Clone, Copy)]
18pub enum ucl_error_t {
19    UCL_EOK = 0,
20    UCL_ESYNTAX,
21    UCL_EIO,
22    UCL_ESTATE,
23    UCL_ENESTED,
24    UCL_EMACRO,
25    UCL_EINTERNAL,
26    UCL_ESSL
27}
28
29#[repr(C)]
30#[derive(Clone, Copy)]
31pub enum ucl_type_t {
32    UCL_OBJECT = 0,
33    UCL_ARRAY,
34    UCL_INT,
35    UCL_FLOAT,
36    UCL_STRING,
37    UCL_BOOLEAN,
38    UCL_TIME,
39    UCL_USERDATA,
40    UCL_NULL
41}
42
43#[repr(C)]
44#[derive(Clone, Copy)]
45pub enum ucl_emitter_t {
46    UCL_EMIT_JSON = 0,
47    UCL_EMIT_JSON_COMPACT,
48    UCL_EMIT_CONFIG,
49    UCL_EMIT_YAML,
50    UCL_EMIT_MSGPACK,
51    UCL_EMIT_MAX
52}
53
54bitflags! {
55#[repr(C)]
56    pub struct ucl_parser_flags_t: c_int {
57        const UCL_PARSER_DEFAULT = 0x0;
58        const UCL_PARSER_KEY_LOWERCASE = 0x1;
59        const UCL_PARSER_ZEROCOPY = 0x2;
60        const UCL_PARSER_NO_TIME = 0x4;
61        const UCL_PARSER_NO_IMPLICIT_ARRAYS = 0x8;
62    }
63}
64
65bitflags! {
66#[repr(C)]
67    pub struct ucl_string_flags_t : c_int {
68        const UCL_STRING_RAW = 0x0;
69        const UCL_STRING_ESCAPE = 0x1;
70        const UCL_STRING_TRIM = 0x2;
71        const UCL_STRING_PARSE_BOOLEAN = 0x4;
72        const UCL_STRING_PARSE_INT = 0x8;
73        const UCL_STRING_PARSE_DOUBLE = 0x10;
74        const UCL_STRING_PARSE_TIME = 0x20;
75        const UCL_STRING_PARSE_NUMBER = Self::UCL_STRING_PARSE_INT.bits
76            | Self::UCL_STRING_PARSE_DOUBLE.bits
77            | Self::UCL_STRING_PARSE_TIME.bits;
78        const UCL_STRING_PARSE = Self::UCL_STRING_PARSE_BOOLEAN.bits
79            | Self::UCL_STRING_PARSE_NUMBER.bits;
80        const UCL_STRING_PARSE_BYTES = 0x40;
81    }
82}
83
84bitflags! {
85#[repr(C)]
86    pub struct ucl_object_flags_t: c_int {
87        const UCL_OBJECT_ALLOCATED_KEY = 0x1;
88        const UCL_OBJECT_ALLOCATED_VALUE = 0x2;
89        const UCL_OBJECT_NEED_KEY_ESCAPE = 0x4;
90        const UCL_OBJECT_EPHEMERAL = 0x8;
91        const UCL_OBJECT_MULTILINE = 0x10;
92        const UCL_OBJECT_MULTIVALUE = 0x20;
93    }
94}
95
96#[repr(C)]
97pub struct ucl_object_t {
98    value: i64,
99    pub key: *const c_char,
100    pub next: *mut ucl_object_t,
101    pub prev: *mut ucl_object_t,
102    pub keylen: u32,
103    pub len: u32,
104    pub rc: u32,
105    pub flags: u16,
106    pub real_type: u16,
107    pub trash_stack: [*const c_char; 2]
108}
109
110impl ucl_object_t {
111    pub unsafe fn iv(&self) -> i64 { self.value }
112    pub unsafe fn sv(&self) -> *const c_char { std::mem::transmute(self.value) }
113    pub unsafe fn dv(&self) -> c_double { std::mem::transmute(self.value) }
114    pub unsafe fn av(&self) -> *mut c_void { std::mem::transmute(self.value) }
115    pub unsafe fn ov(&self) -> *mut c_void { std::mem::transmute(self.value) }
116    pub unsafe fn uv(&self) -> *mut c_void { std::mem::transmute(self.value) }
117}
118
119pub type ucl_userdata_dtor = extern fn(*mut c_void);
120pub type ucl_userdata_emitter = extern fn(*mut c_void) -> *const c_char;
121pub type ucl_object_iter_t = *mut c_void;
122pub type ucl_macro_handler = extern fn(*const c_uchar, size_t, *const ucl_object_t, *mut c_void) -> bool;
123pub type ucl_variable_handler = extern fn(*const c_uchar, size_t, *mut *mut c_uchar, *mut size_t, *mut bool, *mut c_void) -> bool;
124
125#[repr(C)]
126pub struct ucl_parser { _private: [u8; 0] }
127
128#[repr(C)]
129pub struct ucl_emitter_functions {
130    pub ucl_emitter_append_character: Option<extern fn(c_uchar, size_t, *mut c_void) -> c_int>,
131    pub ucl_emitter_append_len: Option<extern fn(*const c_uchar, size_t, *mut c_void) -> c_int>,
132    pub ucl_emitter_append_int: Option<extern fn(i64, *mut c_void) -> c_int>,
133    pub ucl_emitter_append_double: Option<extern fn(c_double, *mut c_void) -> c_int>,
134    pub ucl_emitter_free_func: Option<extern fn(*mut c_void)>,
135    pub ud: *mut c_void
136}
137
138#[repr(C)]
139pub struct ucl_emitter_operations {
140    ucl_emitter_write_elt: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool, bool),
141    ucl_emitter_start_object: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool),
142    ucl_emitter_end_object: extern fn(*mut ucl_emitter_context, *const ucl_object_t),
143    ucl_emitter_start_array: extern fn(*mut ucl_emitter_context, *const ucl_object_t, bool),
144    ucl_emitter_end_array: extern fn(*mut ucl_emitter_context, *const ucl_object_t),
145}
146
147#[repr(C)]
148pub struct ucl_emitter_context {
149    name: *const c_char,
150    id: c_int,
151    func: *const ucl_emitter_functions,
152    ops: *const ucl_emitter_operations,
153    indent: c_uint,
154    top: *const ucl_object_t,
155    data: [c_uchar; 1]
156}
157
158#[repr(C)]
159#[derive(Clone, Copy)]
160pub enum ucl_schema_error_code {
161    UCL_SCHEMA_OK = 0,
162    UCL_SCHEMA_TYPE_MISMATCH,
163    UCL_SCHEMA_INVALID_SCHEMA,
164    UCL_SCHEMA_MISSING_PROPERTY,
165    UCL_SCHEMA_CONSTRAINT,
166    UCL_SCHEMA_MISSING_DEPENDENCY,
167    UCL_SCHEMA_UNKNOWN
168}
169
170#[repr(C)]
171pub struct ucl_schema_error {
172    pub code: ucl_schema_error_code,
173    pub msg: [c_char; 128],
174    pub obj: *const ucl_object_t
175}
176
177extern {
178    // Parser functions
179    pub fn ucl_parser_new(flags: c_int) -> *mut ucl_parser;
180    pub fn ucl_parser_register_macro(parser: *mut ucl_parser, macro_name: *const c_char, handler: ucl_macro_handler, ud: *mut c_void);
181    pub fn ucl_parser_register_variable(parser: *mut ucl_parser, var: *const c_char, value: *const c_char);
182    pub fn ucl_parser_add_chunk(parser: *mut ucl_parser, data: *const c_char, len: size_t) -> bool;
183    pub fn ucl_parser_add_string(parser: *mut ucl_parser, data: *const c_char, len: size_t) -> bool;
184    pub fn ucl_parser_add_file(parser: *mut ucl_parser, filename: *const c_char) -> bool;
185    pub fn ucl_parser_get_object(parser: *mut ucl_parser) -> *mut ucl_object_t;
186    pub fn ucl_parser_get_error(parser: *mut ucl_parser) -> *const c_char;
187    pub fn ucl_parser_free(parser: *mut ucl_parser);
188    pub fn ucl_parser_set_filevars(parser: *mut ucl_parser, filename: *const c_char, need_expand: bool) -> bool;
189    pub fn ucl_parser_set_default_priority(parser: *mut ucl_parser, prio: c_uint) -> bool;
190    pub fn ucl_parser_set_variables_handler(parser: *mut ucl_parser, handler: ucl_variable_handler, ud: *mut c_void);
191    pub fn ucl_parser_add_chunk_priority(parser: *mut ucl_parser, data: *const c_uchar, len: size_t, prio: c_uint) -> bool;
192    pub fn ucl_parser_add_string_priority(parser: *mut ucl_parser, data: *const c_uchar, len: size_t, prio: c_uint) -> bool;
193    pub fn ucl_parser_add_file_priority(parser: *mut ucl_parser, filename: *const c_uchar, prio: c_uint) -> bool;
194    pub fn ucl_parser_add_fd(parser: *mut ucl_parser, fd: c_int) -> bool;
195    pub fn ucl_parser_add_fd_priority(parser: *mut ucl_parser, fd: c_int, prio: c_uint) -> bool;
196    pub fn ucl_parser_clear_error(parser: *mut ucl_parser);
197    pub fn ucl_parser_get_error_code(parser: *mut ucl_parser) -> c_int;
198    pub fn ucl_parser_get_error_column(parser: *mut ucl_parser) -> c_uint;
199    pub fn ucl_parser_get_error_linenum(parser: *mut ucl_parser) -> c_uint;
200
201    // Pubkey
202    pub fn ucl_pubkey_add(parser: *mut ucl_parser, key: *const c_char, len: size_t) -> bool;
203
204    // Emit functions
205    pub fn ucl_object_emit(obj: *const ucl_object_t, emit_type: ucl_emitter_t) -> *mut c_char;
206    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;
207    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_memory_funcs (
208    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_file_funcs (
209    // UCL_EXTERN struct ucl_emitter_functions* ucl_object_emit_fd_funcs (
210    // UCL_EXTERN void ucl_object_emit_streamline_start_container (
211    // UCL_EXTERN void ucl_object_emit_streamline_add_object (
212    // UCL_EXTERN void ucl_object_emit_streamline_end_container (
213    // UCL_EXTERN void ucl_object_emit_streamline_finish (
214    pub fn ucl_object_emit_funcs_free(f: *mut ucl_emitter_functions);
215    // UCL_EXTERN struct ucl_emitter_context* ucl_object_emit_streamline_new (
216
217    // Conversion functions
218    pub fn ucl_object_toboolean(obj: *const ucl_object_t) -> bool;
219    pub fn ucl_object_toboolean_safe(obj: *const ucl_object_t, target: *mut bool) -> bool;
220    pub fn ucl_object_todouble(obj: *const ucl_object_t) -> c_double;
221    pub fn ucl_object_todouble_safe (obj: *const ucl_object_t, target: *mut c_double) -> bool;
222    pub fn ucl_object_toint(obj: *const ucl_object_t) -> i64;
223    pub fn ucl_object_toint_safe(obj: *const ucl_object_t, target: *mut i64) -> bool;
224    pub fn ucl_object_tolstring(obj: *const ucl_object_t) -> *const c_char;
225    pub fn ucl_object_tostring(obj: *const ucl_object_t) -> *const c_char;
226    pub fn ucl_object_tostring_forced(obj: *const ucl_object_t) -> *const c_char;
227    pub fn ucl_object_tostring_safe(obj: *const ucl_object_t, target: *mut *const c_char) -> bool;
228    pub fn ucl_object_tolstring_safe(obj: *const ucl_object_t, target: *mut *const c_char, len: *mut size_t) -> bool;
229
230    // Generation functions
231    pub fn ucl_object_new() -> *mut ucl_object_t;
232    pub fn ucl_object_new_full(val: ucl_type_t, prio: c_uint) -> *mut ucl_object_t;
233    pub fn ucl_object_typed_new(val: ucl_type_t) -> *mut ucl_object_t;
234    pub fn ucl_object_new_userdata(dtor: ucl_userdata_dtor, emitter: ucl_userdata_emitter) -> *mut ucl_object_t;
235    pub fn ucl_object_fromint(val: i64) -> *mut ucl_object_t;
236    pub fn ucl_object_fromdouble(val: c_double) -> *mut ucl_object_t;
237    pub fn ucl_object_frombool(val: bool) -> *mut ucl_object_t;
238    pub fn ucl_object_fromstring(val: *const c_char) -> *mut ucl_object_t;
239    pub fn ucl_object_fromlstring(val: *const c_char, len: size_t) -> *mut ucl_object_t;
240    pub fn ucl_object_fromstring_common(val: *const c_char, len: size_t, flags: ucl_string_flags_t) -> *mut ucl_object_t;
241
242    // Utility functions
243    pub fn ucl_copy_key_trash(obj: *const ucl_object_t) -> *mut c_char;
244    pub fn ucl_copy_value_trash(obj: *const ucl_object_t) -> *mut c_char;
245    pub fn ucl_object_copy(other: *const ucl_object_t) -> *mut ucl_object_t;
246    pub fn ucl_object_type(obj: *const ucl_object_t) -> ucl_type_t;
247
248    // Object manipulation
249    // UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
250    // UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
251    pub fn ucl_object_merge(top: *mut ucl_object_t, elt: *mut ucl_object_t, copy: bool) -> bool;
252    // UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top,
253    // UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top,
254    // UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key,
255    pub fn ucl_object_pop_key(top: *mut ucl_object_t, key: *const c_char) -> *mut ucl_object_t;
256    // UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
257
258    // Array manipulation
259    // UCL_EXTERN bool ucl_array_append (ucl_object_t *top,
260    // UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top,
261    // UCL_EXTERN bool ucl_array_merge (ucl_object_t *top, ucl_object_t *elt,
262    // UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top,
263    pub fn ucl_array_size (top: *const ucl_object_t) -> usize;
264    pub fn ucl_array_head(top: *const ucl_object_t) -> *mut ucl_object_t;
265    pub fn ucl_array_tail(top: *const ucl_object_t) -> *mut ucl_object_t;
266    pub fn ucl_array_pop_last(top: *mut ucl_object_t) -> *mut ucl_object_t;
267    pub fn ucl_array_pop_first(top: *mut ucl_object_t) -> *mut ucl_object_t;
268    pub fn ucl_array_find_index(top: *const ucl_object_t, index: usize) -> *const ucl_object_t;
269    // UCL_EXTERN unsigned int ucl_array_index_of (ucl_object_t *top,
270
271    // Iteration functions
272    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;
273    pub fn ucl_object_iterate_new(obj: *const ucl_object_t) -> ucl_object_iter_t;
274    pub fn ucl_object_iterate_reset(it: ucl_object_iter_t, obj: *const ucl_object_t) -> ucl_object_iter_t;
275    pub fn ucl_object_iterate_safe(iter: ucl_object_iter_t, expand_values: bool) -> *const ucl_object_t;
276    pub fn ucl_object_iterate_free(it: ucl_object_iter_t);
277
278    // UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head,
279    pub fn ucl_object_lookup(obj: *const ucl_object_t, key: *const c_char) -> *const ucl_object_t;
280    // UCL_EXTERN const ucl_object_t* ucl_object_find_keyl (const ucl_object_t *obj,
281    pub fn ucl_object_lookup_path(obj: *const ucl_object_t, path: *const c_char) -> *const ucl_object_t;
282    // UCL_EXTERN const ucl_object_t *ucl_lookup_path_char (const ucl_object_t *obj,
283    pub fn ucl_object_key (obj: *const ucl_object_t) -> *const c_char;
284    pub fn ucl_object_keyl(obj: *const ucl_object_t, len: *mut size_t) -> *const c_char;
285    pub fn ucl_object_ref(obj: *const ucl_object_t) -> *mut ucl_object_t;
286    pub fn ucl_object_unref(obj: *mut ucl_object_t);
287    // UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1,
288    // UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
289    pub fn ucl_object_get_priority(obj: *const ucl_object_t) -> c_uint;
290    // UCL_EXTERN void ucl_object_set_priority (ucl_object_t *obj,
291    pub fn ucl_object_validate (schema: *const ucl_object_t, obj: *const ucl_object_t, err: *mut ucl_schema_error) -> bool;
292}