taikai 0.1.0

kaitai-like serialization and deserialization
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
use proc_macro2::TokenStream;

use crate::type_spec::TypeSpec;
use crate::types::Type;
use crate::types::Meta;
use crate::types::Endian;
use crate::types::Encoding;

pub const PRIMITIVES: &[&str] = &[
    "u8",
    "u16",
    "u32",
    "u64",
    "u128",
    "i8",
    "i16",
    "i32",
    "i64",
    "i128",
    "f32",
    "f64",
];

#[derive(Debug)]
pub enum Repeat {
    NoRepeat,
    Eos,
    Expr(TokenStream),
    Until(TokenStream),
}

impl Repeat {
    pub fn is_repeat(&self) -> bool {
        match self {
            Repeat::NoRepeat => false,
            _ => true,
        }
    }
}

impl Default for Repeat {
    fn default() -> Self {
        Repeat::NoRepeat
    }
}

#[derive(Default, Debug)]
pub struct Attribute {
    pub id: String,
    pub typ: String,
    pub enum_: Option<String>,
    pub repeat: Repeat,
    pub cond: Option<TokenStream>,
    pub contents: Vec<u8>,
    pub size: Option<SizeProperties>,
    pub encoding: Option<Encoding>
}

#[derive(Debug)]
pub enum Length {
    Eos,
    Terminator(u8),
    Size(TokenStream),
}

#[derive(Debug)]
pub struct SizeProperties {
    pub length: Length,
    pub consume: bool,
    pub include: bool,
    pub eos_error: bool,
}

pub struct Instance {
    pub attr: Attribute,
    pos: usize,
    pub value: Option<TokenStream>,
}

impl Attribute {
    pub fn new<T: Into<String>, U: Into<String>>(
        id: T,
        typ: U,
        repeat: Repeat,
        cond: Option<TokenStream>,
        contents: Vec<u8>,
        encoding: Option<Encoding>,
        enum_: Option<String>,
        size: Option<SizeProperties>) -> Self
    {
        Self {
            id: id.into(),
            typ: typ.into(),
            repeat,
            cond,
            encoding,
            contents,
            enum_,
            size,
        }
    }

    pub fn name(&self) -> TokenStream {
        syn::parse_str(&self.id.clone()).unwrap()
    }

    pub fn resolve_scalar_type(&self, structure: &TypeSpec) -> Type {
        let path = self.typ.split('.').collect();
        structure.resolve_type(path)
    }

    pub fn absolute_path_of_compound_type(&self, structure: &TypeSpec) -> TokenStream {
        let scalar = if self.contents.is_empty() {
            let scalar = self.resolve_scalar_type(&structure);
            scalar.absolute_final_path()
        } else {
            quote!{ () }
        };

        let scalar = if self.size.is_some() && self.typ != "str" && !self.repeat.is_repeat() {
            quote!( std::vec::Vec<#scalar> )
        } else {
            scalar
        };

        let compound = match self.repeat {
            Repeat::NoRepeat => scalar,
            _ => quote!( std::vec::Vec<#scalar> ),
        };

        if self.cond.is_some() {
            quote!( std::option::Option<#compound> )
        } else {
            compound
        }
    }

    pub fn read_call(&self,
        typ: &Type,
        parent_precursors: &[TokenStream],
        root_precursor: TokenStream,
        meta: &Meta) -> TokenStream
    {
        let primitive_with_endian = |p: &&str| {
            self.typ.starts_with(p) &&
            self.typ.len() == p.len() + 2 &&
            (self.typ.ends_with("le") || self.typ.ends_with("be"))
        };

        let read_size = self.size.as_ref().map(|size| {
            let to_vec = quote!( |v: &[u8]| -> std::result::Result<Vec<u8>, ()> { Ok(v.to_vec()) } );
            match &size.length {
                Length::Size(len) => quote!( map_res!(take!(#len), #to_vec) ),
                Length::Terminator(chr) => quote!( map_res!(take_till!(|ch| ch == #chr), #to_vec) ),
                Length::Eos => quote!( map_res!(nom::rest, #to_vec) ),
            }
        });

        let read_scalar = match typ {
            Type::Primitive(_) if self.typ == "u8" && read_size.is_some() => {
                read_size.unwrap()
            }
            Type::Primitive(_) if PRIMITIVES.iter().any(primitive_with_endian) => {
                // primitive with endian (e.g. u8be)
                let (typ, endian) = &self.typ.split_at(self.typ.len() - 2);

                syn::parse_str(&format!("{}_{}", endian, typ)[..]).unwrap()
            }
            Type::Primitive(_) if PRIMITIVES.contains(&&self.typ[..]) => {
                // primitive without endian (e.g. u8)
                let big = syn::parse_str(&format!("be_{}", self.typ)[..]).unwrap();
                let little = syn::parse_str(&format!("le_{}", self.typ)[..]).unwrap();

                match &meta.endian {
                    Endian::Big => big,
                    Endian::Little => little,
                    Endian::Runtime(cond) => quote!(
                        match #cond {
                            Endian::Big => #big,
                            Endian::Little => #little,
                        }
                    )
                }
            },
            Type::Primitive(_) if self.typ == "str" => {
                // a 'str' or 'strz'
                let read_size = read_size.unwrap();

                let enc = match self.encoding.as_ref().unwrap_or(&meta.encoding) {
                    Encoding::Runtime(enc) => enc.clone(),
                    Encoding::Fixed(enc) => {
                        let enc = format!("{:?}", enc);
                        let enc: TokenStream = syn::parse_str(&enc[..]).unwrap();
                        quote!(#enc) // convert to str token
                    }
                };

                quote!( map_res!(#read_size, |v: Vec<u8>| decode_string(&v, &#enc)) )
            },
            Type::Custom(typ) => {
                // user-defined struct (e.g. super.header)
                let typ = typ.as_ref().borrow().absolute_final_path();
                let read_fn = TypeSpec::build_function_name("read", &parent_precursors, &Some(root_precursor));

                quote!(
                    apply!(#typ :: #read_fn, &_new_parents, _new_root, _meta, _ctx)
                )
            },
            _ => unimplemented!("Attribute::read_call(): type '{}' unknown", self.typ),
        };

        let read_scalar = if let Some(ref e) = self.enum_ {
            unimplemented!()
            //let e = typ.resolve_enum(e);
            //e.match_from_primitive(read_scalar)
        } else {
            read_scalar
        };

        let read_scalar = if self.contents.is_empty() {
            read_scalar
        } else {
            let contents: TokenStream;
            contents = syn::parse_str(&format!("&{:?}", self.contents)[..]).unwrap();
            quote!( do_parse!(tag!(#contents) >> (())) )
        };

        let read_compound = match &self.repeat {
            Repeat::NoRepeat => read_scalar,
            Repeat::Eos => quote!( many0!(complete!(#read_scalar)) ),
            //Repeat::Until(cond) => quote!( repeat_while!(#cond, #read_scalar) ), // many_till?
            Repeat::Until(_) => unimplemented!("Repeat::Until not implemented, yet"),
            Repeat::Expr(expr) => quote!( count!(#read_scalar, #expr) ),
        };

        let read_compound = if let Some(SizeProperties { length: Length::Size(len), .. }) = &self.size {
            match typ {
                Type::Custom(_) => {
                    quote! (
                        flat_map!(take!(#len), #read_compound)
                    )
                }
                _ => read_compound
            }
        } else {
            read_compound
        };

        let read = if let Some(cond) = &self.cond {
            quote!( cond!(#cond, #read_compound) )
        } else {
            read_compound
        };

        quote!{
            do_parse!(_input, __v: #read >> (__v))
        }
    }

    pub fn write_call(&self,
        typ: &Type,
        parents: &[TokenStream],
        root: TokenStream,
        meta: &Meta) -> TokenStream
    {
        let primitive_with_endian = |p: &&str| {
            self.typ.starts_with(p) &&
            self.typ.len() == p.len() + 2 &&
            (self.typ.ends_with("le") || self.typ.ends_with("be"))
        };
    
        let write_scalar = match typ {
            Type::Primitive(_) if self.typ == "u8" && self.size.is_some() => {
                quote!(
                    _io.write_all
                )
            },
            Type::Primitive(_) if PRIMITIVES.iter().any(primitive_with_endian) => {
                // primitive with endian (e.g. u8be)
                let (styp, endian) = &self.typ.split_at(self.typ.len() - 2);
    
                let endian = match *endian {
                    "le" => quote!(byteorder::LittleEndian),
                    "be" => quote!(byteorder::BigEndian),
                    _ => panic!("Endian '{}' in '{}' unkown!", endian, self.typ),
                };

                let func: TokenStream = syn::parse_str(&format!("write_{}", styp)[..]).unwrap();

                let typ: TokenStream = syn::parse_str(&styp).unwrap();
                if styp == &"u8" {
                    quote!( (#[inline] |attr: &#typ| _io.#func(*attr)) )
                } else {
                    quote!( (#[inline] |attr: &#typ| _io.#func::<#endian>(*attr)) )
                }
            }
            Type::Primitive(_) if PRIMITIVES.contains(&&self.typ[..]) => {
                // primitive without endian (e.g. u8)
                let call: TokenStream = syn::parse_str(&format!("_io.write_{}", self.typ)[..]).unwrap();

                let code = match &meta.endian {
                    _ if self.typ == "u8" => quote!( #call ),
                    Endian::Big => quote!( #call::<byteorder::BigEndian> ),
                    Endian::Little => quote!( #call::<byteorder::LittleEndian> ),
                    Endian::Runtime(cond) => quote!(
                        match #cond {
                            Endian::Big => #call::<byteorder::BigEndian>,
                            Endian::Little => #call::<byteorder::BigLittle>,
                        }
                    )
                };

                let typ: TokenStream = syn::parse_str(&self.typ).unwrap();
                quote!( (#[inline] |attr: &#typ| #code(*attr)) )
            },
            Type::Primitive(_) if self.typ == "str" => {
                let enc = match self.encoding.as_ref().unwrap_or(&meta.encoding) {
                    Encoding::Runtime(enc) => enc.clone(),
                    Encoding::Fixed(enc) => {
                        let enc = format!("{:?}", enc);
                        let enc: TokenStream = syn::parse_str(&enc[..]).unwrap();
                        quote!(#enc) // convert to str token
                    }
                };

                quote!( (|s| -> std::io::Result<()> {
                    encode_string(_io, s, &#enc)
                }))
            },
            Type::Custom(_) => {
                // user-defined struct (e.g. super.header)
                let write_fn = TypeSpec::build_function_name("write", &parents, &Some(root));
                let typ = typ.absolute_final_path();

                quote!(
                    (#[inline] |attr: &#typ| -> std::io::Result<()> { attr.#write_fn(_io, &_new_parents, _new_root, _meta, _ctx) } )
                )
            },
            _ => unimplemented!("Attribute::write_call(): type '{}' unknown", self.typ),
        };

        let attr: TokenStream = syn::parse_str(&self.id).unwrap();

        let write_scalar = if self.contents.is_empty() {
            write_scalar
        } else {
            let contents: TokenStream;
            contents = syn::parse_str(&format!("&{:?}", self.contents)[..]).unwrap();
            quote!( (#[inline] |_attr| -> std::io::Result<()> { _io.write_all(#contents) } ) )
        };

        let write_compound = match &self.repeat {
            Repeat::NoRepeat => quote!( (#write_scalar(&attr)?) ),
            _ => quote!(
                for _el in attr {
                    #write_scalar(_el)?
                }
            ),
        };

        let code = if let Some(cond) = &self.cond {
            quote!(
                if let Some(attr) = &self.#attr {
                    assert!(#cond);
                    #write_compound
                } else {
                    assert!(!(#cond));
                }
            )
        } else {
            quote!(
                {
                    let attr = &self.#attr;
                    #write_compound
                }
            )
        };

        quote!( try { #code } )
    }

}

impl Instance {
    pub fn new<T: Into<String>, U: Into<String>>(
        id: T,
        pos: usize,
        value: Option<TokenStream>,
        typ: U,
        repeat: Repeat,
        cond: Option<TokenStream>,
        contents: Vec<u8>,
        enc: Option<Encoding>,
        enum_: Option<String>,
        size_props: Option<SizeProperties>) -> Self
    {
        Self {
            pos,
            value,
            attr: Attribute::new(id, typ, repeat, cond, contents, enc, enum_, size_props),
        }
    }

    pub fn from_attr(attr: Attribute,
        pos: usize,
        value: Option<TokenStream>) -> Self
    {
        Self {
            pos,
            value,
            attr,
        }
    }
}