Current section

28 Versions

Jump to

Compare versions

9 files changed
+357 additions
-653 deletions
  @@ -1,5 +1,5 @@
1 1 name = "gleam_bson"
2 - version = "0.17.0"
2 + version = "0.18.0"
3 3
4 4 description = "A bson encoder and decoder written in gleam"
5 5 licences = ["Apache-2.0"]
  @@ -11,7 +11,7 @@ repository = {type = "github", user = "massivefermion", repo = "gleam_bson"}
11 11
12 12 [dependencies]
13 13 gleam_crypto = "~> 0.3"
14 - gleam_stdlib = "~> 0.24"
14 + gleam_stdlib = "~> 0.25"
15 15
16 16 [dev-dependencies]
17 17 gleeunit = "~> 0.7"
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"gleam_bson">>}.
2 2 {<<"app">>, <<"gleam_bson">>}.
3 - {<<"version">>, <<"0.17.0">>}.
3 + {<<"version">>, <<"0.18.0">>}.
4 4 {<<"description">>, <<"A bson encoder and decoder written in gleam">>}.
5 5 {<<"licenses">>, [<<"Apache-2.0">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -10,15 +10,15 @@
10 10 {<<"Repository">>, <<"https://github.com/massivefermion/gleam_bson">>}
11 11 ]}.
12 12 {<<"requirements">>, [
13 - {<<"gleam_stdlib">>, [
14 - {<<"app">>, <<"gleam_stdlib">>},
15 - {<<"optional">>, false},
16 - {<<"requirement">>, <<"~> 0.24">>}
17 - ]},
18 13 {<<"gleam_crypto">>, [
19 14 {<<"app">>, <<"gleam_crypto">>},
20 15 {<<"optional">>, false},
21 16 {<<"requirement">>, <<"~> 0.3">>}
17 + ]},
18 + {<<"gleam_stdlib">>, [
19 + {<<"app">>, <<"gleam_stdlib">>},
20 + {<<"optional">>, false},
21 + {<<"requirement">>, <<"~> 0.25">>}
22 22 ]}
23 23 ]}.
24 24 {<<"files">>, [
  @@ -3,6 +3,7 @@ import gleam/int
3 3 import bson/uuid
4 4 import gleam/pair
5 5 import gleam/list
6 + import gleam/queue
6 7 import bson/custom
7 8 import bson/generic
8 9 import bson/object_id
  @@ -29,7 +30,7 @@ fn decode_document(data: BitString) -> Result(types.Value, Nil) {
29 30 case total_size == given_size {
30 31 True -> {
31 32 try body = bit_string.slice(rest, 0, total_size - 4 - 1)
32 - try body = decode_body(body, [])
33 + try body = decode_body(body, queue.new())
33 34 Ok(types.Document(body))
34 35 }
35 36 False -> Error(Nil)
  @@ -41,10 +42,14 @@ fn decode_document(data: BitString) -> Result(types.Value, Nil) {
41 42
42 43 fn decode_body(
43 44 data: BitString,
44 - storage: List(#(String, types.Value)),
45 + storage: queue.Queue(#(String, types.Value)),
45 46 ) -> Result(List(#(String, types.Value)), Nil) {
46 47 case bit_string.byte_size(data) {
47 - 0 -> Ok(storage)
48 + 0 ->
49 + storage
50 + |> queue.to_list
51 + |> Ok
52 +
48 53 _ -> {
49 54 let <<code:8, data:bit_string>> = data
50 55 let total_size = bit_string.byte_size(data)
  @@ -62,42 +67,38 @@ fn decode_body(
62 67 case sub_kind {
63 68 sub_kind if sub_kind == generic_kind -> {
64 69 try value = generic.from_bit_string(value)
65 - decode_body(
70 + recurse_with_new_kv(
66 71 rest,
67 - storage
68 - |> list.reverse
69 - |> list.prepend(#(key, types.Binary(types.Generic(value))))
70 - |> list.reverse,
72 + storage,
73 + key,
74 + types.Binary(types.Generic(value)),
71 75 )
72 76 }
73 77 sub_kind if sub_kind == md5_kind -> {
74 78 try value = md5.from_bit_string(value)
75 - decode_body(
79 + recurse_with_new_kv(
76 80 rest,
77 - storage
78 - |> list.reverse
79 - |> list.prepend(#(key, types.Binary(types.MD5(value))))
80 - |> list.reverse,
81 + storage,
82 + key,
83 + types.Binary(types.MD5(value)),
81 84 )
82 85 }
83 86 sub_kind if sub_kind == uuid_kind -> {
84 87 try value = uuid.from_bit_string(value)
85 - decode_body(
88 + recurse_with_new_kv(
86 89 rest,
87 - storage
88 - |> list.reverse
89 - |> list.prepend(#(key, types.Binary(types.UUID(value))))
90 - |> list.reverse,
90 + storage,
91 + key,
92 + types.Binary(types.UUID(value)),
91 93 )
92 94 }
93 95 _ if sub_code >= 0x80 -> {
94 96 try value = custom.from_bit_string_with_code(sub_code, value)
95 - decode_body(
97 + recurse_with_new_kv(
96 98 rest,
97 - storage
98 - |> list.reverse
99 - |> list.prepend(#(key, types.Binary(types.Custom(value))))
100 - |> list.reverse,
99 + storage,
100 + key,
101 + types.Binary(types.Custom(value)),
101 102 )
102 103 }
103 104 _ -> Error(Nil)
  @@ -105,110 +106,39 @@ fn decode_body(
105 106 }
106 107 kind if kind == double -> {
107 108 let <<value:little-float, rest:bit_string>> = rest
108 - decode_body(
109 - rest,
110 - storage
111 - |> list.reverse
112 - |> list.prepend(#(key, types.Double(value)))
113 - |> list.reverse,
114 - )
109 + recurse_with_new_kv(rest, storage, key, types.Double(value))
115 110 }
116 111 kind if kind == object_id_kind -> {
117 112 let <<value:96-bit_string, rest:bit_string>> = rest
118 113 try oid = object_id.from_bit_string(value)
119 - decode_body(
120 - rest,
121 - storage
122 - |> list.reverse
123 - |> list.prepend(#(key, types.ObjectId(oid)))
124 - |> list.reverse,
125 - )
114 + recurse_with_new_kv(rest, storage, key, types.ObjectId(oid))
126 115 }
127 116 kind if kind == boolean -> {
128 117 let <<value:8, rest:bit_string>> = rest
129 - case value {
130 - 1 ->
131 - decode_body(
132 - rest,
133 - storage
134 - |> list.reverse
135 - |> list.prepend(#(key, types.Boolean(True)))
136 - |> list.reverse,
137 - )
138 - 0 ->
139 - decode_body(
140 - rest,
141 - storage
142 - |> list.reverse
143 - |> list.prepend(#(key, types.Boolean(False)))
144 - |> list.reverse,
145 - )
146 - _ -> Error(Nil)
147 - }
118 + use value <- decode_boolean(value)
119 + recurse_with_new_kv(rest, storage, key, types.Boolean(value))
148 120 }
149 121 kind if kind == null ->
150 - decode_body(
151 - rest,
152 - storage
153 - |> list.reverse
154 - |> list.prepend(#(key, types.Null))
155 - |> list.reverse,
156 - )
122 + recurse_with_new_kv(rest, storage, key, types.Null)
157 123 kind if kind == min ->
158 - decode_body(
159 - rest,
160 - storage
161 - |> list.reverse
162 - |> list.prepend(#(key, types.Min))
163 - |> list.reverse,
164 - )
124 + recurse_with_new_kv(rest, storage, key, types.Min)
165 125 kind if kind == max ->
166 - decode_body(
167 - rest,
168 - storage
169 - |> list.reverse
170 - |> list.prepend(#(key, types.Max))
171 - |> list.reverse,
172 - )
126 + recurse_with_new_kv(rest, storage, key, types.Max)
173 127 kind if kind == int32 -> {
174 128 let <<value:32-little, rest:bit_string>> = rest
175 - decode_body(
176 - rest,
177 - storage
178 - |> list.reverse
179 - |> list.prepend(#(key, types.Integer(value)))
180 - |> list.reverse,
181 - )
129 + recurse_with_new_kv(rest, storage, key, types.Integer(value))
182 130 }
183 131 kind if kind == int64 -> {
184 132 let <<value:64-little, rest:bit_string>> = rest
185 - decode_body(
186 - rest,
187 - storage
188 - |> list.reverse
189 - |> list.prepend(#(key, types.Integer(value)))
190 - |> list.reverse,
191 - )
133 + recurse_with_new_kv(rest, storage, key, types.Integer(value))
192 134 }
193 135 kind if kind == datetime -> {
194 136 let <<value:64-little, rest:bit_string>> = rest
195 - decode_body(
196 - rest,
197 - storage
198 - |> list.reverse
199 - |> list.prepend(#(key, types.DateTime(value)))
200 - |> list.reverse,
201 - )
137 + recurse_with_new_kv(rest, storage, key, types.DateTime(value))
202 138 }
203 139 kind if kind == timestamp -> {
204 140 let <<value:64-little-unsigned, rest:bit_string>> = rest
205 - decode_body(
206 - rest,
207 - storage
208 - |> list.reverse
209 - |> list.prepend(#(key, types.Timestamp(value)))
210 - |> list.reverse,
211 - )
141 + recurse_with_new_kv(rest, storage, key, types.Timestamp(value))
212 142 }
213 143 kind if kind == regex -> {
214 144 try pattern_bytes = consume_till_zero(rest, <<>>)
  @@ -219,12 +149,11 @@ fn decode_body(
219 149 let <<_:size(options_size), rest:bit_string>> = rest
220 150 try pattern = bit_string.to_string(pattern_bytes)
221 151 try options = bit_string.to_string(options_bytes)
222 - decode_body(
152 + recurse_with_new_kv(
223 153 rest,
224 - storage
225 - |> list.reverse
226 - |> list.prepend(#(key, types.Regex(#(pattern, options))))
227 - |> list.reverse,
154 + storage,
155 + key,
156 + types.Regex(#(pattern, options)),
228 157 )
229 158 }
230 159 kind if kind == string -> {
  @@ -240,13 +169,7 @@ fn decode_body(
240 169 str_size + 1,
241 170 bit_string.byte_size(rest) - str_size - 1,
242 171 )
243 - decode_body(
244 - rest,
245 - storage
246 - |> list.reverse
247 - |> list.prepend(#(key, types.Str(str)))
248 - |> list.reverse,
249 - )
172 + recurse_with_new_kv(rest, storage, key, types.Str(str))
250 173 }
251 174 False -> Error(Nil)
252 175 }
  @@ -264,13 +187,7 @@ fn decode_body(
264 187 str_size + 1,
265 188 bit_string.byte_size(rest) - str_size - 1,
266 189 )
267 - decode_body(
268 - rest,
269 - storage
270 - |> list.reverse
271 - |> list.prepend(#(key, types.JS(str)))
272 - |> list.reverse,
273 - )
190 + recurse_with_new_kv(rest, storage, key, types.JS(str))
274 191 }
275 192 False -> Error(Nil)
276 193 }
  @@ -286,53 +203,27 @@ fn decode_body(
286 203 list.try_map(
287 204 doc,
288 205 fn(item) {
289 - try first =
290 - item
291 - |> pair.first
292 - |> int.parse
293 - Ok(#(first, pair.second(item)))
206 + try first = int.parse(item.0)
207 + Ok(#(first, item.1))
294 208 },
295 209 )
296 210 types.Array(
297 - list.sort(
298 - doc,
299 - fn(a, b) {
300 - let a_index = pair.first(a)
301 - let b_index = pair.first(b)
302 - int.compare(a_index, b_index)
303 - },
304 - )
211 + list.sort(doc, fn(a, b) { int.compare(a.0, b.0) })
305 212 |> list.map(pair.second),
306 213 )
307 214 |> Ok
308 215 }
309 216 _ -> Error(Nil)
310 217 }
311 - case doc_size == bit_string.byte_size(rest) {
312 - True ->
313 - storage
314 - |> list.reverse
315 - |> list.prepend(#(key, doc))
316 - |> list.reverse
317 - |> Ok
318 - False ->
319 - case
320 - bit_string.slice(
321 - rest,
322 - doc_size,
323 - bit_string.byte_size(rest) - doc_size,
324 - )
325 - {
326 - Ok(rest) ->
327 - decode_body(
328 - rest,
329 - storage
330 - |> list.reverse
331 - |> list.prepend(#(key, doc))
332 - |> list.reverse,
333 - )
334 - Error(Nil) -> Error(Nil)
335 - }
218 + case
219 + bit_string.slice(
220 + rest,
221 + doc_size,
222 + bit_string.byte_size(rest) - doc_size,
223 + )
224 + {
225 + Ok(rest) -> recurse_with_new_kv(rest, storage, key, doc)
226 + Error(Nil) -> Error(Nil)
336 227 }
337 228 }
338 229 _ -> Error(Nil)
  @@ -356,3 +247,15 @@ fn consume_till_zero(
356 247 }
357 248 }
358 249 }
250 +
251 + fn recurse_with_new_kv(rest, storage, key, value) {
252 + decode_body(rest, queue.push_back(storage, #(key, value)))
253 + }
254 +
255 + fn decode_boolean(value, rest) {
256 + case value {
257 + 0 -> rest(False)
258 + 1 -> rest(True)
259 + _ -> Error(Nil)
260 + }
261 + }
  @@ -26,7 +26,7 @@ decode_document(Data) ->
26 26 case gleam@bit_string:slice(Rest, 0, (Total_size - 4) - 1) of
27 27 {error, _try} -> {error, _try};
28 28 {ok, Body} ->
29 - case decode_body(Body, []) of
29 + case decode_body(Body, gleam@queue:new()) of
30 30 {error, _try@1} -> {error, _try@1};
31 31 {ok, Body@1} ->
32 32 {ok, {document, Body@1}}
  @@ -41,14 +41,16 @@ decode_document(Data) ->
41 41 {error, nil}
42 42 end.
43 43
44 - -spec decode_body(bitstring(), list({binary(), bson@types:value()})) -> {ok,
45 - list({binary(),
46 - bson@types:value()})} |
47 - {error, nil}.
44 + -spec decode_body(
45 + bitstring(),
46 + gleam@queue:queue({binary(), bson@types:value()})
47 + ) -> {ok, list({binary(), bson@types:value()})} | {error, nil}.
48 48 decode_body(Data, Storage) ->
49 49 case gleam@bit_string:byte_size(Data) of
50 50 0 ->
51 - {ok, Storage};
51 + _pipe = Storage,
52 + _pipe@1 = gleam@queue:to_list(_pipe),
53 + {ok, _pipe@1};
52 54
53 55 _@1 ->
54 56 <<Code:8, Data@1/bitstring>> = Data,
  @@ -74,95 +76,61 @@ decode_body(Data, Storage) ->
74 76 case Kind of
75 77 Kind@1 when Kind@1 =:= {kind, <<16#05>>} ->
76 78 <<Byte_size:32/little-integer,
77 - Sub_code:8,
78 - Rest@1/bitstring>> = Rest,
79 - Given_size = Byte_size
80 - * 8,
79 + Sub_code:8,
80 + Rest@1/bitstring>> = Rest,
81 + Given_size = Byte_size * 8,
81 82 <<Value:Given_size/bitstring,
82 - Rest@2/bitstring>> = Rest@1,
83 + Rest@2/bitstring>> = Rest@1,
83 84 Sub_kind = {sub_kind, <<Sub_code>>},
84 85 case Sub_kind of
85 86 Sub_kind@1 when Sub_kind@1 =:= {sub_kind,
86 - <<16#0>>} ->
87 + <<16#0>>} ->
87 88 case bson@generic:from_bit_string(
88 89 Value
89 90 ) of
90 91 {error, _try@3} -> {error, _try@3};
91 92 {ok, Value@1} ->
92 - decode_body(
93 + recurse_with_new_kv(
93 94 Rest@2,
94 - begin
95 - _pipe = Storage,
96 - _pipe@1 = gleam@list:reverse(
97 - _pipe
98 - ),
99 - _pipe@2 = gleam@list:prepend(
100 - _pipe@1,
101 - {Key@1,
102 - {binary,
103 - {generic,
104 - Value@1}}}
105 - ),
106 - gleam@list:reverse(
107 - _pipe@2
108 - )
109 - end
95 + Storage,
96 + Key@1,
97 + {binary,
98 + {generic,
99 + Value@1}}
110 100 )
111 101 end;
112 102
113 103 Sub_kind@2 when Sub_kind@2 =:= {sub_kind,
114 - <<16#5>>} ->
104 + <<16#5>>} ->
115 105 case bson@md5:from_bit_string(
116 106 Value
117 107 ) of
118 108 {error, _try@4} -> {error, _try@4};
119 109 {ok, Value@2} ->
120 - decode_body(
110 + recurse_with_new_kv(
121 111 Rest@2,
122 - begin
123 - _pipe@3 = Storage,
124 - _pipe@4 = gleam@list:reverse(
125 - _pipe@3
126 - ),
127 - _pipe@5 = gleam@list:prepend(
128 - _pipe@4,
129 - {Key@1,
130 - {binary,
131 - {md5,
132 - Value@2}}}
133 - ),
134 - gleam@list:reverse(
135 - _pipe@5
136 - )
137 - end
112 + Storage,
113 + Key@1,
114 + {binary,
115 + {md5,
116 + Value@2}}
138 117 )
139 118 end;
140 119
141 120 Sub_kind@3 when Sub_kind@3 =:= {sub_kind,
142 - <<16#4>>} ->
121 + <<16#4>>} ->
143 122 case bson@uuid:from_bit_string(
144 123 Value
145 124 ) of
146 125 {error, _try@5} -> {error, _try@5};
147 126 {ok, Value@3} ->
148 - decode_body(
127 + recurse_with_new_kv(
149 128 Rest@2,
150 - begin
151 - _pipe@6 = Storage,
152 - _pipe@7 = gleam@list:reverse(
153 - _pipe@6
154 - ),
155 - _pipe@8 = gleam@list:prepend(
156 - _pipe@7,
157 - {Key@1,
158 - {binary,
159 - {uuid,
160 - Value@3}}}
161 - ),
162 - gleam@list:reverse(
163 - _pipe@8
164 - )
165 - end
129 + Storage,
130 + Key@1,
131 + {binary,
132 + {uuid,
133 + Value@3}}
166 134 )
167 135 end;
168 136
  @@ -173,24 +141,13 @@ decode_body(Data, Storage) ->
173 141 ) of
174 142 {error, _try@6} -> {error, _try@6};
175 143 {ok, Value@4} ->
176 - decode_body(
144 + recurse_with_new_kv(
177 145 Rest@2,
178 - begin
179 - _pipe@9 = Storage,
180 - _pipe@10 = gleam@list:reverse(
181 - _pipe@9
182 - ),
183 - _pipe@11 = gleam@list:prepend(
184 - _pipe@10,
185 - {Key@1,
186 - {binary,
187 - {custom,
188 - Value@4}}}
189 - ),
190 - gleam@list:reverse(
191 - _pipe@11
192 - )
193 - end
146 + Storage,
147 + Key@1,
148 + {binary,
149 + {custom,
150 + Value@4}}
194 151 )
195 152 end;
196 153
  @@ -200,225 +157,112 @@ decode_body(Data, Storage) ->
200 157
201 158 Kind@2 when Kind@2 =:= {kind, <<16#01>>} ->
202 159 <<Value@5/little-float,
203 - Rest@3/bitstring>> = Rest,
204 - decode_body(
160 + Rest@3/bitstring>> = Rest,
161 + recurse_with_new_kv(
205 162 Rest@3,
206 - begin
207 - _pipe@12 = Storage,
208 - _pipe@13 = gleam@list:reverse(
209 - _pipe@12
210 - ),
211 - _pipe@14 = gleam@list:prepend(
212 - _pipe@13,
213 - {Key@1,
214 - {double, Value@5}}
215 - ),
216 - gleam@list:reverse(_pipe@14)
217 - end
163 + Storage,
164 + Key@1,
165 + {double, Value@5}
218 166 );
219 167
220 168 Kind@3 when Kind@3 =:= {kind, <<16#07>>} ->
221 169 <<Value@6:96/bitstring,
222 - Rest@4/bitstring>> = Rest,
170 + Rest@4/bitstring>> = Rest,
223 171 case bson@object_id:from_bit_string(
224 172 Value@6
225 173 ) of
226 174 {error, _try@7} -> {error, _try@7};
227 175 {ok, Oid} ->
228 - decode_body(
176 + recurse_with_new_kv(
229 177 Rest@4,
230 - begin
231 - _pipe@15 = Storage,
232 - _pipe@16 = gleam@list:reverse(
233 - _pipe@15
234 - ),
235 - _pipe@17 = gleam@list:prepend(
236 - _pipe@16,
237 - {Key@1,
238 - {object_id,
239 - Oid}}
240 - ),
241 - gleam@list:reverse(
242 - _pipe@17
243 - )
244 - end
178 + Storage,
179 + Key@1,
180 + {object_id, Oid}
245 181 )
246 182 end;
247 183
248 184 Kind@4 when Kind@4 =:= {kind, <<16#08>>} ->
249 185 <<Value@7:8, Rest@5/bitstring>> = Rest,
250 - case Value@7 of
251 - 1 ->
252 - decode_body(
186 + decode_boolean(
187 + Value@7,
188 + fun(Value@8) ->
189 + recurse_with_new_kv(
253 190 Rest@5,
254 - begin
255 - _pipe@18 = Storage,
256 - _pipe@19 = gleam@list:reverse(
257 - _pipe@18
258 - ),
259 - _pipe@20 = gleam@list:prepend(
260 - _pipe@19,
261 - {Key@1,
262 - {boolean, true}}
263 - ),
264 - gleam@list:reverse(
265 - _pipe@20
266 - )
267 - end
268 - );
269 -
270 - 0 ->
271 - decode_body(
272 - Rest@5,
273 - begin
274 - _pipe@21 = Storage,
275 - _pipe@22 = gleam@list:reverse(
276 - _pipe@21
277 - ),
278 - _pipe@23 = gleam@list:prepend(
279 - _pipe@22,
280 - {Key@1,
281 - {boolean,
282 - false}}
283 - ),
284 - gleam@list:reverse(
285 - _pipe@23
286 - )
287 - end
288 - );
289 -
290 - _@4 ->
291 - {error, nil}
292 - end;
191 + Storage,
192 + Key@1,
193 + {boolean, Value@8}
194 + )
195 + end
196 + );
293 197
294 198 Kind@5 when Kind@5 =:= {kind, <<16#0A>>} ->
295 - decode_body(
199 + recurse_with_new_kv(
296 200 Rest,
297 - begin
298 - _pipe@24 = Storage,
299 - _pipe@25 = gleam@list:reverse(
300 - _pipe@24
301 - ),
302 - _pipe@26 = gleam@list:prepend(
303 - _pipe@25,
304 - {Key@1, null}
305 - ),
306 - gleam@list:reverse(_pipe@26)
307 - end
201 + Storage,
202 + Key@1,
203 + null
308 204 );
309 205
310 206 Kind@6 when Kind@6 =:= {kind, <<16#FF>>} ->
311 - decode_body(
207 + recurse_with_new_kv(
312 208 Rest,
313 - begin
314 - _pipe@27 = Storage,
315 - _pipe@28 = gleam@list:reverse(
316 - _pipe@27
317 - ),
318 - _pipe@29 = gleam@list:prepend(
319 - _pipe@28,
320 - {Key@1, min}
321 - ),
322 - gleam@list:reverse(_pipe@29)
323 - end
209 + Storage,
210 + Key@1,
211 + min
324 212 );
325 213
326 214 Kind@7 when Kind@7 =:= {kind, <<16#7F>>} ->
327 - decode_body(
215 + recurse_with_new_kv(
328 216 Rest,
329 - begin
330 - _pipe@30 = Storage,
331 - _pipe@31 = gleam@list:reverse(
332 - _pipe@30
333 - ),
334 - _pipe@32 = gleam@list:prepend(
335 - _pipe@31,
336 - {Key@1, max}
337 - ),
338 - gleam@list:reverse(_pipe@32)
339 - end
217 + Storage,
218 + Key@1,
219 + max
340 220 );
341 221
342 222 Kind@8 when Kind@8 =:= {kind, <<16#10>>} ->
343 - <<Value@8:32/little,
344 - Rest@6/bitstring>> = Rest,
345 - decode_body(
223 + <<Value@9:32/little,
224 + Rest@6/bitstring>> = Rest,
225 + recurse_with_new_kv(
346 226 Rest@6,
347 - begin
348 - _pipe@33 = Storage,
349 - _pipe@34 = gleam@list:reverse(
350 - _pipe@33
351 - ),
352 - _pipe@35 = gleam@list:prepend(
353 - _pipe@34,
354 - {Key@1,
355 - {integer, Value@8}}
356 - ),
357 - gleam@list:reverse(_pipe@35)
358 - end
227 + Storage,
228 + Key@1,
229 + {integer, Value@9}
359 230 );
360 231
361 232 Kind@9 when Kind@9 =:= {kind, <<16#12>>} ->
362 - <<Value@9:64/little,
363 - Rest@7/bitstring>> = Rest,
364 - decode_body(
233 + <<Value@10:64/little,
234 + Rest@7/bitstring>> = Rest,
235 + recurse_with_new_kv(
365 236 Rest@7,
366 - begin
367 - _pipe@36 = Storage,
368 - _pipe@37 = gleam@list:reverse(
369 - _pipe@36
370 - ),
371 - _pipe@38 = gleam@list:prepend(
372 - _pipe@37,
373 - {Key@1,
374 - {integer, Value@9}}
375 - ),
376 - gleam@list:reverse(_pipe@38)
377 - end
237 + Storage,
238 + Key@1,
239 + {integer, Value@10}
378 240 );
379 241
380 242 Kind@10 when Kind@10 =:= {kind,
381 - <<16#09>>} ->
382 - <<Value@10:64/little,
383 - Rest@8/bitstring>> = Rest,
384 - decode_body(
243 + <<16#09>>} ->
244 + <<Value@11:64/little,
245 + Rest@8/bitstring>> = Rest,
246 + recurse_with_new_kv(
385 247 Rest@8,
386 - begin
387 - _pipe@39 = Storage,
388 - _pipe@40 = gleam@list:reverse(
389 - _pipe@39
390 - ),
391 - _pipe@41 = gleam@list:prepend(
392 - _pipe@40,
393 - {Key@1,
394 - {date_time, Value@10}}
395 - ),
396 - gleam@list:reverse(_pipe@41)
397 - end
248 + Storage,
249 + Key@1,
250 + {date_time, Value@11}
398 251 );
399 252
400 253 Kind@11 when Kind@11 =:= {kind,
401 - <<16#11>>} ->
402 - <<Value@11:64/little-unsigned,
403 - Rest@9/bitstring>> = Rest,
404 - decode_body(
254 + <<16#11>>} ->
255 + <<Value@12:64/little-unsigned,
256 + Rest@9/bitstring>> = Rest,
257 + recurse_with_new_kv(
405 258 Rest@9,
406 - begin
407 - _pipe@42 = Storage,
408 - _pipe@43 = gleam@list:reverse(
409 - _pipe@42
410 - ),
411 - _pipe@44 = gleam@list:prepend(
412 - _pipe@43,
413 - {Key@1,
414 - {timestamp, Value@11}}
415 - ),
416 - gleam@list:reverse(_pipe@44)
417 - end
259 + Storage,
260 + Key@1,
261 + {timestamp, Value@12}
418 262 );
419 263
420 264 Kind@12 when Kind@12 =:= {kind,
421 - <<16#0B>>} ->
265 + <<16#0B>>} ->
422 266 case consume_till_zero(Rest, <<>>) of
423 267 {error, _try@8} -> {error, _try@8};
424 268 {ok, Pattern_bytes} ->
  @@ -427,8 +271,8 @@ decode_body(Data, Storage) ->
427 271 )
428 272 + 1)
429 273 * 8,
430 - <<_@5:Pattern_size,
431 - Rest@10/bitstring>> = Rest,
274 + <<_@4:Pattern_size,
275 + Rest@10/bitstring>> = Rest,
432 276 case consume_till_zero(
433 277 Rest@10,
434 278 <<>>
  @@ -440,8 +284,8 @@ decode_body(Data, Storage) ->
440 284 )
441 285 + 1)
442 286 * 8,
443 - <<_@6:Options_size,
444 - Rest@11/bitstring>> = Rest@10,
287 + <<_@5:Options_size,
288 + Rest@11/bitstring>> = Rest@10,
445 289 case gleam@bit_string:to_string(
446 290 Pattern_bytes
447 291 ) of
  @@ -452,24 +296,13 @@ decode_body(Data, Storage) ->
452 296 ) of
453 297 {error, _try@11} -> {error, _try@11};
454 298 {ok, Options} ->
455 - decode_body(
299 + recurse_with_new_kv(
456 300 Rest@11,
457 - begin
458 - _pipe@45 = Storage,
459 - _pipe@46 = gleam@list:reverse(
460 - _pipe@45
461 - ),
462 - _pipe@47 = gleam@list:prepend(
463 - _pipe@46,
464 - {Key@1,
465 - {regex,
466 - {Pattern,
467 - Options}}}
468 - ),
469 - gleam@list:reverse(
470 - _pipe@47
471 - )
472 - end
301 + Storage,
302 + Key@1,
303 + {regex,
304 + {Pattern,
305 + Options}}
473 306 )
474 307 end
475 308 end
  @@ -477,9 +310,9 @@ decode_body(Data, Storage) ->
477 310 end;
478 311
479 312 Kind@13 when Kind@13 =:= {kind,
480 - <<16#02>>} ->
313 + <<16#02>>} ->
481 314 <<Given_size@1:32/little-integer,
482 - Rest@12/bitstring>> = Rest,
315 + Rest@12/bitstring>> = Rest,
483 316 case consume_till_zero(
484 317 Rest@12,
485 318 <<>>
  @@ -510,23 +343,12 @@ decode_body(Data, Storage) ->
510 343 ) of
511 344 {error, _try@14} -> {error, _try@14};
512 345 {ok, Rest@13} ->
513 - decode_body(
346 + recurse_with_new_kv(
514 347 Rest@13,
515 - begin
516 - _pipe@48 = Storage,
517 - _pipe@49 = gleam@list:reverse(
518 - _pipe@48
519 - ),
520 - _pipe@50 = gleam@list:prepend(
521 - _pipe@49,
522 - {Key@1,
523 - {str,
524 - Str@1}}
525 - ),
526 - gleam@list:reverse(
527 - _pipe@50
528 - )
529 - end
348 + Storage,
349 + Key@1,
350 + {str,
351 + Str@1}
530 352 )
531 353 end
532 354 end;
  @@ -537,9 +359,9 @@ decode_body(Data, Storage) ->
537 359 end;
538 360
539 361 Kind@14 when Kind@14 =:= {kind,
540 - <<16#0D>>} ->
362 + <<16#0D>>} ->
541 363 <<Given_size@2:32/little-integer,
542 - Rest@14/bitstring>> = Rest,
364 + Rest@14/bitstring>> = Rest,
543 365 case consume_till_zero(
544 366 Rest@14,
545 367 <<>>
  @@ -570,23 +392,12 @@ decode_body(Data, Storage) ->
570 392 ) of
571 393 {error, _try@17} -> {error, _try@17};
572 394 {ok, Rest@15} ->
573 - decode_body(
395 + recurse_with_new_kv(
574 396 Rest@15,
575 - begin
576 - _pipe@51 = Storage,
577 - _pipe@52 = gleam@list:reverse(
578 - _pipe@51
579 - ),
580 - _pipe@53 = gleam@list:prepend(
581 - _pipe@52,
582 - {Key@1,
583 - {js,
584 - Str@3}}
585 - ),
586 - gleam@list:reverse(
587 - _pipe@53
588 - )
589 - end
397 + Storage,
398 + Key@1,
399 + {js,
400 + Str@3}
590 401 )
591 402 end
592 403 end;
  @@ -597,10 +408,10 @@ decode_body(Data, Storage) ->
597 408 end;
598 409
599 410 Kind@15 when (Kind@15 =:= {kind,
600 - <<16#03>>}) orelse (Kind@15 =:= {kind,
601 - <<16#04>>}) ->
411 + <<16#03>>}) orelse (Kind@15 =:= {kind,
412 + <<16#04>>}) ->
602 413 <<Doc_size:32/little-integer,
603 - _@7/bitstring>> = Rest,
414 + _@6/bitstring>> = Rest,
604 415 case gleam@bit_string:slice(
605 416 Rest,
606 417 0,
  @@ -613,133 +424,98 @@ decode_body(Data, Storage) ->
613 424 {ok, {document, Doc@1}} ->
614 425 case case Kind@15 of
615 426 Kind@16 when Kind@16 =:= {kind,
616 - <<16#03>>} ->
427 + <<16#03>>} ->
617 428 {ok,
618 - {document,
619 - Doc@1}};
429 + {document,
430 + Doc@1}};
620 431
621 432 Kind@17 when Kind@17 =:= {kind,
622 - <<16#04>>} ->
433 + <<16#04>>} ->
623 434 case gleam@list:try_map(
624 435 Doc@1,
625 436 fun(
626 437 Item
627 438 ) ->
628 - case begin
629 - _pipe@54 = Item,
630 - _pipe@55 = gleam@pair:first(
631 - _pipe@54
632 - ),
633 - gleam@int:parse(
634 - _pipe@55
439 + case gleam@int:parse(
440 + erlang:element(
441 + 1,
442 + Item
635 443 )
636 - end of
444 + ) of
637 445 {error, _try@20} -> {error, _try@20};
638 446 {ok, First} ->
639 447 {ok,
640 - {First,
641 - gleam@pair:second(
642 - Item
643 - )}}
448 + {First,
449 + erlang:element(
450 + 2,
451 + Item
452 + )}}
644 453 end
645 454 end
646 455 ) of
647 456 {error, _try@21} -> {error, _try@21};
648 457 {ok, Doc@2} ->
649 - _pipe@57 = {array,
650 - begin
651 - _pipe@56 = gleam@list:sort(
652 - Doc@2,
653 - fun(
654 - A,
458 + _pipe@3 = {array,
459 + begin
460 + _pipe@2 = gleam@list:sort(
461 + Doc@2,
462 + fun(
463 + A,
464 + B
465 + ) ->
466 + gleam@int:compare(
467 + erlang:element(
468 + 1,
469 + A
470 + ),
471 + erlang:element(
472 + 1,
655 473 B
656 - ) ->
657 - A_index = gleam@pair:first(
658 - A
659 - ),
660 - B_index = gleam@pair:first(
661 - B
662 - ),
663 - gleam@int:compare(
664 - A_index,
665 - B_index
666 - )
667 - end
668 - ),
669 - gleam@list:map(
670 - _pipe@56,
671 - fun gleam@pair:second/1
474 + )
672 475 )
673 - end},
476 + end
477 + ),
478 + gleam@list:map(
479 + _pipe@2,
480 + fun gleam@pair:second/1
481 + )
482 + end},
674 483 {ok,
675 - _pipe@57}
484 + _pipe@3}
676 485 end;
677 486
678 - _@8 ->
487 + _@7 ->
679 488 {error, nil}
680 489 end of
681 490 {error, _try@22} -> {error, _try@22};
682 491 {ok, Doc@3} ->
683 - case Doc_size
684 - =:= gleam@bit_string:byte_size(
685 - Rest
492 + case gleam@bit_string:slice(
493 + Rest,
494 + Doc_size,
495 + gleam@bit_string:byte_size(
496 + Rest
497 + )
498 + - Doc_size
686 499 ) of
687 - true ->
688 - _pipe@58 = Storage,
689 - _pipe@59 = gleam@list:reverse(
690 - _pipe@58
691 - ),
692 - _pipe@60 = gleam@list:prepend(
693 - _pipe@59,
694 - {Key@1,
695 - Doc@3}
696 - ),
697 - _pipe@61 = gleam@list:reverse(
698 - _pipe@60
699 - ),
700 - {ok,
701 - _pipe@61};
500 + {ok,
501 + Rest@16} ->
502 + recurse_with_new_kv(
503 + Rest@16,
504 + Storage,
505 + Key@1,
506 + Doc@3
507 + );
702 508
703 - false ->
704 - case gleam@bit_string:slice(
705 - Rest,
706 - Doc_size,
707 - gleam@bit_string:byte_size(
708 - Rest
709 - )
710 - - Doc_size
711 - ) of
712 - {ok,
713 - Rest@16} ->
714 - decode_body(
715 - Rest@16,
716 - begin
717 - _pipe@62 = Storage,
718 - _pipe@63 = gleam@list:reverse(
719 - _pipe@62
720 - ),
721 - _pipe@64 = gleam@list:prepend(
722 - _pipe@63,
723 - {Key@1,
724 - Doc@3}
725 - ),
726 - gleam@list:reverse(
727 - _pipe@64
728 - )
729 - end
730 - );
731 -
732 - {error,
733 - nil} ->
734 - {error,
735 - nil}
736 - end
509 + {error,
510 + nil} ->
511 + {error,
512 + nil}
737 513 end
738 514 end
739 515 end
740 516 end;
741 517
742 - _@9 ->
518 + _@8 ->
743 519 {error, nil}
744 520 end
745 521 end
  @@ -767,3 +543,28 @@ consume_till_zero(Data, Storage) ->
767 543 )
768 544 end
769 545 end.
546 +
547 + -spec recurse_with_new_kv(
548 + bitstring(),
549 + gleam@queue:queue({binary(), bson@types:value()}),
550 + binary(),
551 + bson@types:value()
552 + ) -> {ok, list({binary(), bson@types:value()})} | {error, nil}.
553 + recurse_with_new_kv(Rest, Storage, Key, Value) ->
554 + decode_body(Rest, gleam@queue:push_back(Storage, {Key, Value})).
555 +
556 + -spec decode_boolean(
557 + integer(),
558 + fun((boolean()) -> {ok, list({binary(), bson@types:value()})} | {error, nil})
559 + ) -> {ok, list({binary(), bson@types:value()})} | {error, nil}.
560 + decode_boolean(Value, Rest) ->
561 + case Value of
562 + 0 ->
563 + Rest(false);
564 +
565 + 1 ->
566 + Rest(true);
567 +
568 + _@1 ->
569 + {error, nil}
570 + end.
  @@ -22,11 +22,11 @@ document(Doc) ->
22 22 end,
23 23 Size = gleam@bit_string:byte_size(Doc@1) + 5,
24 24 {entity,
25 - {kind, <<16#03>>},
26 - begin
27 - _pipe@2 = [<<Size:32/little>>, Doc@1, <<0>>],
28 - gleam@bit_string:concat(_pipe@2)
29 - end}.
25 + {kind, <<16#03>>},
26 + begin
27 + _pipe@2 = [<<Size:32/little>>, Doc@1, <<0>>],
28 + gleam@bit_string:concat(_pipe@2)
29 + end}.
30 30
31 31 -spec encode_kv({binary(), bson@types:value()}) -> bitstring().
32 32 encode_kv(Pair) ->
  @@ -168,56 +168,56 @@ md5(Value) ->
168 168 Value@1 = bson@md5:to_bit_string(Value),
169 169 Length = gleam@bit_string:byte_size(Value@1),
170 170 {entity,
171 - {kind, <<16#05>>},
172 - begin
173 - _pipe = [<<Length:32/little>>,
174 - erlang:element(2, {sub_kind, <<16#5>>}),
175 - Value@1],
176 - gleam@bit_string:concat(_pipe)
177 - end}.
171 + {kind, <<16#05>>},
172 + begin
173 + _pipe = [<<Length:32/little>>,
174 + erlang:element(2, {sub_kind, <<16#5>>}),
175 + Value@1],
176 + gleam@bit_string:concat(_pipe)
177 + end}.
178 178
179 179 -spec uuid(bson@uuid:uuid()) -> entity().
180 180 uuid(Value) ->
181 181 Value@1 = bson@uuid:to_bit_string(Value),
182 182 Length = gleam@bit_string:byte_size(Value@1),
183 183 {entity,
184 - {kind, <<16#05>>},
185 - begin
186 - _pipe = [<<Length:32/little>>,
187 - erlang:element(2, {sub_kind, <<16#4>>}),
188 - Value@1],
189 - gleam@bit_string:concat(_pipe)
190 - end}.
184 + {kind, <<16#05>>},
185 + begin
186 + _pipe = [<<Length:32/little>>,
187 + erlang:element(2, {sub_kind, <<16#4>>}),
188 + Value@1],
189 + gleam@bit_string:concat(_pipe)
190 + end}.
191 191
192 192 -spec custom(bson@custom:custom()) -> entity().
193 193 custom(Value) ->
194 194 {Code, Value@1} = bson@custom:to_bit_string_with_code(Value),
195 195 Length = gleam@bit_string:byte_size(Value@1),
196 196 {entity,
197 - {kind, <<16#05>>},
198 - begin
199 - _pipe = [<<Length:32/little>>, <<Code>>, Value@1],
200 - gleam@bit_string:concat(_pipe)
201 - end}.
197 + {kind, <<16#05>>},
198 + begin
199 + _pipe = [<<Length:32/little>>, <<Code>>, Value@1],
200 + gleam@bit_string:concat(_pipe)
201 + end}.
202 202
203 203 -spec generic(bson@generic:generic()) -> entity().
204 204 generic(Value) ->
205 205 Value@1 = bson@generic:to_bit_string(Value),
206 206 Length = gleam@bit_string:byte_size(Value@1),
207 207 {entity,
208 - {kind, <<16#05>>},
209 - begin
210 - _pipe = [<<Length:32/little>>,
211 - erlang:element(2, {sub_kind, <<16#0>>}),
212 - Value@1],
213 - gleam@bit_string:concat(_pipe)
214 - end}.
208 + {kind, <<16#05>>},
209 + begin
210 + _pipe = [<<Length:32/little>>,
211 + erlang:element(2, {sub_kind, <<16#0>>}),
212 + Value@1],
213 + gleam@bit_string:concat(_pipe)
214 + end}.
215 215
216 216 -spec regex(binary(), binary()) -> entity().
217 217 regex(Pattern, Options) ->
218 218 {entity,
219 - {kind, <<16#0B>>},
220 - begin
221 - _pipe = [<<Pattern/binary, 0, Options/binary, 0>>],
222 - gleam@bit_string:concat(_pipe)
223 - end}.
219 + {kind, <<16#0B>>},
220 + begin
221 + _pipe = [<<Pattern/binary, 0, Options/binary, 0>>],
222 + gleam@bit_string:concat(_pipe)
223 + end}.
Loading more files…