1#![allow(clippy::zero_prefixed_literal)]
2
3use crate::error::{Error, ErrorCode, Result};
4use core::cmp;
7use core::ops::Deref;
8use core::str;
9use debug_unsafe::slice::SliceGetter;
10
11#[cfg(feature = "std")]
12use super::iter::LineColIterator;
13#[cfg(feature = "std")]
14use crate::io;
15#[cfg(feature = "raw_value")]
16use crate::raw::BorrowedRawDeserializer;
17#[cfg(all(feature = "raw_value", feature = "std"))]
18use crate::raw::OwnedRawDeserializer;
19#[cfg(feature = "raw_value")]
20use serde::de::Visitor;
21
22pub trait Read<'de>: private::Sealed {
29 #[doc(hidden)]
30 fn next(&mut self) -> Result<Option<u8>>;
31 #[doc(hidden)]
32 fn peek(&mut self) -> Result<Option<u8>>;
33
34 #[doc(hidden)]
36 fn discard(&mut self);
37
38 #[doc(hidden)]
46 fn position(&self) -> Position;
47
48 #[doc(hidden)]
56 fn peek_position(&self) -> Position;
57
58 #[doc(hidden)]
61 fn byte_offset(&self) -> usize;
62
63 fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str>;
64
65 fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]>;
66
67 fn parse_int_any_pos(&mut self) -> Result<u64>;
68
69 fn str_from_saved(&mut self) -> Result<&'de str>;
74
75 #[doc(hidden)]
79 fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>>;
80
81 #[doc(hidden)]
93 fn ignore_str(&mut self) -> Result<()>;
94
95 #[doc(hidden)]
98 fn decode_hex_escape(&mut self) -> Result<u16>;
99
100 #[cfg(feature = "raw_value")]
104 #[doc(hidden)]
105 fn begin_raw_buffering(&mut self);
106
107 #[cfg(feature = "raw_value")]
110 #[doc(hidden)]
111 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
112 where
113 V: Visitor<'de>;
114
115 #[doc(hidden)]
120 const SHOULD_EARLY_RETURN_IF_FAILED: bool;
121
122 #[doc(hidden)]
125 fn set_failed(&mut self, failed: &mut bool);
126
127 fn save_start(&mut self);
128 fn save_end(&mut self);
129 fn clear_saved(&mut self);
130 fn get_saved(&mut self) -> &'de [u8];
131 fn saved_is_empty(&self) -> bool;
132}
133
134pub struct Position {
135 pub line: usize,
136 pub column: usize,
137}
138
139pub enum Reference<'b, 'c, T>
140where
141 T: ?Sized + 'static,
142{
143 Borrowed(&'b T),
144 Copied(&'c T),
145}
146
147impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
148where
149 T: ?Sized + 'static,
150{
151 type Target = T;
152
153 fn deref(&self) -> &Self::Target {
154 match *self {
155 Reference::Borrowed(b) => b,
156 Reference::Copied(c) => c,
157 }
158 }
159}
160
161#[cfg(feature = "std")]
163#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
164pub struct IoRead<R>
165where
166 R: io::Read,
167{
168 iter: LineColIterator<io::Bytes<R>>,
169 ch: Option<u8>,
171 #[cfg(feature = "raw_value")]
172 raw_buffer: Option<Vec<u8>>,
173}
174
175pub struct SliceRead<'a> {
180 slice: &'a [u8],
181 index: usize,
183 #[cfg(feature = "raw_value")]
184 raw_buffering_start_index: usize,
185 save_start: usize,
186 save_end: usize,
187}
188
189pub struct StrRead<'a> {
193 delegate: SliceRead<'a>,
194 #[cfg(feature = "raw_value")]
195 data: &'a str,
196}
197
198mod private {
200 pub trait Sealed {}
201}
202
203#[cfg(feature = "std")]
206impl<R> IoRead<R>
207where
208 R: io::Read,
209{
210 pub fn new(reader: R) -> Self {
212 IoRead {
213 iter: LineColIterator::new(reader.bytes()),
214 ch: None,
215 #[cfg(feature = "raw_value")]
216 raw_buffer: None,
217 }
218 }
219}
220
221#[cfg(feature = "std")]
222impl<R> private::Sealed for IoRead<R> where R: io::Read {}
223
224#[cfg(feature = "std")]
225impl<R> IoRead<R>
226where
227 R: io::Read,
228{
229 fn parse_str_bytes<'s, T, F>(&'s mut self, validate: bool, result: F) -> Result<T>
230 where
231 T: 's,
232 F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
233 {
234 unimplemented!()
235 }
257}
258
259#[cfg(feature = "std")]
260impl<'de, R> Read<'de> for IoRead<R>
261where
262 R: io::Read,
263{
264 #[inline]
265 fn next(&mut self) -> Result<Option<u8>> {
266 match self.ch.take() {
267 Some(ch) => {
268 #[cfg(feature = "raw_value")]
269 {
270 if let Some(buf) = &mut self.raw_buffer {
271 buf.push(ch);
272 }
273 }
274 Ok(Some(ch))
275 }
276 None => match self.iter.next() {
277 Some(Err(err)) => Err(Error::io(err)),
278 Some(Ok(ch)) => {
279 #[cfg(feature = "raw_value")]
280 {
281 if let Some(buf) = &mut self.raw_buffer {
282 buf.push(ch);
283 }
284 }
285 Ok(Some(ch))
286 }
287 None => Ok(None),
288 },
289 }
290 }
291
292 #[inline]
293 fn peek(&mut self) -> Result<Option<u8>> {
294 match self.ch {
295 Some(ch) => Ok(Some(ch)),
296 None => match self.iter.next() {
297 Some(Err(err)) => Err(Error::io(err)),
298 Some(Ok(ch)) => {
299 self.ch = Some(ch);
300 Ok(self.ch)
301 }
302 None => Ok(None),
303 },
304 }
305 }
306
307 #[cfg(not(feature = "raw_value"))]
308 #[inline]
309 fn discard(&mut self) {
310 self.ch = None;
311 }
312
313 #[cfg(feature = "raw_value")]
314 fn discard(&mut self) {
315 if let Some(ch) = self.ch.take() {
316 if let Some(buf) = &mut self.raw_buffer {
317 buf.push(ch);
318 }
319 }
320 }
321
322 fn position(&self) -> Position {
323 Position {
324 line: self.iter.line(),
325 column: self.iter.col(),
326 }
327 }
328
329 #[inline]
330 fn peek_position(&self) -> Position {
331 self.position()
334 }
335
336 #[inline]
337 fn byte_offset(&self) -> usize {
338 match self.ch {
339 Some(_) => self.iter.byte_offset() - 1,
340 None => self.iter.byte_offset(),
341 }
342 }
343
344 #[inline]
345 fn read_str<'s>(&'s mut self, _len: usize) -> Result<&'de str> {
346 unimplemented!()
347 }
348
349 #[inline]
350 fn read_slice<'s>(&'s mut self, _len: usize) -> Result<&'de [u8]> {
351 unimplemented!()
352 }
353
354 #[inline]
355 fn parse_int_any_pos(&mut self) -> Result<u64> {
356 unimplemented!()
357 }
358
359 #[inline]
370 fn str_from_saved(&mut self) -> Result<&'de str> {
371 let saved = self.get_saved();
372 as_str(self, saved)
373 }
374
375 #[inline]
376 fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
377 self.parse_str_bytes(true, as_str).map(Reference::Copied)
378 }
379
380 fn ignore_str(&mut self) -> Result<()> {
387 loop {
388 let ch = next_or_eof(self)?;
389 if !ESCAPE[ch as usize] {
390 continue;
391 }
392 match ch {
393 b' ' | b'\n' | b'\t' | b'\r' => {
394 return Ok(());
395 }
396 _ => {
400 return error(self, ErrorCode::ControlCharacterWhileParsingString);
401 }
402 }
403 }
404 }
405
406 fn decode_hex_escape(&mut self) -> Result<u16> {
407 let mut n = 0;
408 for _ in 0..4 {
409 match decode_hex_val(next_or_eof(self)?) {
410 None => return error(self, ErrorCode::InvalidEscape),
411 Some(val) => {
412 n = (n << 4) + val;
413 }
414 }
415 }
416 Ok(n)
417 }
418
419 #[cfg(feature = "raw_value")]
420 fn begin_raw_buffering(&mut self) {
421 self.raw_buffer = Some(Vec::new());
422 }
423
424 #[cfg(feature = "raw_value")]
425 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
426 where
427 V: Visitor<'de>,
428 {
429 let raw = self.raw_buffer.take().unwrap();
430 let raw = match String::from_utf8(raw) {
431 Ok(raw) => raw,
432 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
433 };
434 visitor.visit_map(OwnedRawDeserializer {
435 raw_value: Some(raw),
436 })
437 }
438
439 const SHOULD_EARLY_RETURN_IF_FAILED: bool = true;
440
441 #[inline]
442 #[cold]
443 fn set_failed(&mut self, failed: &mut bool) {
444 *failed = true;
445 }
446
447 #[inline]
448 fn save_start(&mut self) {
449 unimplemented!()
450 }
451 #[inline]
452 fn save_end(&mut self) {
453 unimplemented!()
454 }
455 #[inline]
456 fn clear_saved(&mut self) {
457 unimplemented!()
458 }
459 #[inline]
460 fn get_saved(&mut self) -> &'de [u8] {
461 unimplemented!()
462 }
463 #[inline]
464 fn saved_is_empty(&self) -> bool {
465 unimplemented!()
466 }
467}
468
469impl<'de> SliceRead<'de> {
472 pub fn new(slice: &'de [u8]) -> Self {
474 SliceRead {
475 slice,
476 index: 0,
477 #[cfg(feature = "raw_value")]
478 raw_buffering_start_index: 0,
479 save_start: 0,
480 save_end: 0,
481 }
482 }
483
484 fn position_of_index(&self, i: usize) -> Position {
485 let mut position = Position { line: 1, column: 0 };
486 for ch in &self.slice[..i] {
487 match *ch {
488 b'\n' => {
489 position.line += 1;
490 position.column = 0;
491 }
492 _ => {
493 position.column += 1;
494 }
495 }
496 }
497 position
498 }
499
500 fn parse_str_bytes<'s, T, F>(
504 &'s mut self,
505 _validate: bool,
506 result: F,
507 ) -> Result<Reference<'de, 's, T>>
508 where
509 T: ?Sized + 's,
510 F: FnOnce(&'s Self, &'de [u8]) -> Result<&'de T>,
511 {
512 let start = self.index;
514
515 loop {
516 if self.index == self.slice.len() {
520 return error(self, ErrorCode::EofWhileParsingString);
521 }
522 match self.slice[self.index] {
523 b':' | b'{' | b'[' => {
524 let borrowed = &self.slice[start..self.index];
528 return result(self, borrowed).map(Reference::Borrowed);
530 }
536 _ => {
543 self.index += 1;
544 }
548 }
549 }
550 }
551}
552
553impl<'de> private::Sealed for SliceRead<'de> {}
554
555impl<'de> Read<'de> for SliceRead<'de> {
556 #[inline]
557 fn next(&mut self) -> Result<Option<u8>> {
558 Ok(if self.index < self.slice.len() {
561 let ch = self.slice[self.index];
562 self.index += 1;
563 Some(ch)
564 } else {
565 None
566 })
567 }
568
569 #[inline]
570 fn peek(&mut self) -> Result<Option<u8>> {
571 Ok(if self.index < self.slice.len() {
574 Some(self.slice[self.index])
575 } else {
576 None
577 })
578 }
579
580 #[inline]
581 fn discard(&mut self) {
582 self.index += 1;
583 }
584
585 #[inline]
586 fn position(&self) -> Position {
587 self.position_of_index(self.index)
588 }
589
590 #[inline]
591 fn peek_position(&self) -> Position {
592 self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
595 }
596
597 #[inline]
598 fn byte_offset(&self) -> usize {
599 self.index
600 }
601
602 #[inline]
603 fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
604 let start = self.index;
605 self.index += len;
606 as_str(self, &self.slice[start..self.index])
607 }
608
609 #[inline]
610 fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
611 let start = self.index;
612 self.index += len;
613 Ok(&self.slice[start..self.index])
614 }
615
616 #[inline]
617 fn parse_int_any_pos(&mut self) -> Result<u64> {
618 let (res, i) = atoi_simd::parse_any_pos(self.slice.get_safe_unchecked(self.index..))?;
619 self.index += i;
620 Ok(res)
621 }
622
623 #[inline]
653 fn str_from_saved(&mut self) -> Result<&'de str> {
654 let saved = self.get_saved();
655 as_str(self, saved)
656 }
657
658 #[inline]
659 fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
660 self.parse_str_bytes(true, as_str)
661 }
662
663 fn ignore_str(&mut self) -> Result<()> {
669 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
671 self.index += 1;
672 }
673 if self.index == self.slice.len() {
674 return error(self, ErrorCode::EofWhileParsingString);
675 }
676 match self.slice[self.index] {
677 b' ' | b'\n' | b'\t' | b'\r' => {
678 self.index += 1;
679 Ok(())
680 }
681 _ => error(self, ErrorCode::ControlCharacterWhileParsingString),
686 }
687 }
689
690 fn decode_hex_escape(&mut self) -> Result<u16> {
691 if self.index + 4 > self.slice.len() {
692 self.index = self.slice.len();
693 return error(self, ErrorCode::EofWhileParsingString);
694 }
695
696 let mut n = 0;
697 for _ in 0..4 {
698 let ch = decode_hex_val(self.slice[self.index]);
699 self.index += 1;
700 match ch {
701 None => return error(self, ErrorCode::InvalidEscape),
702 Some(val) => {
703 n = (n << 4) + val;
704 }
705 }
706 }
707 Ok(n)
708 }
709
710 #[cfg(feature = "raw_value")]
711 fn begin_raw_buffering(&mut self) {
712 self.raw_buffering_start_index = self.index;
713 }
714
715 #[cfg(feature = "raw_value")]
716 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
717 where
718 V: Visitor<'de>,
719 {
720 let raw = &self.slice[self.raw_buffering_start_index..self.index];
721 let raw = match str::from_utf8(raw) {
722 Ok(raw) => raw,
723 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
724 };
725 visitor.visit_map(BorrowedRawDeserializer {
726 raw_value: Some(raw),
727 })
728 }
729
730 const SHOULD_EARLY_RETURN_IF_FAILED: bool = false;
731
732 #[inline]
733 #[cold]
734 fn set_failed(&mut self, _failed: &mut bool) {
735 self.slice = &self.slice[..self.index];
736 }
737
738 #[inline]
739 fn save_start(&mut self) {
740 self.save_start = self.index;
741 }
742 #[inline]
743 fn save_end(&mut self) {
744 self.save_end = self.index;
745 }
746 #[inline]
747 fn clear_saved(&mut self) {
748 self.save_end = self.save_start;
749 }
750 #[inline]
751 fn get_saved(&mut self) -> &'de [u8] {
752 &self.slice[self.save_start..self.save_end]
753 }
754 #[inline]
755 fn saved_is_empty(&self) -> bool {
756 self.save_end == self.save_start
757 }
758}
759
760impl<'a> StrRead<'a> {
763 pub fn new(s: &'a str) -> Self {
765 StrRead {
766 delegate: SliceRead::new(s.as_bytes()),
767 #[cfg(feature = "raw_value")]
768 data: s,
769 }
770 }
771}
772
773impl<'de> private::Sealed for StrRead<'de> {}
774
775impl<'de> Read<'de> for StrRead<'de> {
776 #[inline]
777 fn next(&mut self) -> Result<Option<u8>> {
778 self.delegate.next()
779 }
780
781 #[inline]
782 fn peek(&mut self) -> Result<Option<u8>> {
783 self.delegate.peek()
784 }
785
786 #[inline]
787 fn discard(&mut self) {
788 self.delegate.discard();
789 }
790
791 #[inline]
792 fn position(&self) -> Position {
793 self.delegate.position()
794 }
795
796 #[inline]
797 fn peek_position(&self) -> Position {
798 self.delegate.peek_position()
799 }
800
801 #[inline]
802 fn byte_offset(&self) -> usize {
803 self.delegate.byte_offset()
804 }
805
806 #[inline]
807 fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
808 self.delegate.read_str(len)
809 }
810
811 #[inline]
812 fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
813 self.delegate.read_slice(len)
814 }
815
816 #[inline]
817 fn parse_int_any_pos(&mut self) -> Result<u64> {
818 self.delegate.parse_int_any_pos()
819 }
820
821 #[inline]
832 fn str_from_saved(&mut self) -> Result<&'de str> {
833 unsafe { Ok(str::from_utf8_unchecked(self.get_saved())) }
834 }
835
836 #[inline]
837 fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
838 self.delegate.parse_str_bytes(true, |_, bytes| {
839 Ok(unsafe { str::from_utf8_unchecked(bytes) })
843 })
844 }
845
846 #[inline]
851 fn ignore_str(&mut self) -> Result<()> {
852 self.delegate.ignore_str()
853 }
854
855 #[inline]
856 fn decode_hex_escape(&mut self) -> Result<u16> {
857 self.delegate.decode_hex_escape()
858 }
859
860 #[cfg(feature = "raw_value")]
861 #[inline]
862 fn begin_raw_buffering(&mut self) {
863 self.delegate.begin_raw_buffering();
864 }
865
866 #[cfg(feature = "raw_value")]
867 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
868 where
869 V: Visitor<'de>,
870 {
871 let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
872 visitor.visit_map(BorrowedRawDeserializer {
873 raw_value: Some(raw),
874 })
875 }
876
877 const SHOULD_EARLY_RETURN_IF_FAILED: bool = false;
878
879 #[inline]
880 #[cold]
881 fn set_failed(&mut self, failed: &mut bool) {
882 self.delegate.set_failed(failed);
883 }
884
885 #[inline]
886 fn save_start(&mut self) {
887 self.delegate.save_start()
888 }
889 #[inline]
890 fn save_end(&mut self) {
891 self.delegate.save_end()
892 }
893 #[inline]
894 fn clear_saved(&mut self) {
895 self.delegate.clear_saved()
896 }
897 #[inline]
898 fn get_saved(&mut self) -> &'de [u8] {
899 self.delegate.get_saved()
900 }
901 #[inline]
902 fn saved_is_empty(&self) -> bool {
903 self.delegate.saved_is_empty()
904 }
905}
906
907impl<'de, R> private::Sealed for &mut R where R: Read<'de> {}
910
911impl<'de, R> Read<'de> for &mut R
912where
913 R: Read<'de>,
914{
915 #[inline]
916 fn next(&mut self) -> Result<Option<u8>> {
917 R::next(self)
918 }
919
920 #[inline]
921 fn peek(&mut self) -> Result<Option<u8>> {
922 R::peek(self)
923 }
924
925 #[inline]
926 fn discard(&mut self) {
927 R::discard(self);
928 }
929
930 #[inline]
931 fn position(&self) -> Position {
932 R::position(self)
933 }
934
935 #[inline]
936 fn peek_position(&self) -> Position {
937 R::peek_position(self)
938 }
939
940 #[inline]
941 fn byte_offset(&self) -> usize {
942 R::byte_offset(self)
943 }
944
945 #[inline]
946 fn read_str<'s>(&'s mut self, len: usize) -> Result<&'de str> {
947 R::read_str(self, len)
948 }
949
950 #[inline]
951 fn read_slice<'s>(&'s mut self, len: usize) -> Result<&'de [u8]> {
952 R::read_slice(self, len)
953 }
954
955 #[inline]
956 fn parse_int_any_pos(&mut self) -> Result<u64> {
957 R::parse_int_any_pos(self)
958 }
959
960 #[inline]
971 fn str_from_saved(&mut self) -> Result<&'de str> {
972 R::str_from_saved(self)
973 }
974
975 #[inline]
976 fn parse_str<'s>(&'s mut self) -> Result<Reference<'de, 's, str>> {
977 R::parse_str(self)
978 }
979
980 #[inline]
986 fn ignore_str(&mut self) -> Result<()> {
987 R::ignore_str(self)
988 }
989
990 #[inline]
991 fn decode_hex_escape(&mut self) -> Result<u16> {
992 R::decode_hex_escape(self)
993 }
994
995 #[cfg(feature = "raw_value")]
996 #[inline]
997 fn begin_raw_buffering(&mut self) {
998 R::begin_raw_buffering(self);
999 }
1000
1001 #[cfg(feature = "raw_value")]
1002 #[inline]
1003 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
1004 where
1005 V: Visitor<'de>,
1006 {
1007 R::end_raw_buffering(self, visitor)
1008 }
1009
1010 const SHOULD_EARLY_RETURN_IF_FAILED: bool = R::SHOULD_EARLY_RETURN_IF_FAILED;
1011
1012 #[inline]
1013 fn set_failed(&mut self, failed: &mut bool) {
1014 R::set_failed(self, failed);
1015 }
1016
1017 #[inline]
1018 fn save_start(&mut self) {
1019 R::save_start(self)
1020 }
1021 #[inline]
1022 fn save_end(&mut self) {
1023 R::save_end(self)
1024 }
1025 #[inline]
1026 fn clear_saved(&mut self) {
1027 R::clear_saved(self)
1028 }
1029 #[inline]
1030 fn get_saved(&mut self) -> &'de [u8] {
1031 R::get_saved(self)
1032 }
1033 #[inline]
1034 fn saved_is_empty(&self) -> bool {
1035 R::saved_is_empty(self)
1036 }
1037}
1038
1039pub trait Fused: private::Sealed {}
1043impl<'a> Fused for SliceRead<'a> {}
1044impl<'a> Fused for StrRead<'a> {}
1045
1046static ESCAPE: [bool; 256] = {
1049 const CT: bool = true; const QU: bool = true; const BS: bool = true; const __: bool = false; [
1054 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
1072};
1073
1074#[inline]
1075fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
1076where
1077 R: ?Sized + Read<'de>,
1078{
1079 match read.next()? {
1080 Some(b) => Ok(b),
1081 None => error(read, ErrorCode::EofWhileParsingString),
1082 }
1083}
1084
1085fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
1097where
1098 R: ?Sized + Read<'de>,
1099{
1100 let position = read.position();
1101 Err(Error::syntax(reason, position.line, position.column))
1102}
1103
1104#[inline]
1105fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
1106 str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
1107}
1108
1109static HEX: [u8; 256] = {
1239 const __: u8 = 255; [
1241 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
1259};
1260
1261fn decode_hex_val(val: u8) -> Option<u16> {
1262 let n = HEX[val as usize] as u16;
1263 if n == 255 {
1264 None
1265 } else {
1266 Some(n)
1267 }
1268}