protospec-build 0.3.0

One binary format language to rule them all, One binary format language to find them, One binary format language to bring them all and in the darkness bind them.
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use super::*;
use crate::coder::decode::*;
use crate::{coder::*, map_async};

fn emit_target(target: &Target) -> TokenStream {
    match target {
        Target::Direct => quote! { reader },
        Target::Stream(x) => emit_register(*x),
        Target::Buf(x) => {
            let buf = emit_register(*x);
            quote! { (&mut #buf) }
        }
    }
}

fn prepare_decode(
    options: &CompileOptions,
    context: &Context,
    instructions: &[Instruction],
    is_async: bool,
    is_root: bool,
) -> TokenStream {
    let async_ = map_async(is_async);
    let mut statements = vec![];
    if is_root {
        if is_async {
            statements.push(quote! {
                use tokio::io::{ AsyncRead, AsyncBufRead, AsyncBufReadExt, AsyncReadExt };
            })
        } else {
            statements.push(quote! {
                use std::io::Read;
            })
        }
    }

    for instruction in instructions.iter() {
        if options.debug_mode {
            let raw = format!("decode {}: {:?}", context.name, instruction);
            statements.push(quote! {
                println!("{}", #raw);
            });
        }
        match instruction {
            Instruction::Eval(target, expr) => {
                let target = emit_register(*target);
                let value = emit_expression(expr, &|field| {
                    emit_register(
                        *context
                            .field_register_map
                            .get(&field.name)
                            .expect("missing register for field"),
                    )
                });
                statements.push(quote! {
                    let #target = #value;
                });
            }
            Instruction::Construct(target, Constructable::Tuple(items)) => {
                let target = emit_register(*target);
                let items = flatten(
                    items
                        .iter()
                        .map(|x| {
                            let x = emit_register(*x);
                            quote! {#x, }
                        })
                        .collect::<Vec<_>>(),
                );
                statements.push(quote! {
                    let #target = (#items);
                });
            }
            Instruction::Construct(target, Constructable::TaggedTuple { name, items }) => {
                let target = emit_register(*target);
                let items = flatten(
                    items
                        .iter()
                        .map(|x| {
                            let x = emit_register(*x);
                            quote! {#x, }
                        })
                        .collect::<Vec<_>>(),
                );
                let name = emit_ident(name);
                statements.push(quote! {
                    let #target = #name(#items);
                });
            }
            Instruction::Construct(target, Constructable::Struct { name, items }) => {
                let target = emit_register(*target);
                let items = flatten(
                    items
                        .iter()
                        .map(|(name, x)| {
                            let x = emit_register(*x);
                            let name = emit_ident(name);
                            quote! {#name: #x,}
                        })
                        .collect::<Vec<_>>(),
                );
                let name = emit_ident(name);
                statements.push(quote! {
                    let #target = #name { #items };
                });
            }
            Instruction::Construct(target, Constructable::TaggedEnum { name, discriminant, values }) => {
                let target = emit_register(*target);
                let items = flatten(
                    values
                        .iter()
                        .map(|x| {
                            let x = emit_register(*x);
                            quote! {#x, }
                        })
                        .collect::<Vec<_>>(),
                );
                let name = emit_ident(name);
                let discriminant = emit_ident(discriminant);
                statements.push(quote! {
                    let #target = #name::#discriminant(#items);
                });
            }
            Instruction::Construct(target, Constructable::TaggedEnumStruct { name, discriminant, values }) => {
                let target = emit_register(*target);
                let items = flatten(
                    values
                        .iter()
                        .map(|(name, x)| {
                            let x = emit_register(*x);
                            let name = emit_ident(name);
                            quote! {#name: #x,}
                        })
                        .collect::<Vec<_>>(),
                );
                let name = emit_ident(name);
                let discriminant = emit_ident(discriminant);
                statements.push(quote! {
                    let #target = #name::#discriminant { #items };
                });
            }
            Instruction::Constrict(stream, new_stream, len) => {
                let stream = emit_target(stream);
                let new_stream = emit_register(*new_stream);
                let len = emit_register(*len);
                statements.push(quote! {
                    let mut #new_stream = #stream.take(#len as u64);
                    let #new_stream = &mut #new_stream;
                });
            }
            Instruction::WrapStream(stream, new_stream, transformer, args) => {
                let new_stream_value = emit_register(*new_stream);
                let args = args.iter().map(|x| emit_register(*x)).collect::<Vec<_>>();
                let input = emit_target(stream);
                let transformed = transformer.inner.decoding_gen(input, args, is_async);
                statements.push(quote! {
                    let mut #new_stream_value = #transformed;
                    let #new_stream_value = &mut #new_stream_value;
                })
            }
            Instruction::ConditionalWrapStream(
                condition,
                prelude,
                stream,
                new_stream,
                transformer,
                args,
            ) => {
                let condition = emit_register(*condition);
                let new_stream_value = emit_register(*new_stream);
                let args = args.iter().map(|x| emit_register(*x)).collect::<Vec<_>>();
                let input = emit_target(stream);
                let transformed = transformer
                    .inner
                    .decoding_gen(input.clone(), args, is_async);
                let prelude = prepare_decode(options, context, &prelude[..], is_async, false);

                //todo: would be nicer to use generics here instead of trait object
                if is_async {
                    statements.push(quote! {
                        let mut r_xform;
                        let #new_stream_value: &mut dyn AsyncBufRead + Unpin + Send + Sync = if #condition {
                            #prelude
                            r_xform = #transformed;
                            &mut r_xform
                        } else {
                            #input as &mut dyn AsyncBufRead + Unpin + Send + Sync
                        };
                    })
                } else {
                    statements.push(quote! {
                        let mut r_xform;
                        let #new_stream_value: &mut dyn Read = if #condition {
                            #prelude
                            r_xform = #transformed;
                            &mut r_xform
                        } else {
                            #input as &mut dyn Read
                        };
                    })
                }
            }
            Instruction::DecodeForeign(target, data, type_ref, args) => {
                let target = emit_target(target);
                let data = emit_register(*data);
                let mut out_arguments = vec![];
                for argument in args {
                    let value = emit_register(*argument);
                    out_arguments.push(value);
                }

                statements.push(
                    type_ref
                        .obj
                        .decoding_gen(target, data, out_arguments, is_async),
                );
            }
            Instruction::DecodeRef(target, source, class, args) => {
                let mut out_arguments = vec![];
                for argument in args {
                    let value = emit_register(*argument);
                    out_arguments.push(quote! {, #value});
                }
                let out_arguments = flatten(out_arguments);
                let target = emit_target(target);
                let source = emit_register(*source);
                let class = emit_ident(class);
                if is_async {
                    statements.push(quote! {
                        let #source = #class::decode_async(#target #out_arguments).await?;
                    });
                } else {
                    statements.push(quote! {
                        let #source = #class::decode_sync(#target #out_arguments)?;
                    });
                }
            }
            Instruction::DecodeRepr(name, type_, value, target) => {
                let target = emit_target(target);
                let value = emit_register(*value);

                let enum_ident = format_ident!("{}", &name);
                let length = type_.size() as usize;

                statements.push(quote! {
                    let #value = {
                        let mut scratch = [0u8; #length];
                        #target.read_exact(&mut scratch[..])#async_?;
                        #enum_ident::from_repr(#type_::from_be_bytes((&scratch[..]).try_into()?))?
                    };
                });
            }
            Instruction::DecodePrimitive(target, data, PrimitiveType::Bool) => {
                let target = emit_target(target);
                let data = emit_register(*data);

                statements.push(quote! {
                    let #data = {
                        let mut scratch = [0u8; 1];
                        #target.read_exact(&mut scratch[..1])#async_?;
                        scratch[0] != 0
                    };
                });
            }
            Instruction::DecodePrimitive(target, data, type_) => {
                let target = emit_target(target);
                let data = emit_register(*data);
                let length = type_.size() as usize;

                statements.push(quote! {
                    let #data = {
                        let mut scratch = [0u8; #length];
                        #target.read_exact(&mut scratch[..])#async_?;
                        #type_::from_be_bytes((&scratch[..]).try_into()?)
                    };
                });
            }
            Instruction::DecodePrimitiveArray(target, data, type_, len) => {
                let target = emit_target(target);
                let data = emit_register(*data);
                if let Some(len) = len {
                    let len = emit_register(*len);
                    statements.push(quote! {
                        let #data = {
                            let t_count = #len as usize;
                            let size = mem::size_of::<#type_>();
                            let mut raw: Vec<u8> = Vec::with_capacity(t_count * size);
                            unsafe { raw.set_len(t_count * size) };
                            #target.read_exact(&mut raw[..])#async_?;
                            raw.chunks_exact(size).map(|x| #type_::from_be_bytes(x.try_into().unwrap())).collect()
                        };
                    });
                } else {
                    statements.push(quote! {
                        let #data = {
                            let mut raw: Vec<u8> = Vec::new();
                            #target.read_to_end(&mut raw)#async_?;
                            let size = mem::size_of::<#type_>();
                            raw.chunks_exact(size).map(|x| #type_::from_be_bytes(x.try_into().unwrap())).collect()
                        };
                    });
                }
            }
            Instruction::DecodeReprArray(target, data, name, type_, len) => {
                let target = emit_target(target);
                let data = emit_register(*data);
                let enum_ident = format_ident!("{}", &name);
                if let Some(len) = len {
                    let len = emit_register(*len);
                    statements.push(quote! {
                        let #data = {
                            let t_count = #len as usize;
                            let size = mem::size_of::<#type_>();
                            let mut raw: Vec<u8> = Vec::with_capacity(t_count * size);
                            unsafe { raw.set_len(t_count * size) };
                            #target.read_exact(&mut raw[..])#async_?;
                            raw.chunks_exact(size).map(|x| #enum_ident::from_repr(#type_::from_be_bytes(x.try_into().unwrap()))).collect()
                        };
                    });
                } else {
                    statements.push(quote! {
                        let #data = {
                            let mut raw: Vec<u8> = Vec::new();
                            #target.read_to_end(&mut raw)#async_?;
                            let size = mem::size_of::<#type_>();
                            raw.chunks_exact(size).map(|x| #enum_ident::from_repr(#type_::from_be_bytes(x.try_into().unwrap()))).collect()
                        };
                    });
                }
            }
            Instruction::Loop(target, stop_index, terminator, output, inner) => {
                let output = emit_register(*output);
                let inner = prepare_decode(options, context, &inner[..], is_async, false);
                let stop = stop_index.map(emit_register);
                let terminator = terminator.map(emit_register);
                let target = emit_target(target);
                if let Some(stop) = stop {
                    statements.push(quote! {
                        let mut #output = Vec::with_capacity(#stop as usize);
                        for _ in 0..#stop {
                            #inner
                        }
                    });
                } else if let Some(terminator) = terminator {
                    statements.push(quote! {
                        let mut #output = Vec::new();
                        loop {
                            let buf = #target.fill_buf()#async_?;
                            if buf.len() == 0 {
                                break;
                            }
                            if (buf.len() < #terminator.len()) {
                                //todo: confirm this cannot infinite loop
                                continue;
                            }
                            if &buf[..#terminator.len()] == #terminator {
                                #target.consume(#terminator.len());
                                break;
                            }
                            #inner
                        }
                    });
                } else {
                    statements.push(quote! {
                        let mut #output = Vec::new();
                        //TODO: optimize this to not buffer with a Peekable type
                        {
                            let mut r = vec![];
                            #target.read_to_end(&mut r)#async_?;
                            let r_len = r.len() as u64;

                            {
                                let mut #target = Cursor::new(r);
                                let #target = &mut #target;
                                while #target.position() < r_len {
                                    #inner
                                }
                            }
                        }
                    });
                }
            }
            Instruction::LoopOutput(output, item) => {
                let output = emit_register(*output);
                let item = emit_register(*item);
                statements.push(quote! {
                    #output.push(#item);
                });
            }
            Instruction::Conditional(target, interior, condition, inner) => {
                let target = emit_register(*target);
                let interior = emit_register(*interior);
                let condition = emit_register(*condition);
                let inner = prepare_decode(options, context, &inner[..], is_async, false);
                statements.push(quote! {
                    let #target = if #condition {
                        #inner
                        Some(#interior)
                    } else {
                        None
                    };
                });
            }
            Instruction::ConditionalPredicate(condition, inner) => {
                let condition = emit_register(*condition);
                let inner = prepare_decode(options, context, &inner[..], is_async, false);
                statements.push(quote! {
                    if #condition {
                        #inner
                    }
                });
            },
            Instruction::Return(result) => {
                let result = emit_register(*result);
                statements.push(quote! {
                    return Ok(#result);
                });
            },
            Instruction::Error(e) => {
                statements.push(quote! {
                    return Err(decode_error(#e).into());
                });
            },
            Instruction::Skip(target, len) => {
                let target = emit_target(target);
                let len = emit_register(*len);
                statements.push(quote! {
                    let mut big_scratch = vec![0u8; #len as usize];
                    #target.read_exact(&mut big_scratch[..])#async_?;
                });
            },
        }
    }

    let statements = flatten(statements);
    quote! {
        #statements
    }
}

pub fn prepare_decoder(options: &CompileOptions, coder: &Context, is_async: bool) -> TokenStream {
    let decode = prepare_decode(options, &coder, &coder.instructions[..], is_async, true);
    quote! {
        #decode
    }
}