dapt 0.1.9

serializing and unseralizing unstructured data
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::fmt;
use std::ops::Deref;

use crate::Error as DaptError;

use crate::binary::{BKeyValue, BReference, BToken, Binary};
use crate::error::DaptResult;

use super::lexer::Lexer;
use super::node::{
    Aquireable, Array, Discoverable, FieldLiteral, First, Multi, Recursive, Regexp, Wildcard,
};

const NESTING_OPERATOR: &str = ".";
const INDEX_OPERATOR: &str = "[";
const INDEX_OPERATOR_END: &str = "]";
const WILDCARD_OPERATOR: &str = "*";
const RECURSIVE_OPERATOR: &str = "~";
const FIRST_OPERATOR: &str = "{";
const FIRST_OPERATOR_END: &str = "}";
const FIRST_OPERATOR_SEP: &str = ",";
const MULTI_OPERATOR: &str = "(";
const MULTI_OPERATOR_END: &str = ")";
const MULTI_OPERATOR_SEP: &str = "|";
const REGEXP_OPERATOR: &str = "/";
const STRING_WRAP: &str = "\"";

#[derive(Debug)]
pub enum ParseError {
    EOF,
    EOS,
    UnexpectedEOF,
    MalformedPath(String),
    InvalidIndex(String),
}

impl ParseError {
    pub fn to_string(&self) -> String {
        match self {
            ParseError::EOF => "EOF".to_string(),
            // used to signal the end of a section in operators like
            // first or multi.
            ParseError::EOS => "End Of Section".to_string(),
            ParseError::UnexpectedEOF => "Unexpected EOF".to_string(),
            ParseError::MalformedPath(s) => format!("Malformed path: {}", s),
            ParseError::InvalidIndex(s) => format!("Invalid index: {}", s),
        }
    }
}

type ParseResult<T> = Result<T, ParseError>;

#[derive(Debug, PartialEq, Clone)]
pub enum Node {
    FieldLiteral(FieldLiteral),
    Array(Array),
    Wildcard(Wildcard),
    Recursive(Recursive),
    First(First),
    Multi(Multi),
    Regexp(Regexp),
}

impl Node {
    pub fn find<F>(&self, bin: &Binary, b: BReference, f: &mut F)
    where
        F: FnMut(BReference),
    {
        match self {
            Node::FieldLiteral(fl) => fl.find(bin, b, f),
            Node::Array(ar) => ar.find(bin, b, f),
            Node::Wildcard(w) => w.find(bin, b, f),
            Node::Recursive(r) => r.find(bin, b, f),
            Node::First(fr) => fr.find(bin, b, f),
            Node::Multi(m) => m.find(bin, b, f),
            Node::Regexp(r) => r.find(bin, b, f),
        }
    }

    pub fn aquire(&self, bin: &mut Binary, b: BReference) -> DaptResult<BKeyValue> {
        match self {
            Node::FieldLiteral(fl) => fl.aquire(bin, b),
            _ => Err(DaptError::CanNotAquire(
                "can not aquire from non field literal".to_string(),
            )),
        }
    }

    pub fn new_field_literal(field: &str) -> ParseResult<Node> {
        Ok(Node::FieldLiteral(FieldLiteral::from_escaped(field)))
    }
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Node::FieldLiteral(fl) => write!(f, "{}", fl),
            Node::Array(ar) => write!(f, "{}", ar),
            Node::Wildcard(wc) => write!(f, "{}", wc),
            Node::Recursive(r) => write!(f, ".{}", r),
            Node::First(fr) => write!(f, "{}", fr),
            Node::Multi(m) => write!(f, "{}", m),
            Node::Regexp(r) => write!(f, "{}", r),
        }
    }
}

pub struct Parser<'a> {
    lex: Lexer<'a>,
}

impl<'a> From<Lexer<'a>> for Parser<'a> {
    fn from(lex: Lexer<'a>) -> Parser<'a> {
        Parser { lex }
    }
}

impl<'a> From<&'a str> for Parser<'a> {
    fn from(path: &'a str) -> Parser<'a> {
        Parser {
            lex: Lexer::from(path),
        }
    }
}

// Path is a collection of nodes. It also impelmets the Discoverable
// trait.
#[derive(Debug, PartialEq, Clone)]
pub struct Path(Vec<Node>);

impl Path {
    pub fn new(path: &str) -> Result<Path, ParseError> {
        Path::try_from(path)
    }

    fn from_nodes(nodes: Vec<Node>) -> Path {
        Path(nodes)
    }

    pub fn aquire(&self, bin: &mut Binary, b: BReference) -> DaptResult<BKeyValue> {
        let mut b = b;
        let mut kv = None;

        for node in self.iter() {
            kv = Some(node.aquire(bin, b)?);
            b = BToken::from(kv.unwrap()).get_reference(bin);
        }

        if let Some(kv) = kv {
            Ok(kv)
        } else {
            Err(DaptError::CanNotAquire("empty path".to_string()))
        }
    }

    // https://github.com/rust-lang/rust/issues/43520 here is why we need
    // this, in the future hopefully we can do this without the heap allocations
    pub fn find_simple(&self, bin: &Binary, b: BReference) -> Vec<BReference> {
        let mut cur_ptrs = vec![b];
        let mut next_ptrs = vec![];

        for node in self.iter() {
            cur_ptrs
                .iter()
                .for_each(|b| node.find(bin, *b, &mut |b| next_ptrs.push(b)));
            cur_ptrs.resize(next_ptrs.len(), BReference::from(0));
            cur_ptrs.copy_from_slice(&next_ptrs);
            next_ptrs.clear();
        }

        cur_ptrs
    }

    fn find_depth<F>(&self, bin: &Binary, b: BReference, depth: usize, f: &mut F)
    where
        F: FnMut(BReference),
    {
        let node = match self.0.get(depth) {
            Some(node) => node,
            None => {
                f(b);
                return;
            }
        };

        node.find(bin, b, &mut |b| self.find_depth(bin, b, depth + 1, f));
    }

    pub fn append_key(&mut self, key: &str) {
        self.0.push(Node::FieldLiteral(FieldLiteral::new(key)));
    }
}

impl Default for Path {
    fn default() -> Self {
        Path(vec![])
    }
}

impl Discoverable for Path {
    fn find<F>(&self, bin: &Binary, b: BReference, f: &mut F)
    where
        F: FnMut(BReference),
    {
        self.find_depth(bin, b, 0, f)
    }
}

impl fmt::Display for Path {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut path = String::new();
        for node in self.iter() {
            match node {
                Node::FieldLiteral(fl) => path.push_str(&format!(".{}", fl)),
                Node::Array(ar) => path.push_str(&format!("{}", ar)),
                Node::Wildcard(wc) => path.push_str(&format!(".{}", wc)),
                Node::Recursive(r) => path.push_str(&format!(".{}", r)),
                Node::First(f) => path.push_str(&format!(".{}", f)),
                Node::Multi(m) => path.push_str(&format!(".{}", m)),
                Node::Regexp(r) => path.push_str(&format!(".{}", r)),
            }
        }

        write!(f, "{}", path.trim_start_matches('.'))
    }
}

impl Deref for Path {
    type Target = Vec<Node>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TryFrom<&str> for Path {
    type Error = ParseError;

    fn try_from(path: &str) -> Result<Self, Self::Error> {
        let mut p = Parser::from(path);
        let mut nodes = vec![];

        loop {
            let node = p.parse(Node::new_field_literal);
            match node {
                Err(ParseError::EOF) => break,
                Err(err) => Err(err)?,
                Ok(n) => nodes.push(n),
            };
        }

        Ok(Path(nodes))
    }
}

impl From<Vec<Node>> for Path {
    fn from(nodes: Vec<Node>) -> Path {
        Path(nodes)
    }
}

impl Parser<'_> {
    fn parse<F>(&mut self, ext: F) -> ParseResult<Node>
    where
        F: Fn(&str) -> ParseResult<Node>,
    {
        let token = self.lex.token();
        if let None = token {
            return Err(ParseError::EOF);
        }
        let token = token.unwrap();

        match token {
            // if we hit a nesting operator we should just try again
            NESTING_OPERATOR => self.parse(Node::new_field_literal),
            STRING_WRAP => self.parse_wrapped_field_literal(),
            INDEX_OPERATOR => self.parse_index(),
            WILDCARD_OPERATOR => Ok(Node::Wildcard(Wildcard)),
            RECURSIVE_OPERATOR => self.parse_recursive(),
            MULTI_OPERATOR => self.parse_multi(),
            FIRST_OPERATOR => self.parse_first(),
            REGEXP_OPERATOR => self.parse_regexp(),
            _ => ext(token),
        }
    }

    fn parse_wrapped_field_literal(&mut self) -> ParseResult<Node> {
        let node = match self.lex.token() {
            Some(tok) => FieldLiteral::from_escaped(tok),
            None => return Err(ParseError::UnexpectedEOF),
        };

        match self.lex.token() {
            Some(STRING_WRAP) => Ok(Node::FieldLiteral(node)),
            Some(token) => Err(ParseError::MalformedPath(format!(
                "unexpected token: {}, expected string wrap",
                token
            ))),
            None => Err(ParseError::UnexpectedEOF),
        }
    }

    fn parse_regexp(&mut self) -> ParseResult<Node> {
        let reg = match self.lex.token() {
            Some(reg) => Regexp::new(reg),
            None => return Err(ParseError::UnexpectedEOF),
        };

        match self.lex.token() {
            Some(REGEXP_OPERATOR) => Ok(Node::Regexp(reg)),
            Some(token) => Err(ParseError::MalformedPath(format!(
                "unexpected token: {}, expected regex operator",
                token
            ))),
            None => Err(ParseError::UnexpectedEOF),
        }
    }

    fn parse_multi(&mut self) -> ParseResult<Node> {
        let mut paths = vec![];

        loop {
            let (path, cont) = self.parse_path(|token| match token {
                MULTI_OPERATOR_SEP => Err(ParseError::EOS),
                MULTI_OPERATOR_END => Err(ParseError::EOF),
                MULTI_OPERATOR => Err(ParseError::MalformedPath("unexpected (".to_string())),
                FIRST_OPERATOR => Err(ParseError::MalformedPath("unexpected {".to_string())),
                FIRST_OPERATOR_END => Err(ParseError::MalformedPath("unexpected }".to_string())),
                FIRST_OPERATOR_SEP => Err(ParseError::MalformedPath("unexpected ,".to_string())),
                _ => Node::new_field_literal(token),
            })?;

            paths.push(path);

            if !cont {
                break;
            }
        }

        Ok(Node::Multi(Multi::new(paths)))
    }

    fn parse_first(&mut self) -> ParseResult<Node> {
        let mut paths = vec![];

        loop {
            let (path, cont) = self.parse_path(|token| match token {
                FIRST_OPERATOR_SEP => Err(ParseError::EOS),
                FIRST_OPERATOR_END => Err(ParseError::EOF),
                FIRST_OPERATOR => Err(ParseError::MalformedPath("unexpected {".to_string())),
                MULTI_OPERATOR => Err(ParseError::MalformedPath("unexpected (".to_string())),
                MULTI_OPERATOR_END => Err(ParseError::MalformedPath("unexpected )".to_string())),
                MULTI_OPERATOR_SEP => Err(ParseError::MalformedPath("unexpected |".to_string())),
                _ => Node::new_field_literal(token),
            })?;

            paths.push(path);

            if !cont {
                break;
            }
        }

        Ok(Node::First(First::new(paths)))
    }

    // parse_path is a helper function that expects you to handle returning
    // EOS when done parsing a path, and EOF when done parsing a list of
    // paths. This is used for first, and multi. If you should continue the
    // function will return true, if you should stop because EOF was returned
    // from the parser it will return false
    fn parse_path<F>(&mut self, ext: F) -> ParseResult<(Path, bool)>
    where
        F: Fn(&str) -> ParseResult<Node>,
    {
        let mut nodes = vec![];
        let mut cont = true;
        loop {
            let node = self.parse(&ext);
            match node {
                Err(ParseError::EOS) => break,
                Err(ParseError::EOF) => {
                    cont = false;
                    break;
                }
                Err(err) => Err(err)?,
                Ok(n) => nodes.push(n),
            };
        }

        if nodes.len() == 0 {
            return Err(ParseError::MalformedPath(
                "empty path in first operator".to_string(),
            ));
        }

        Ok((Path::from_nodes(nodes), cont))
    }

    fn parse_recursive(&mut self) -> ParseResult<Node> {
        let node = self.parse(Node::new_field_literal);
        if let Err(ParseError::EOF) = node {
            return Err(ParseError::UnexpectedEOF);
        }

        Ok(Node::Recursive(Recursive::new(node?)))
    }

    fn parse_index(&mut self) -> ParseResult<Node> {
        let token = self.lex.token();
        match token {
            Some(INDEX_OPERATOR_END) => Ok(Node::Array(Array::new(None))),
            Some(index) => {
                let end_index = self.lex.token();
                if let None = end_index {
                    return Err(ParseError::UnexpectedEOF);
                }

                if end_index.unwrap() != INDEX_OPERATOR_END {
                    return Err(ParseError::MalformedPath(
                        "missing closing bracket for array".to_string(),
                    ));
                }

                let index = match index.parse::<usize>() {
                    Ok(index) => index,
                    Err(_) => return Err(ParseError::InvalidIndex(index.to_string())),
                };

                Ok(Node::Array(Array::new(Some(index))))
            }
            None => Err(ParseError::UnexpectedEOF),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test_parse {
        ($path:expr, $($args:tt),*) => {
            let mut p = Parser::from($path);
            let mut nodes = vec![];
            let expected: Vec<&str> = vec![$($args),*];

            let mut node = p.parse(Node::new_field_literal);
            while let Ok(n) = node {
                nodes.push(n.to_string());
                node = p.parse(Node::new_field_literal);
            }

            assert_eq!(nodes, expected);
        };
    }

    #[test]
    fn test_parse() {
        test_parse!("Im.am a.fish", "Im", "\"am a\"", "fish");
        test_parse!("a.b.c.d.e", "a", "b", "c", "d", "e");
        test_parse!("a.\"b.a\".c", "a", "\"b.a\"", "c");
        test_parse!("a.b\\.a.c", "a", "\"b.a\"", "c");
    }
}