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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
//! A Hjson deserializer.
//!
use {
crate::{
de_enum::*,
de_map::*,
de_number::*,
de_seq::*,
error::{
Error,
ErrorCode::{self, *},
Result,
},
utf8::*,
},
serde::de::{self, IntoDeserializer, Visitor},
};
/// The deserializer. You normally don't call it directly
/// but use the `from_str` function available at crate's level.
pub struct Deserializer<'de> {
// the complete string we received
src: &'de str,
// where we're at, in bytes
pos: usize,
// Make it possible to avoid reading a string as a quoteless
// string when a key map is waited for (for example in
// {
// key: value
// }
// ) so that the key doesn't go til the end of the line.
pub(crate) accept_quoteless_value: bool,
}
impl<'de> Deserializer<'de> {
pub fn from_str(src: &'de str) -> Self {
Deserializer {
src,
pos: 0,
accept_quoteless_value: true,
}
}
/// compute the number of lines and columns to current pos
#[cold]
fn location(&self) -> (usize, usize) {
let (mut line, mut col) = (1, 1);
for ch in self.src[..self.pos].chars() {
if ch == '\n' {
col = 1;
line += 1;
} else {
col += 1;
}
}
(line, col)
}
/// build a syntax error
#[cold]
pub(crate) fn err(&self, code: ErrorCode) -> Error {
let (line, col) = self.location();
// we'll show the next 15 chars in the error message
let at = self.input().chars().take(15).collect();
Error::Syntax {
line,
col,
code,
at,
}
}
/// convert a serde raised error into one with precise location
#[cold]
pub(crate) fn cook_err<T>(&self, err: Error) -> Result<T> {
match err {
Error::RawSerde(message) => {
let (line, col) = self.location();
// we have no real idea where Serde found the problem
// so we write the position but not the characters around
Err(Error::Serde {
line,
col,
message,
})
}
e => Err(e),
}
}
#[cold]
pub(crate) fn fail<T>(&self, code: ErrorCode) -> Result<T> {
Err(self.err(code))
}
/// return an error if there's more than just spaces
/// and comments in the remaining input
pub fn check_all_consumed(&mut self) -> Result<()> {
self.eat_shit().ok();
if self.input().is_empty() {
Ok(())
} else {
self.fail(TrailingCharacters)
}
}
/// what remains to be parsed (including the
/// character we peeked at, if any)
#[inline(always)]
pub(crate) fn input(&self) -> &'de str {
&self.src[self.pos..]
}
/// takes all remaining characters
#[inline(always)]
pub(crate) fn take_all(&mut self) -> &'de str {
let s = &self.src[self.pos..];
self.pos = self.src.len();
s
}
/// return the next code point and its byte size, without advancing the cursor
// adapted from https://doc.rust-lang.org/src/core/str/validations.rs.html
#[inline]
fn peek_code_point(&self) -> Result<(u32, usize)> {
let bytes = self.src.as_bytes();
if self.pos >= bytes.len() {
return self.fail(Eof);
}
// As we start from an already verified UTF8 str, and a valid position,
// we can safely assume the bytes here are consistent with an UTF8 string
let x = bytes[self.pos];
if x < 128 {
return Ok(((x as u32), 1));
}
// Decode from a byte combination out of: [[[x y] z] w]
let init = utf8_first_byte(x, 2);
// SAFETY bytes assumed valid utf8
let y = unsafe { *bytes.get_unchecked(self.pos+1) };
let mut ch = utf8_acc_cont_byte(init, y);
if x >= 0xE0 {
// [[x y z] w] case
// 5th bit in 0xE0 .. 0xEF is always clear, so `init` is still valid
let z = unsafe { *bytes.get_unchecked(self.pos+2) };
let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
ch = init << 12 | y_z;
if x >= 0xF0 {
// [x y z w] case
// use only the lower 3 bits of `init`
let w = unsafe { *bytes.get_unchecked(self.pos+3) };
ch = (init & 7) << 18 | utf8_acc_cont_byte(y_z, w);
Ok((ch, 4))
} else {
Ok((ch, 3))
}
} else {
Ok((ch, 2))
}
}
/// return the next byte (or an error on EOF).
/// There's no guarantee the byte is a whole char
#[inline]
pub(crate) fn peek_byte(&self) -> Result<u8> {
let bytes = self.src.as_bytes();
if self.pos >= bytes.len() {
self.fail(Eof)
} else {
Ok(bytes[self.pos])
}
}
/// Return the next byte (at position pos). As it advances the cursor,
/// caller MUST throw an error if the byte isn't a valid full character.
#[inline]
pub(crate) fn next_byte(&mut self) -> Result<u8> {
let bytes = self.src.as_bytes();
if self.pos >= bytes.len() {
self.fail(Eof)
} else {
let b = bytes[self.pos];
self.pos += 1;
Ok(b)
}
}
/// Look at the first character in the input without consuming it.
#[inline]
pub(crate) fn peek_char(&self) -> Result<char> {
self.peek_code_point()
.map(|(code, _)| unsafe { char::from_u32_unchecked(code) })
}
/// Consume the first character in the input.
#[inline]
pub(crate) fn next_char(&mut self) -> Result<char> {
let (code, len) = self.peek_code_point()?;
self.pos += len;
let ch = unsafe { char::from_u32_unchecked(code) };
Ok(ch)
}
/// read bytes_count bytes of a string.
///
/// The validity of pos + bytes_count as a valid UTF8 position must
/// have been checked before.
#[inline]
pub(crate) fn take_str(&mut self, bytes_count: usize) -> Result<&str> {
if self.src.len() >= self.pos + bytes_count {
let pos = self.pos;
self.pos += bytes_count;
Ok(&self.src[pos..pos + bytes_count])
} else {
self.fail(Eof)
}
}
/// if the next bytes are s, then advance its length and return true
/// otherwise return false.
/// We do a comparison with a &[u8] to avoid the risk of trying read
/// at arbitrary positions and fall between valid UTF8 positions
#[inline]
pub(crate) fn try_read(&mut self, s: &[u8]) -> bool {
#[allow(clippy::collapsible_if)]
if self.src.len() >= self.pos + s.len() {
if &self.src.as_bytes()[self.pos..self.pos + s.len()] == s {
self.pos += s.len();
return true;
}
}
false
}
/// return the `len` first bytes of the input, without checking anything
/// (assuming it has been done) nor consuming anything
#[inline]
pub(crate) fn start(&self, len: usize) -> &'de str {
&self.src[self.pos..self.pos + len]
}
/// remove the next character (which is assumed to be ch)
#[inline]
pub(crate) fn drop(&mut self, ch: char) {
self.advance(ch.len_utf8());
}
/// advance the cursor (assuming bytes_count is consistent with chars)
#[inline]
pub(crate) fn advance(&mut self, bytes_count: usize) {
self.pos += bytes_count;
}
/// tells whether the next tree bytes are `'''` which
/// is the start or end of a multiline string literal in Hjson
#[inline]
fn is_at_triple_quote(&self, offset: usize) -> bool {
self.src.len() >= self.pos + offset + 3
&& &self.src[offset + self.pos..offset + self.pos + 3] == "'''"
}
#[inline]
fn eat_line(&mut self) -> Result<()> {
self.accept_quoteless_value = true;
let bytes = self.src.as_bytes();
unsafe {
for i in self.pos..bytes.len() {
if *bytes.get_unchecked(i) == b'\n' {
self.advance(i - self.pos + 1);
return Ok(());
}
}
}
self.fail(Eof)
}
#[inline]
pub(crate) fn eat_until_star_slash(&mut self) -> Result<()> {
match self.input().find("*/") {
Some(len) => {
self.advance(len + 2);
Ok(())
}
None => self.fail(Eof),
}
}
/// advance until the first non space character and
/// return the number of eaten bytes in the last
/// line (which is the number of chars as the only
/// whitespaces in Hjson are 1 byte long)
pub(crate) fn eat_spaces(&mut self) -> Result<usize> {
let mut eaten_bytes = 0;
loop {
let b = self.peek_byte()?;
match b {
b'\n' => {
self.accept_quoteless_value = true;
self.advance(1);
eaten_bytes = 0;
}
b' ' | b'\t'| b'\x0C' | b'\r' => {
self.advance(1);
eaten_bytes += 1
}
_ => {
return Ok(eaten_bytes);
}
}
}
}
/// consume spaces, new lines, comments, and stop before
/// first interesting char
#[inline]
pub(crate) fn eat_shit(&mut self) -> Result<()> {
let mut last_is_slash = false;
loop {
match self.peek_byte()? {
b'#' => {
self.eat_line()?;
last_is_slash = false;
}
b'*' => {
if last_is_slash {
self.eat_until_star_slash()?;
} else {
self.advance(1);
}
last_is_slash = false;
}
b'/' => {
if last_is_slash {
self.eat_line()?;
last_is_slash = false;
} else {
self.advance(1);
last_is_slash = true;
}
}
b'\n' => {
self.accept_quoteless_value = true;
self.advance(1);
last_is_slash = false;
}
b' ' | b'\t'| b'\x0C' | b'\r' => { // Hjson whitespaces
self.advance(1);
last_is_slash = false;
}
_ => {
if last_is_slash {
// we don't consume the /: it's the start of a string
self.pos -= 1;
}
return Ok(());
}
}
}
}
pub(crate) fn eat_shit_and(&mut self, mut including: Option<char>) -> Result<()> {
let mut last_is_slash = false;
loop {
let ch = self.peek_char()?;
match ch {
'#' => {
self.eat_line()?;
last_is_slash = false;
}
'*' => {
if last_is_slash {
self.eat_until_star_slash()?;
} else {
self.advance(1);
}
last_is_slash = false;
}
'/' => {
if last_is_slash {
self.eat_line()?;
last_is_slash = false;
} else {
self.advance(1);
last_is_slash = true;
}
}
'\n' => {
self.accept_quoteless_value = true;
self.advance(1);
last_is_slash = false;
}
_ if including == Some(ch) => {
self.drop(ch);
including = None;
last_is_slash = false;
}
_ if ch.is_whitespace() => {
self.drop(ch);
last_is_slash = false;
}
_ => {
if last_is_slash {
self.pos -= 1;
}
return Ok(());
}
}
}
}
/// Parse the JSON identifier `true` or `false`.
fn parse_bool(&mut self) -> Result<bool> {
self.eat_shit()?;
if self.try_read(b"true") {
Ok(true)
} else if self.try_read(b"false") {
Ok(false)
} else {
self.fail(ExpectedBoolean)
}
}
/// read the characters of the coming integer, without parsing the
/// resulting string
#[inline]
fn read_integer(&mut self, unsigned: bool) -> Result<&'de str> {
// parsing could be done in the same loop but then I would have
// to handle overflow
self.eat_shit()?;
let bytes = self.src.as_bytes();
for (idx, b) in bytes.iter().skip(self.pos).enumerate() {
match b {
b'-' if unsigned => {
return self.fail(ExpectedPositiveInteger);
}
b'-' if idx > 0 => {
return self.fail(UnexpectedChar);
}
b'0'..=b'9' | b'-' => {
// if it's too long, this will be handled at conversion
}
_ => {
let s = self.start(idx);
self.advance(idx); // we keep the last char
return Ok(s);
}
}
}
Ok(self.take_all())
}
/// read the characters of the coming floating point number, without parsing
#[inline]
fn read_float(&mut self) -> Result<&'de str> {
self.eat_shit()?;
let bytes = &self.src.as_bytes()[self.pos..];
for (idx, b) in bytes.iter().enumerate() {
match b {
b'0'..=b'9' | b'-' | b'+' | b'.' | b'e' | b'E' => {
// if it's invalid, this will be handled at conversion
}
_ => {
let s = self.start(idx);
self.advance(idx); // we keep the last char
return Ok(s);
}
}
}
Ok(self.take_all())
}
/// Parse a string until the next unescaped quote
#[inline]
fn parse_quoted_string(&mut self) -> Result<String> {
let mut s = String::new();
let starting_quote = self.next_char()?;
loop {
let mut c = self.next_char()?;
if c == starting_quote {
break;
} else if c == '\\' {
c = match self.next_byte()? {
b'\"' => '\"',
b'\'' => '\'',
b'\\' => '\\',
b'/' => '/',
b'b' => '\x08', // why did they put this in JSON ?
b'f' => '\x0c', // and this one ?!
b'n' => '\n',
b'r' => '\r',
b't' => '\t',
b'u' => {
self.take_str(4).ok()
.and_then(|s| u32::from_str_radix(s, 16).ok())
.and_then(std::char::from_u32)
.ok_or_else(|| self.err(InvalidEscapeSequence))?
}
_ => {
return self.fail(InvalidEscapeSequence);
}
};
}
s.push(c);
}
Ok(s)
}
/// Parse a string until end of line
fn parse_quoteless_str(&mut self) -> Result<&'de str> {
for (idx, ch) in self.input().char_indices() {
if ch == '\r' || ch == '\n' {
let s = self.start(idx);
self.advance(idx + 1);
return Ok(s.trim_end());
}
}
Ok(self.take_all().trim_end())
}
/// Parse a string until the next triple quote.
fn parse_multiline_string(&mut self) -> Result<String> {
if !self.is_at_triple_quote(0) {
// We could probably assume the first three bytes
// are "'''" and can be dropped without check
return self.fail(ExpectedString);
}
self.advance(3);
self.eat_line()?;
// we count the spaces on the first line
let indent = self.eat_spaces()?;
let mut v = String::new();
let mut line_len = indent;
for (idx, ch) in self.input().char_indices() {
match ch {
'\'' if self.is_at_triple_quote(idx) => {
self.advance(idx + 3);
v.truncate(v.trim_end().len()); // trimming end
return Ok(v);
}
'\r' => {
// a \r not followed by a \n is probably not
// valid but I'm not sure an error would be
// more useful here than silently ignoring it
}
'\n' => {
v.push(ch);
line_len = 0;
}
_ => {
if line_len >= indent || !ch.is_whitespace() {
v.push(ch);
}
line_len += 1;
}
}
}
self.fail(Eof) // it's not legal to not have the triple quotes
}
/// Parse an identifier without quotes:
/// - map key
/// - enum variant
fn parse_quoteless_identifier(&mut self) -> Result<&'de str> {
self.eat_shit()?;
for (idx, ch) in self.input().char_indices() {
match ch {
',' | '[' | ']' | '{' | '}' | ':' | '\r'| '\n' => {
let s = self.start(idx);
self.advance(idx);
return Ok(s);
}
' ' | '\t' => {
let s = self.start(idx);
self.advance(idx + 1);
return Ok(s);
}
_ => {}
}
}
Ok(self.take_all())
}
/// parse a string which may be a value
/// (i.e. not an map key or variant identifier )
fn parse_string_value(&mut self) -> Result<String> {
self.eat_shit()?;
let b = self.peek_byte()?;
let v = match b {
b',' | b':' | b'[' | b']' | b'{' | b'}' => self.fail(UnexpectedChar),
b'\'' if self.is_at_triple_quote(0) => self.parse_multiline_string(),
b'"' | b'\'' => self.parse_quoted_string(),
_ => (if self.accept_quoteless_value {
self.parse_quoteless_str()
} else {
self.parse_quoteless_identifier()
})
.map(|s| s.to_string()),
};
self.accept_quoteless_value = true;
v
}
#[inline]
fn parse_identifier(&mut self) -> Result<String> {
self.eat_shit()?;
let b = self.peek_byte()?;
// we set accept_quoteless_value to true so that a quoteless
// string can be accepted *after* the current identifier
self.accept_quoteless_value = true;
let r = match b {
b',' | b':' | b'[' | b']' | b'{' | b'}' => self.fail(UnexpectedChar),
b'"' | b'\'' => self.parse_quoted_string(),
_ => self.parse_quoteless_identifier().map(|s| s.to_string())
};
r
}
/// Braceless Hjson: same than usual but not within { and },
/// can only be for the whole document
fn deserialize_braceless_map<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let mut map_reader = MapReader::braceless(self);
map_reader.braceless = true;
let value = match visitor.visit_map(map_reader) {
Ok(v) => v,
Err(e) => {
return self.cook_err(e);
}
};
Ok(value)
}
}
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
match self.peek_byte()? {
b'"' | b'\'' => self.deserialize_string(visitor),
b'0'..=b'9' | b'-' => {
let number = Number::read(self)?;
number.visit(self, visitor)
}
b'[' => self.deserialize_seq(visitor),
b'{' => self.deserialize_map(visitor),
_ => {
if self.try_read(b"null") {
return visitor.visit_none();
}
if self.try_read(b"true") {
return visitor.visit_bool(true);
}
if self.try_read(b"false") {
return visitor.visit_bool(false);
}
let s = self.parse_string_value()?;
visitor.visit_string(s)
}
}
}
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_bool(self.parse_bool()?)
}
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(false)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedI8)))?;
visitor.visit_i8(v)
}
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(false)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedI16)))?;
visitor.visit_i16(v)
}
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(false)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedI32)))?;
visitor.visit_i32(v)
}
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(false)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedI64)))?;
visitor.visit_i64(v)
}
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(true)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedU8)))?;
visitor.visit_u8(v)
}
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(true)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedU16)))?;
visitor.visit_u16(v)
}
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(true)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedU32)))?;
visitor.visit_u32(v)
}
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_integer(true)
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedU64)))?;
visitor.visit_u64(v)
}
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_float()
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedF32)))?;
visitor.visit_f32(v)
}
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let v = self
.read_float()
.and_then(|s| s.parse().map_err(|_| self.err(ExpectedF64)))?;
visitor.visit_f64(v)
}
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let c = self
.parse_string_value()
.and_then(|s| s.chars().next().ok_or_else(|| self.err(ExpectedSingleChar)))?;
visitor.visit_char(c)
}
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
// we can't always borrow strs from the source as it's not possible
// when there's an escape sequence. So str are parsed as strings.
self.deserialize_string(visitor)
}
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_string(self.parse_string_value()?)
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
if self.try_read(b"null") {
visitor.visit_none()
} else {
visitor.visit_some(self)
}
}
// In Serde, unit means an anonymous value containing no data.
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
if self.try_read(b"null") {
visitor.visit_unit()
} else {
self.fail(ExpectedNull)
}
}
// Unit struct means a named value containing no data.
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
visitor.visit_newtype_struct(self)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
if self.next_byte()? == b'[' {
let value = visitor.visit_seq(SeqReader::new(self))?;
if self.next_byte()? == b']' {
Ok(value)
} else {
self.fail(ExpectedArrayEnd)
}
} else {
self.fail(ExpectedArray)
}
}
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
let on_start = self.pos == 0;
if let Err(e) = self.eat_shit() {
if on_start && e.is_eof() {
return self.deserialize_braceless_map(visitor);
} else {
return Err(e);
}
}
if self.peek_byte()? == b'{' {
self.advance(1);
let value = match visitor.visit_map(MapReader::within_braces(self)) {
Ok(v) => v,
Err(e) => {
return self.cook_err(e);
}
};
self.eat_shit()?;
if self.next_byte()? == b'}' {
Ok(value)
} else {
self.fail(ExpectedMapEnd)
}
} else if on_start {
self.deserialize_braceless_map(visitor)
} else {
self.fail(ExpectedMap)
}
}
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_map(visitor)
}
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.eat_shit()?;
match self.peek_byte()? {
b'"' | b'\'' => {
// Visit a unit variant.
visitor.visit_enum(self.parse_quoted_string()?.into_deserializer())
}
b'{' => {
self.advance(1);
// Visit a newtype variant, tuple variant, or struct variant.
let value = visitor.visit_enum(EnumReader::new(self))?;
self.eat_shit()?;
if self.next_byte()? == b'}' {
Ok(value)
} else {
self.fail(ExpectedMapEnd)
}
}
_ => {
visitor.visit_enum(self.parse_quoteless_identifier()?.into_deserializer())
}
}
}
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_string(self.parse_identifier()?)
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_any(visitor)
}
}