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
use std::collections::HashMap;

use crate::{encode_str, Codec, MapKey, Tag, ValueType, VarInt, EMPTY_OPTIONAL};

// Writing Tags

#[inline]
pub fn tag_bool(output: &mut Vec<u8>, tag: &str, value: bool) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_zero(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    output.push(0);
}

#[inline]
pub fn tag_u8(output: &mut Vec<u8>, tag: &str, value: u8) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_u16(output: &mut Vec<u8>, tag: &str, value: u16) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_u32(output: &mut Vec<u8>, tag: &str, value: u32) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_usize(output: &mut Vec<u8>, tag: &str, value: usize) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_u64(output: &mut Vec<u8>, tag: &str, value: u64) {
    Tag::encode_from(tag, &ValueType::VarInt, output);
    value.encode(output);
}

#[inline]
pub fn tag_empty_str(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::String, output);
    encode_empty_str(output);
}

#[inline]
pub fn tag_empty_blob(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::Blob, output);
    output.push(0);
}

#[inline]
pub fn tag_str(output: &mut Vec<u8>, tag: &str, value: &str) {
    Tag::encode_from(tag, &ValueType::String, output);
    encode_str(value, output);
}

#[inline]
pub fn encode_empty_str(output: &mut Vec<u8>) {
    output.push(1);
    output.push(0);
}

#[inline]
pub fn tag_group_start(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::Group, output);
}

#[inline]
pub fn tag_start(output: &mut Vec<u8>, tag: &str, ty: ValueType) {
    Tag::encode_from(tag, &ty, output);
}

#[inline]
pub fn tag_value<T: Codec>(output: &mut Vec<u8>, tag: &str, value: &T) {
    Tag::encode_from(tag, &T::value_type(), output);
    T::encode(value, output);
}

#[inline]
pub fn tag_list_start(output: &mut Vec<u8>, tag: &str, ty: ValueType, len: usize) {
    Tag::encode_from(tag, &ValueType::List, output);
    ty.encode(output);
    len.encode(output);
}

#[inline]
pub fn tag_optional_start(output: &mut Vec<u8>, tag: &str, ty: u8) {
    Tag::encode_from(tag, &ValueType::Optional, output);
    output.push(ty);
}

#[inline]
pub fn tag_optional_none(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::Optional, output);
    output.push(EMPTY_OPTIONAL);
}

#[inline]
pub fn tag_list<T: Codec>(output: &mut Vec<u8>, tag: &str, value: Vec<T>) {
    Tag::encode_from(tag, &ValueType::List, output);
    value.encode(output);
}

#[inline]
pub fn tag_list_empty(output: &mut Vec<u8>, tag: &str, ty: ValueType) {
    Tag::encode_from(tag, &ValueType::List, output);
    ty.encode(output);
    output.push(0);
}

#[inline]
pub fn tag_var_int_list_empty(output: &mut Vec<u8>, tag: &str) {
    Tag::encode_from(tag, &ValueType::VarIntList, output);
    output.push(0);
}

#[inline]
pub fn tag_var_int_list<T: VarInt>(output: &mut Vec<u8>, tag: &str, values: Vec<T>) {
    Tag::encode_from(tag, &ValueType::VarIntList, output);
    values.len().encode(output);
    for value in values {
        value.encode(output);
    }
}

pub fn tag_map_start(
    output: &mut Vec<u8>,
    tag: &str,
    key: ValueType,
    value: ValueType,
    len: usize,
) {
    Tag::encode_from(tag, &ValueType::Map, output);
    key.encode(output);
    value.encode(output);
    len.encode(output);
}

#[inline]
pub fn map_value(output: &mut Vec<u8>, key: impl MapKey, value: impl Codec) {
    key.encode(output);
    value.encode(output);
}

pub fn tag_map<K: MapKey, V: Codec>(output: &mut Vec<u8>, tag: &str, value: &HashMap<K, V>) {
    Tag::encode_from(tag, &ValueType::Map, output);
    K::value_type().encode(output);
    V::value_type().encode(output);
    value.len().encode(output);
    for (key, value) in value {
        key.encode(output);
        value.encode(output);
    }
}

#[inline]
pub fn tag_group_end(output: &mut Vec<u8>) {
    output.push(0)
}

#[inline]
pub fn tag_triple<A: VarInt, B: VarInt, C: VarInt>(
    output: &mut Vec<u8>,
    tag: &str,
    value: &(A, B, C),
) {
    Tag::encode_from(tag, &ValueType::Triple, output);
    value.encode(output);
}

#[inline]
pub fn tag_pair<A: VarInt, B: VarInt>(output: &mut Vec<u8>, tag: &str, value: &(A, B)) {
    Tag::encode_from(tag, &ValueType::Pair, output);
    value.encode(output);
}