nanoserde 0.2.1

Serialization library with zero dependencies. Supports Binary, JSON, RON and TOML.
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use core::error::Error;
use core::str::Chars;

use alloc::format;
use alloc::string::{String, ToString};
use alloc::{collections::BTreeMap, vec, vec::Vec};

/// Pattern matching any valid unquoted key character as u32.
/// ABNF line: https://github.com/toml-lang/toml/blob/2431aa308a7bc97eeb50673748606e23a6e0f201/toml.abnf#L55
macro_rules! ident_chars {
    () => {
        '\u{41}'..='\u{5A}'
        | '\u{61}'..='\u{7A}'
        | '\u{30}'..='\u{39}'
        | '\u{2D}'
        | '\u{5F}'
        | '\u{B2}'
        | '\u{B3}'
        | '\u{B9}'
        | '\u{BC}'..='\u{BE}'
        | '\u{C0}'..='\u{D6}'
        | '\u{D8}'..='\u{F6}'
        | '\u{F8}'..='\u{37D}'
        | '\u{37F}'..='\u{1FFF}'
        | '\u{200C}'..='\u{200D}'
        | '\u{203F}'..='\u{2040}'
        | '\u{2070}'..='\u{218F}'
        | '\u{2460}'..='\u{24FF}'
        | '\u{2C00}'..='\u{2FEF}'
        | '\u{3001}'..='\u{D7FF}'
        | '\u{F900}'..='\u{FDCF}'
        | '\u{FDF0}'..='\u{FFFD}'
        | '\u{10000}'..='\u{EFFFF}'
    }
}

/// Pattern matching a character that can terminate a valid ident.
macro_rules! ident_term_chars {
    () => {
        ' ' | '\t' | '\n' | '\0' | '=' | ']'
    };
}

/// A parser for TOML string values.
///
/// ```rust
/// # use nanoserde::*;
/// let toml = "[Section]\nvalue=1";
/// let parsed = TomlParser::parse(toml).unwrap();
/// assert_eq!(parsed["Section.value"], Toml::Num(1.));
/// ```
#[derive(Default)]
#[non_exhaustive]
pub struct TomlParser {
    cur: char,
    line: usize,
    col: usize,
}

/// A TOML parsed token.
#[derive(PartialEq, Debug)]
#[non_exhaustive]
pub enum TomlTok {
    Ident(String),
    Str(String),
    U64(u64),
    I64(i64),
    F64(f64),
    Bool(bool),
    // TODO add option to enforce + sign for conversion to ident
    Nan(bool),
    Inf(bool),
    Date(String),
    Equals,
    BlockOpen,
    BlockClose,
    Comma,
    Eof,
}

impl From<TomlTok> for String {
    fn from(value: TomlTok) -> Self {
        match value {
            TomlTok::Ident(string) => string,
            TomlTok::Str(string) => string,
            TomlTok::U64(number) => number.to_string(),
            TomlTok::I64(number) => number.to_string(),
            TomlTok::F64(number) => number.to_string(),
            TomlTok::Bool(boolean) => boolean.to_string(),
            TomlTok::Nan(negative) => {
                if negative {
                    "-nan".to_string()
                } else {
                    "nan".to_string()
                }
            }
            TomlTok::Inf(negative) => {
                if negative {
                    "-inf".to_string()
                } else {
                    "inf".to_string()
                }
            }
            TomlTok::Date(string) => string,
            TomlTok::Equals => '='.to_string(),
            TomlTok::BlockOpen => '['.to_string(),
            TomlTok::BlockClose => ']'.to_string(),
            TomlTok::Comma => ','.to_string(),
            TomlTok::Eof => '\0'.to_string(),
        }
    }
}

/// A TOML value.
#[derive(Debug, PartialEq)]
pub enum Toml {
    Str(String),
    Bool(bool),
    Num(f64),
    Date(String),
    Array(Vec<BTreeMap<String, Toml>>),
    SimpleArray(Vec<Toml>),
}

impl core::ops::Index<usize> for Toml {
    type Output = BTreeMap<String, Toml>;

    fn index(&self, index: usize) -> &Self::Output {
        match self {
            Toml::Array(array) => &array[index],
            _ => panic!(),
        }
    }
}

impl Toml {
    /// Get the TOML value as a float
    ///
    /// Panics if the TOML value isn't actually a float
    pub fn num(&self) -> f64 {
        match self {
            Toml::Num(num) => *num,
            _ => panic!(),
        }
    }
    /// Get the TOML value as a string
    ///
    /// Panics if the TOML value isn't actually a string
    pub fn str(&self) -> &str {
        match self {
            Toml::Str(string) => string,
            _ => panic!(),
        }
    }
    /// Get the TOML value as a boolean
    ///
    /// Panics if the TOML value isn't actually a boolean
    pub fn boolean(&self) -> bool {
        match self {
            Toml::Bool(boolean) => *boolean,
            _ => panic!(),
        }
    }
    /// Get the TOML value as a date string
    ///
    /// Panics if the TOML value isn't actually a date string.  See
    /// [the spec](https://toml.io/en/v1.0.0#local-date) for what "date
    /// string" means.
    pub fn date(&self) -> String {
        match self {
            Toml::Date(date) => date.to_string(),
            _ => panic!(),
        }
    }
    /// Get the TOML value as a table
    ///
    /// Panics if the TOML value isn't actually a table
    pub fn arr(&self) -> &Vec<BTreeMap<String, Toml>> {
        match self {
            Toml::Array(array) => array,
            _ => panic!(),
        }
    }
    /// Get the TOML value as an array
    ///
    /// Panics if the TOML value isn't actually an array
    pub fn simple_arr(&self) -> &Vec<Toml> {
        match self {
            Toml::SimpleArray(array) => array,
            _ => panic!(),
        }
    }
}

/// The error message when failing to parse a TOML string.
#[derive(Clone)]
#[non_exhaustive]
pub struct TomlErr {
    pub msg: String,
    pub line: usize,
    pub col: usize,
}

impl core::fmt::Debug for TomlErr {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "Toml error: {}, line:{} col:{}",
            self.msg,
            self.line + 1,
            self.col + 1
        )
    }
}

impl core::fmt::Display for TomlErr {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Debug::fmt(self, f)
    }
}

struct Out {
    out: BTreeMap<String, Toml>,
    active_array_element: Option<(String, usize)>,
}
impl Out {
    fn start_array(&mut self, key: &str) {
        if !self.out.contains_key(key) {
            self.out.insert(key.to_string(), Toml::Array(vec![]));
        }

        let n = match self.out.get_mut(key).unwrap() {
            Toml::Array(array) => {
                let n = array.len();
                array.push(BTreeMap::new());
                n
            }
            _ => unreachable!(),
        };

        self.active_array_element = Some((key.to_string(), n));
    }

    fn out(&mut self) -> &mut BTreeMap<String, Toml> {
        if let Some((table, n)) = self.active_array_element.clone() {
            match self.out.get_mut(&table).unwrap() {
                Toml::Array(array) => &mut array[n],
                _ => unreachable!(),
            }
        } else {
            &mut self.out
        }
    }
}

impl Error for TomlErr {}

impl TomlParser {
    /// Parse a TOML string.
    pub fn parse(data: &str) -> Result<BTreeMap<String, Toml>, TomlErr> {
        let i = &mut data.chars();
        let mut t = TomlParser::default();
        t.next(i);
        let mut out = Out {
            out: BTreeMap::new(),
            active_array_element: None,
        };
        let mut local_scope = String::new();
        while t.parse_line(i, &mut local_scope, &mut out)? {}

        Ok(out.out)
    }

    fn parse_line(
        &mut self,
        i: &mut Chars,
        local_scope: &mut String,
        out: &mut Out,
    ) -> Result<bool, TomlErr> {
        let tok = self.next_tok(i)?;
        match tok {
            TomlTok::Eof => {
                // at eof.
                return Ok(false);
            }
            TomlTok::BlockOpen => {
                // its a scope
                // we should expect an ident or a string
                let tok = self.next_tok(i)?;
                match tok {
                    TomlTok::Str(key) | TomlTok::Ident(key) => {
                        *local_scope = key;
                        let tok = self.next_tok(i)?;
                        if tok != TomlTok::BlockClose {
                            return Err(self.err_token(tok));
                        }
                    }
                    TomlTok::BlockOpen => {
                        let tok = self.next_tok(i)?;
                        let key = match tok {
                            TomlTok::Ident(key) => key,
                            TomlTok::U64(_)
                            | TomlTok::I64(_)
                            | TomlTok::F64(_)
                            | TomlTok::Bool(_)
                            | TomlTok::Nan(_)
                            | TomlTok::Inf(_)
                            | TomlTok::Date(_) => tok.into(),
                            _ => return Err(self.err_token(tok)),
                        };
                        let tok = self.next_tok(i)?;
                        if tok != TomlTok::BlockClose {
                            return Err(self.err_token(tok));
                        }
                        let tok = self.next_tok(i)?;
                        if tok != TomlTok::BlockClose {
                            return Err(self.err_token(tok));
                        }
                        out.start_array(&key);
                    }
                    _ => return Err(self.err_token(tok)),
                }
            }
            TomlTok::Str(_)
            | TomlTok::Ident(_)
            | TomlTok::U64(_)
            | TomlTok::I64(_)
            | TomlTok::F64(_)
            | TomlTok::Bool(_)
            | TomlTok::Nan(_)
            | TomlTok::Inf(_)
            | TomlTok::Date(_) => self.parse_key_value(local_scope, tok.into(), i, out.out())?,
            _ => return Err(self.err_token(tok)),
        }
        Ok(true)
    }

    #[allow(clippy::wrong_self_convention)]
    fn to_val(&mut self, tok: TomlTok, i: &mut Chars) -> Result<Toml, TomlErr> {
        match tok {
            TomlTok::BlockOpen => {
                let mut vals = Vec::new();
                loop {
                    let tok = self.next_tok(i)?;
                    if tok == TomlTok::BlockClose || tok == TomlTok::Eof {
                        break;
                    }
                    if tok != TomlTok::Comma {
                        vals.push(self.to_val(tok, i)?);
                    }
                }
                Ok(Toml::SimpleArray(vals))
            }
            TomlTok::Str(v) => Ok(Toml::Str(v)),
            TomlTok::U64(v) => Ok(Toml::Num(v as f64)),
            TomlTok::I64(v) => Ok(Toml::Num(v as f64)),
            TomlTok::F64(v) => Ok(Toml::Num(v)),
            TomlTok::Bool(v) => Ok(Toml::Bool(v)),
            TomlTok::Nan(v) => Ok(Toml::Num(if v { -f64::NAN } else { f64::NAN })),
            TomlTok::Inf(v) => Ok(Toml::Num(if v { -f64::INFINITY } else { f64::INFINITY })),
            TomlTok::Date(v) => Ok(Toml::Date(v)),
            _ => Err(self.err_token(tok)),
        }
    }

    fn parse_key_value(
        &mut self,
        local_scope: &String,
        key: String,
        i: &mut Chars,
        out: &mut BTreeMap<String, Toml>,
    ) -> Result<(), TomlErr> {
        let tok = self.next_tok(i)?;
        if tok != TomlTok::Equals {
            return Err(self.err_token(tok));
        }
        let tok = self.next_tok(i)?;
        let val = self.to_val(tok, i)?;
        let key = if !local_scope.is_empty() {
            format!("{}.{}", local_scope, key)
        } else {
            key
        };
        out.insert(key, val);
        Ok(())
    }

    fn next(&mut self, i: &mut Chars) {
        if let Some(c) = i.next() {
            self.cur = c;
            if self.cur == '\n' {
                self.line += 1;
                self.col = 0;
            } else {
                self.col = 0;
            }
        } else {
            self.cur = '\0';
        }
    }

    fn err_token(&self, tok: TomlTok) -> TomlErr {
        TomlErr {
            msg: format!("Unexpected token {:?} ", tok),
            line: self.line,
            col: self.col,
        }
    }

    fn err_parse(&self, what: &str) -> TomlErr {
        TomlErr {
            msg: format!("Cannot parse toml {} ", what),
            line: self.line,
            col: self.col,
        }
    }

    fn next_tok(&mut self, i: &mut Chars) -> Result<TomlTok, TomlErr> {
        while self.cur == '\n' || self.cur == '\r' || self.cur == '\t' || self.cur == ' ' {
            self.next(i);
        }
        loop {
            if self.cur == '\0' {
                return Ok(TomlTok::Eof);
            }

            #[allow(unreachable_patterns)]
            match self.cur {
                ',' => {
                    self.next(i);
                    return Ok(TomlTok::Comma);
                }
                '[' => {
                    self.next(i);
                    return Ok(TomlTok::BlockOpen);
                }
                ']' => {
                    self.next(i);
                    return Ok(TomlTok::BlockClose);
                }
                '=' => {
                    self.next(i);
                    return Ok(TomlTok::Equals);
                }
                '#' => {
                    while self.cur != '\n' && self.cur != '\0' {
                        self.next(i);
                    }

                    while self.cur == '\n'
                        || self.cur == '\r'
                        || self.cur == '\t'
                        || self.cur == ' '
                    {
                        self.next(i);
                    }
                }
                '+' | '-' | '0'..='9' => return self.parse_num(i),
                '"' => {
                    let mut val = String::new();
                    self.next(i);
                    let mut braces = 1;
                    while self.cur == '"' && braces < 3 {
                        braces += 1;
                        self.next(i);
                    }
                    let escaped_string = braces == 3;
                    loop {
                        if self.cur == '"' && !escaped_string {
                            break;
                        }
                        if self.cur == '"' && escaped_string {
                            let mut tmp = String::new();
                            let mut braces = 0;
                            while self.cur == '"' {
                                tmp.push('"');
                                braces += 1;
                                self.next(i);
                            }
                            if braces == 3 {
                                break;
                            }
                            val.push_str(&tmp);
                        }
                        if self.cur == '\\' {
                            self.next(i);
                        }
                        if self.cur == '\0' {
                            return Err(self.err_parse("string"));
                        }
                        val.push(self.cur);
                        self.next(i);
                    }
                    self.next(i);
                    return Ok(TomlTok::Str(val));
                }
                ident_chars!() => return self.parse_ident(i, String::new()),
                _ => return Err(self.err_parse("tokenizer")),
            }
        }
    }

    /// Parse an ident or similar, starting with the current character.
    fn parse_ident(&mut self, i: &mut Chars, mut start: String) -> Result<TomlTok, TomlErr> {
        while matches!(self.cur, ident_chars!()) {
            start.push(self.cur);
            self.next(i);
        }

        if self.cur == '.' {
            start.push(self.cur);
            self.next(i);
            return self.parse_ident(i, start); // recursion here could be a problem
        }

        if matches!(self.cur, ident_term_chars!()) {
            return Ok(match start.as_ref() {
                "true" => TomlTok::Bool(true),
                "false" => TomlTok::Bool(false),
                "inf" => TomlTok::Inf(false),
                "nan" => TomlTok::Nan(false),
                _ => TomlTok::Ident(start),
            });
        }

        Err(self.err_parse("tokenizer"))
    }

    /// Parses a number (or an ident that starts with numbers), starting with the current character.
    fn parse_num(&mut self, i: &mut Chars) -> Result<TomlTok, TomlErr> {
        let mut num = String::new();

        let mut negative = false;
        if self.cur == '+' {
            self.next(i)
        } else if self.cur == '-' {
            num.push(self.cur);
            negative = true;
            self.next(i);
        }

        if self.cur == 'n' {
            num.push(self.cur);
            self.next(i);
            if self.cur == 'a' {
                num.push(self.cur);
                self.next(i);
                if self.cur == 'n' {
                    num.push(self.cur);
                    self.next(i);
                    if matches!(self.cur, ident_term_chars!()) {
                        return Ok(TomlTok::Nan(negative));
                    }
                }
            }
        } else if self.cur == 'i' {
            num.push(self.cur);
            self.next(i);
            if self.cur == 'n' {
                num.push(self.cur);
                self.next(i);
                if self.cur == 'f' {
                    num.push(self.cur);
                    self.next(i);
                    if matches!(self.cur, ident_term_chars!()) {
                        return Ok(TomlTok::Inf(negative));
                    }
                }
            }
        }

        while matches!(self.cur, '0'..='9' | '_') {
            if self.cur != '_' {
                num.push(self.cur);
            }
            self.next(i);
        }

        if self.cur == '.' {
            num.push(self.cur);
            self.next(i);
            while matches!(self.cur, '0'..='9' | '_') {
                if self.cur != '_' {
                    num.push(self.cur);
                }
                self.next(i);
            }
            if let Ok(num) = num.parse() {
                return Ok(TomlTok::F64(num));
            } else {
                return Err(self.err_parse("number"));
            }
        } else if self.cur == '-' {
            // lets assume its a date. whatever. i don't feel like more parsing today
            num.push(self.cur);
            self.next(i);
            while matches!(self.cur, '0'..='9' | ':' | '-' | 'T') {
                num.push(self.cur);
                self.next(i);
            }
            return Ok(TomlTok::Date(num));
            // TODO rework this
        }

        if matches!(self.cur, ident_chars!()) {
            return self.parse_ident(i, num);
        }

        match negative {
            true => {
                if let Ok(num) = num.parse() {
                    return Ok(TomlTok::I64(num));
                }
            }
            false => {
                if let Ok(num) = num.parse() {
                    return Ok(TomlTok::U64(num));
                }
            }
        }

        Err(self.err_parse("tokenizer"))
    }
}