msd 0.4.0

A library for reading and writing MSD files.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
use crate::de::Position;
use serde::{
    de,
    de::{Expected, Unexpected},
};
use std::{fmt, fmt::Display};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Kind {
    // Formatting errors.
    EndOfFile,
    ExpectedTag,
    UnexpectedTag,
    EndOfTag,
    UnexpectedValues,
    UnexpectedValue,
    EndOfValues,

    // Value errors.
    ExpectedBool,
    ExpectedI8,
    ExpectedI16,
    ExpectedI32,
    ExpectedI64,
    ExpectedI128,
    ExpectedU8,
    ExpectedU16,
    ExpectedU32,
    ExpectedU64,
    ExpectedU128,
    ExpectedF32,
    ExpectedF64,
    ExpectedChar,
    ExpectedString,
    ExpectedUnit,
    ExpectedIdentifier,

    // IO-related errors.
    Io,

    // User-provided errors (provided through `serde::de::Error` trait methods).
    Custom(String),
    InvalidType(String, String),
    InvalidValue(String, String),
    InvalidLength(usize, String),
    UnknownVariant(String, &'static [&'static str]),
    UnknownField(String, &'static [&'static str]),
    MissingField(&'static str),
    DuplicateField(&'static str),

    // Unrepresentable type errors.
    CannotDeserializeAsSelfDescribing,
    CannotDeserializeAsOptionInTuple,
    CannotDeserializeAsSeqInTuple,
    CannotDeserializeAsMapInTuple,
    CannotDeserializeAsStructInTuple,
    CannotDeserializeNestedStruct,
    MustDeserializeStructFieldAsIdentifier,
    CannotDeserializeAsOptionInSeq,
    CannotDeserializeNestedSeq,
    MustDeserializeEnumVariantAsIdentifier,
}

impl Display for Kind {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Kind::EndOfFile => formatter.write_str("unexpected end of file"),
            Kind::ExpectedTag => formatter.write_str("expected tag"),
            Kind::UnexpectedTag => formatter.write_str("unexpected tag"),
            Kind::EndOfTag => formatter.write_str("unexpected end of tag"),
            Kind::UnexpectedValues => formatter.write_str("unexpected values"),
            Kind::UnexpectedValue => formatter.write_str("unexpected value"),
            Kind::EndOfValues => formatter.write_str("unexpected end of values"),
            Kind::ExpectedBool => formatter.write_str("expected bool"),
            Kind::ExpectedI8 => formatter.write_str("expected i8"),
            Kind::ExpectedI16 => formatter.write_str("expected i16"),
            Kind::ExpectedI32 => formatter.write_str("expected i32"),
            Kind::ExpectedI64 => formatter.write_str("expected i64"),
            Kind::ExpectedI128 => formatter.write_str("expected i128"),
            Kind::ExpectedU8 => formatter.write_str("expected u8"),
            Kind::ExpectedU16 => formatter.write_str("expected u16"),
            Kind::ExpectedU32 => formatter.write_str("expected u32"),
            Kind::ExpectedU64 => formatter.write_str("expected u64"),
            Kind::ExpectedU128 => formatter.write_str("expected u128"),
            Kind::ExpectedF32 => formatter.write_str("expected f32"),
            Kind::ExpectedF64 => formatter.write_str("expected f64"),
            Kind::ExpectedChar => formatter.write_str("expected char"),
            Kind::ExpectedString => formatter.write_str("expected string"),
            Kind::ExpectedUnit => formatter.write_str("expected unit value"),
            Kind::ExpectedIdentifier => formatter.write_str("expected identifier"),
            Kind::Io => formatter.write_str("io error"),
            Kind::Custom(msg) => formatter.write_str(msg),
            Kind::InvalidType(unexpected, expected) => {
                write!(
                    formatter,
                    "invalid type: expected {}, found {}",
                    expected, unexpected
                )
            }
            Kind::InvalidValue(unexpected, expected) => {
                write!(
                    formatter,
                    "invalid value: expected {}, found {}",
                    expected, unexpected
                )
            }
            Kind::InvalidLength(length, expected) => {
                write!(
                    formatter,
                    "invalid length {}, expected {}",
                    length, expected
                )
            }
            Kind::UnknownVariant(variant, expected) => {
                write!(
                    formatter,
                    "unknown variant {}, expected one of {:?}",
                    variant, expected
                )
            }

            Kind::UnknownField(field, expected) => {
                write!(
                    formatter,
                    "unknown field {}, expected one of {:?}",
                    field, expected
                )
            }
            Kind::MissingField(field) => {
                write!(formatter, "missing field {}", field)
            }

            Kind::DuplicateField(field) => {
                write!(formatter, "duplicate field {}", field)
            }
            Kind::CannotDeserializeAsSelfDescribing => {
                formatter.write_str("cannot deserialize as self-describing")
            }
            Kind::CannotDeserializeAsOptionInTuple => {
                formatter.write_str("cannot deserialize as option in tuple")
            }
            Kind::CannotDeserializeAsSeqInTuple => {
                formatter.write_str("cannot deserialize as seq in tuple")
            }
            Kind::CannotDeserializeAsMapInTuple => {
                formatter.write_str("cannot deserialize as map in tuple")
            }
            Kind::CannotDeserializeAsStructInTuple => {
                formatter.write_str("cannot deserialize as struct in tuple")
            }
            Kind::CannotDeserializeNestedStruct => {
                formatter.write_str("cannot deserialize nested struct")
            }
            Kind::MustDeserializeStructFieldAsIdentifier => {
                formatter.write_str("must deserialize struct field as identifier")
            }
            Kind::CannotDeserializeAsOptionInSeq => {
                formatter.write_str("cannot deserialize as option in seq")
            }
            Kind::CannotDeserializeNestedSeq => {
                formatter.write_str("cannot deserialize nested seq")
            }
            Kind::MustDeserializeEnumVariantAsIdentifier => {
                formatter.write_str("must deserialize enum variant as identifier")
            }
        }
    }
}

/// An error that may occur during deserialization.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
    position: Position,
    kind: Kind,
}

impl Error {
    pub(super) fn new(kind: Kind, position: Position) -> Self {
        Self { position, kind }
    }

    pub(in crate::de) fn set_position(&mut self, position: Position) {
        self.position = position;
    }
}

impl de::Error for Error {
    fn custom<T>(msg: T) -> Self
    where
        T: Display,
    {
        Self::new(Kind::Custom(msg.to_string()), Position::new(0, 0))
    }

    fn invalid_type(unexpected: Unexpected, expected: &dyn Expected) -> Self {
        Self::new(
            Kind::InvalidType(unexpected.to_string(), expected.to_string()),
            Position::new(0, 0),
        )
    }

    fn invalid_value(unexpected: Unexpected, expected: &dyn Expected) -> Self {
        Self::new(
            Kind::InvalidValue(unexpected.to_string(), expected.to_string()),
            Position::new(0, 0),
        )
    }

    fn invalid_length(len: usize, expected: &dyn Expected) -> Self {
        Self::new(
            Kind::InvalidLength(len, expected.to_string()),
            Position::new(0, 0),
        )
    }

    fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
        Self::new(
            Kind::UnknownVariant(variant.to_owned(), expected),
            Position::new(0, 0),
        )
    }

    fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
        Self::new(
            Kind::UnknownField(field.to_owned(), expected),
            Position::new(0, 0),
        )
    }

    fn missing_field(field: &'static str) -> Self {
        Self::new(Kind::MissingField(field), Position::new(0, 0))
    }

    fn duplicate_field(field: &'static str) -> Self {
        Self::new(Kind::DuplicateField(field), Position::new(0, 0))
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(
            formatter,
            "{} at line {} column {}",
            self.kind,
            self.position.line(),
            self.position.column()
        )
    }
}

impl std::error::Error for Error {}

/// An alias for a [`Result`] with the error type [`Error`].
///
/// [`Result`]: std::result::Result
pub type Result<T> = core::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use super::{Error, Kind};
    use crate::de::Position;
    use serde::de::Error as SerdeError;
    use serde::de::Unexpected;

    #[test]
    fn end_of_file() {
        assert_eq!(
            format!("{}", Error::new(Kind::EndOfFile, Position::new(1, 2))),
            "unexpected end of file at line 1 column 2"
        );
    }

    #[test]
    fn expected_tag() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedTag, Position::new(2, 3))),
            "expected tag at line 2 column 3"
        );
    }

    #[test]
    fn unexpected_tag() {
        assert_eq!(
            format!("{}", Error::new(Kind::UnexpectedTag, Position::new(3, 4))),
            "unexpected tag at line 3 column 4"
        );
    }

    #[test]
    fn end_of_tag() {
        assert_eq!(
            format!("{}", Error::new(Kind::EndOfTag, Position::new(4, 5))),
            "unexpected end of tag at line 4 column 5"
        );
    }

    #[test]
    fn unexpected_values() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::UnexpectedValues, Position::new(5, 6))
            ),
            "unexpected values at line 5 column 6"
        );
    }

    #[test]
    fn unexpected_value() {
        assert_eq!(
            format!("{}", Error::new(Kind::UnexpectedValue, Position::new(6, 7))),
            "unexpected value at line 6 column 7"
        );
    }

    #[test]
    fn end_of_values() {
        assert_eq!(
            format!("{}", Error::new(Kind::EndOfValues, Position::new(7, 8))),
            "unexpected end of values at line 7 column 8"
        );
    }

    #[test]
    fn expected_bool() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedBool, Position::new(8, 9))),
            "expected bool at line 8 column 9"
        );
    }

    #[test]
    fn expected_i8() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedI8, Position::new(9, 10))),
            "expected i8 at line 9 column 10"
        );
    }

    #[test]
    fn expected_i16() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedI16, Position::new(10, 11))),
            "expected i16 at line 10 column 11"
        );
    }

    #[test]
    fn expected_i32() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedI32, Position::new(11, 12))),
            "expected i32 at line 11 column 12"
        );
    }

    #[test]
    fn expected_i64() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedI64, Position::new(12, 13))),
            "expected i64 at line 12 column 13"
        );
    }

    #[test]
    fn expected_i128() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedI128, Position::new(13, 14))),
            "expected i128 at line 13 column 14"
        );
    }

    #[test]
    fn expected_u8() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedU8, Position::new(14, 15))),
            "expected u8 at line 14 column 15"
        );
    }

    #[test]
    fn expected_u16() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedU16, Position::new(15, 16))),
            "expected u16 at line 15 column 16"
        );
    }

    #[test]
    fn expected_u32() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedU32, Position::new(16, 17))),
            "expected u32 at line 16 column 17"
        );
    }

    #[test]
    fn expected_u64() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedU64, Position::new(17, 18))),
            "expected u64 at line 17 column 18"
        );
    }

    #[test]
    fn expected_u128() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedU128, Position::new(18, 19))),
            "expected u128 at line 18 column 19"
        );
    }

    #[test]
    fn expected_f32() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedF32, Position::new(19, 20))),
            "expected f32 at line 19 column 20"
        );
    }

    #[test]
    fn expected_f64() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedF64, Position::new(20, 21))),
            "expected f64 at line 20 column 21"
        );
    }

    #[test]
    fn expected_char() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedChar, Position::new(21, 22))),
            "expected char at line 21 column 22"
        );
    }

    #[test]
    fn expected_string() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::ExpectedString, Position::new(22, 23))
            ),
            "expected string at line 22 column 23"
        );
    }

    #[test]
    fn expected_unit() {
        assert_eq!(
            format!("{}", Error::new(Kind::ExpectedUnit, Position::new(23, 24))),
            "expected unit value at line 23 column 24"
        );
    }

    #[test]
    fn expected_identifier() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::ExpectedIdentifier, Position::new(24, 25))
            ),
            "expected identifier at line 24 column 25"
        );
    }

    #[test]
    fn io() {
        assert_eq!(
            format!("{}", Error::new(Kind::Io, Position::new(25, 26))),
            "io error at line 25 column 26"
        );
    }

    #[test]
    fn custom() {
        let mut error = Error::custom("foo");
        error.set_position(Position::new(26, 27));

        assert_eq!(format!("{}", error), "foo at line 26 column 27");
    }

    #[test]
    fn invalid_type() {
        let mut error = Error::invalid_type(Unexpected::Bool(true), &"foo");
        error.set_position(Position::new(27, 28));

        assert_eq!(
            format!("{}", error),
            "invalid type: expected foo, found boolean `true` at line 27 column 28"
        );
    }

    #[test]
    fn invalid_value() {
        let mut error = Error::invalid_value(Unexpected::Bool(true), &"foo");
        error.set_position(Position::new(28, 29));

        assert_eq!(
            format!("{}", error),
            "invalid value: expected foo, found boolean `true` at line 28 column 29"
        );
    }

    #[test]
    fn invalid_length() {
        let mut error = Error::invalid_length(42, &"foo");
        error.set_position(Position::new(29, 30));

        assert_eq!(
            format!("{}", error),
            "invalid length 42, expected foo at line 29 column 30"
        );
    }

    #[test]
    fn unknown_variant() {
        static EXPECTED: &'static [&'static str] = &["foo", "bar"];
        let mut error = Error::unknown_variant("baz", EXPECTED);
        error.set_position(Position::new(30, 31));

        assert_eq!(
            format!("{}", error),
            "unknown variant baz, expected one of [\"foo\", \"bar\"] at line 30 column 31"
        );
    }

    #[test]
    fn unknown_field() {
        let mut error = Error::unknown_field("baz", &["foo", "bar"]);
        error.set_position(Position::new(31, 32));

        assert_eq!(
            format!("{}", error),
            "unknown field baz, expected one of [\"foo\", \"bar\"] at line 31 column 32"
        );
    }

    #[test]
    fn missing_field() {
        let mut error = Error::missing_field("foo");
        error.set_position(Position::new(32, 33));

        assert_eq!(
            format!("{}", error),
            "missing field foo at line 32 column 33"
        );
    }

    #[test]
    fn duplicate_field() {
        let mut error = Error::duplicate_field("foo");
        error.set_position(Position::new(33, 34));

        assert_eq!(
            format!("{}", error),
            "duplicate field foo at line 33 column 34"
        );
    }

    #[test]
    fn cannot_deserialize_as_self_describing() {
        assert_eq!(
            format!(
                "{}",
                Error::new(
                    Kind::CannotDeserializeAsSelfDescribing,
                    Position::new(34, 35)
                )
            ),
            "cannot deserialize as self-describing at line 34 column 35"
        );
    }

    #[test]
    fn cannot_deserialize_as_option_in_tuple() {
        assert_eq!(
            format!(
                "{}",
                Error::new(
                    Kind::CannotDeserializeAsOptionInTuple,
                    Position::new(35, 36)
                )
            ),
            "cannot deserialize as option in tuple at line 35 column 36"
        );
    }

    #[test]
    fn cannot_deserialize_as_seq_in_tuple() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::CannotDeserializeAsSeqInTuple, Position::new(36, 37))
            ),
            "cannot deserialize as seq in tuple at line 36 column 37"
        );
    }

    #[test]
    fn cannot_deserialize_as_map_in_tuple() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::CannotDeserializeAsMapInTuple, Position::new(37, 38))
            ),
            "cannot deserialize as map in tuple at line 37 column 38"
        );
    }

    #[test]
    fn cannot_deserialize_as_struct_in_tuple() {
        assert_eq!(
            format!(
                "{}",
                Error::new(
                    Kind::CannotDeserializeAsStructInTuple,
                    Position::new(38, 39)
                )
            ),
            "cannot deserialize as struct in tuple at line 38 column 39"
        );
    }

    #[test]
    fn cannot_deserialize_nested_struct() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::CannotDeserializeNestedStruct, Position::new(39, 40))
            ),
            "cannot deserialize nested struct at line 39 column 40"
        );
    }

    #[test]
    fn must_deserialize_struct_field_as_identifier() {
        assert_eq!(
            format!(
                "{}",
                Error::new(
                    Kind::MustDeserializeStructFieldAsIdentifier,
                    Position::new(40, 41)
                )
            ),
            "must deserialize struct field as identifier at line 40 column 41"
        );
    }

    #[test]
    fn cannot_deserialize_as_option_in_seq() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::CannotDeserializeAsOptionInSeq, Position::new(41, 42))
            ),
            "cannot deserialize as option in seq at line 41 column 42"
        );
    }

    #[test]
    fn cannot_deserialize_nested_seq() {
        assert_eq!(
            format!(
                "{}",
                Error::new(Kind::CannotDeserializeNestedSeq, Position::new(42, 43))
            ),
            "cannot deserialize nested seq at line 42 column 43"
        );
    }

    #[test]
    fn must_deserialize_enum_variant_as_identifier() {
        assert_eq!(
            format!(
                "{}",
                Error::new(
                    Kind::MustDeserializeEnumVariantAsIdentifier,
                    Position::new(43, 44)
                )
            ),
            "must deserialize enum variant as identifier at line 43 column 44"
        );
    }

    #[test]
    fn set_position() {
        let mut error = Error::new(Kind::EndOfFile, Position::new(0, 0));

        error.set_position(Position::new(1, 2));

        assert_eq!(error.position, Position::new(1, 2));
    }
}