summaryrefslogtreecommitdiff
path: root/kit/unival.h
blob: e0362b165bcae486c0b2aab18b383ba4eeaf3e3e (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
#ifndef KIT_UNIVAL_H
#define KIT_UNIVAL_H

#include "string_builder.h"
#include "input_stream.h"

#ifdef __cplusplus
extern "C" {
#endif

enum {
  KIT_UV_NONE,
  KIT_UV_BOOLEAN,
  KIT_UV_INTEGER,
  KIT_UV_FLOAT,
  KIT_UV_STRING,
  KIT_UV_BYTES,
  KIT_UV_ARRAY,
  KIT_UV_COMPOSITE,
};

enum {
  KIT_UV_ENCODE_BZIP  = 1,
  KIT_UV_PRINT_PRETTY = 1,
  KIT_UV_PRINT_JSON   = 2,
};

typedef struct kit_unival_ kit_unival_t;

typedef KIT_DA(kit_unival_t) kit_da_unival_t;
typedef KIT_DA(u8) kit_uv_bytes_t;

struct kit_unival_ {
  i8 type;
  union {
    i8                boolean;
    i64               integer;
    f64               float_;
    kit_str_builder_t string;
    kit_uv_bytes_t    bytes;
    kit_da_unival_t   array;
    struct {
      kit_da_unival_t keys;
      kit_da_unival_t values;
    } composite;
  };
};

typedef struct {
  s32           status;
  kit_unival_t *value;
} kit_uv_decode_result_t;

kit_uv_decode_result_t kit_uv_decode(kit_is_handle_t  is,
                                     kit_allocator_t *alloc);

typedef struct {
  s32            status;
  kit_uv_bytes_t value;
} kit_uv_encode_result_t;

kit_uv_encode_result_t kit_uv_encode(kit_unival_t    *value,
                                     kit_allocator_t *alloc,
                                     i32              flags);

kit_uv_decode_result_t kit_uv_parse(kit_is_handle_t  is,
                                    kit_allocator_t *alloc);

kit_uv_decode_result_t kit_uv_parse_json(kit_is_handle_t  is,
                                         kit_allocator_t *alloc);

typedef struct {
  s32               status;
  kit_str_builder_t value;
} kit_uv_print_result_t;

kit_uv_print_result_t kit_uv_print(kit_unival_t    *value,
                                   kit_allocator_t *alloc, i32 flags);

void kit_uv_destroy(kit_unival_t *value);

#ifdef __cplusplus
}
#endif

#endif