1use std::ops::Deref;
2use std::{char, cmp, io, str};
3
4#[cfg(feature = "raw_value")]
5use serde::de::Visitor;
6
7use iter::LineColIterator;
8
9use error::{Error, ErrorCode, Result};
10
11#[cfg(feature = "raw_value")]
12use raw::{BorrowedRawDeserializer, OwnedRawDeserializer};
13
14pub trait Read<'de>: private::Sealed {
21 #[doc(hidden)]
22 fn next(&mut self) -> Result<Option<u8>>;
23 #[doc(hidden)]
24 fn peek(&mut self) -> Result<Option<u8>>;
25
26 #[doc(hidden)]
28 fn discard(&mut self);
29
30 #[doc(hidden)]
38 fn position(&self) -> Position;
39
40 #[doc(hidden)]
48 fn peek_position(&self) -> Position;
49
50 #[doc(hidden)]
53 fn byte_offset(&self) -> usize;
54
55 #[doc(hidden)]
59 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
60
61 #[doc(hidden)]
68 fn parse_str_raw<'s>(
69 &'s mut self,
70 scratch: &'s mut Vec<u8>,
71 ) -> Result<Reference<'de, 's, [u8]>>;
72
73 #[doc(hidden)]
76 fn ignore_str(&mut self) -> Result<()>;
77
78 #[doc(hidden)]
81 fn decode_hex_escape(&mut self) -> Result<u16>;
82
83 #[cfg(feature = "raw_value")]
87 #[doc(hidden)]
88 fn begin_raw_buffering(&mut self);
89
90 #[cfg(feature = "raw_value")]
93 #[doc(hidden)]
94 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
95 where
96 V: Visitor<'de>;
97}
98
99pub struct Position {
100 pub line: usize,
101 pub column: usize,
102}
103
104pub enum Reference<'b, 'c, T: ?Sized + 'static> {
105 Borrowed(&'b T),
106 Copied(&'c T),
107}
108
109impl<'b, 'c, T: ?Sized + 'static> Deref for Reference<'b, 'c, T> {
110 type Target = T;
111
112 fn deref(&self) -> &Self::Target {
113 match *self {
114 Reference::Borrowed(b) => b,
115 Reference::Copied(c) => c,
116 }
117 }
118}
119
120pub struct IoRead<R>
122where
123 R: io::Read,
124{
125 iter: LineColIterator<io::Bytes<R>>,
126 ch: Option<u8>,
128 #[cfg(feature = "raw_value")]
129 raw_buffer: Option<Vec<u8>>,
130}
131
132pub struct SliceRead<'a> {
137 slice: &'a [u8],
138 index: usize,
140 #[cfg(feature = "raw_value")]
141 raw_buffering_start_index: usize,
142}
143
144pub struct StrRead<'a> {
148 delegate: SliceRead<'a>,
149 #[cfg(feature = "raw_value")]
150 data: &'a str,
151}
152
153mod private {
155 pub trait Sealed {}
156}
157
158impl<R> IoRead<R>
161where
162 R: io::Read,
163{
164 pub fn new(reader: R) -> Self {
166 #[cfg(not(feature = "raw_value"))]
167 {
168 IoRead {
169 iter: LineColIterator::new(reader.bytes()),
170 ch: None,
171 }
172 }
173 #[cfg(feature = "raw_value")]
174 {
175 IoRead {
176 iter: LineColIterator::new(reader.bytes()),
177 ch: None,
178 raw_buffer: None,
179 }
180 }
181 }
182}
183
184impl<R> private::Sealed for IoRead<R> where R: io::Read {}
185
186impl<R> IoRead<R>
187where
188 R: io::Read,
189{
190 fn parse_str_bytes<'s, T, F>(
191 &'s mut self,
192 scratch: &'s mut Vec<u8>,
193 validate: bool,
194 result: F,
195 ) -> Result<T>
196 where
197 T: 's,
198 F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
199 {
200 loop {
201 let ch = try!(next_or_eof(self));
202 if !ESCAPE[ch as usize] {
203 scratch.push(ch);
204 continue;
205 }
206 match ch {
207 b'"' => {
208 return result(self, scratch);
209 }
210 b'\\' => {
211 try!(parse_escape(self, scratch));
212 }
213 _ => {
214 if validate {
215 return error(self, ErrorCode::ControlCharacterWhileParsingString);
216 }
217 scratch.push(ch);
218 }
219 }
220 }
221 }
222}
223
224impl<'de, R> Read<'de> for IoRead<R>
225where
226 R: io::Read,
227{
228 #[inline]
229 fn next(&mut self) -> Result<Option<u8>> {
230 match self.ch.take() {
231 Some(ch) => {
232 #[cfg(feature = "raw_value")]
233 {
234 if let Some(ref mut buf) = self.raw_buffer {
235 buf.push(ch);
236 }
237 }
238 Ok(Some(ch))
239 }
240 None => match self.iter.next() {
241 Some(Err(err)) => Err(Error::io(err)),
242 Some(Ok(ch)) => {
243 #[cfg(feature = "raw_value")]
244 {
245 if let Some(ref mut buf) = self.raw_buffer {
246 buf.push(ch);
247 }
248 }
249 Ok(Some(ch))
250 }
251 None => Ok(None),
252 },
253 }
254 }
255
256 #[inline]
257 fn peek(&mut self) -> Result<Option<u8>> {
258 match self.ch {
259 Some(ch) => Ok(Some(ch)),
260 None => match self.iter.next() {
261 Some(Err(err)) => Err(Error::io(err)),
262 Some(Ok(ch)) => {
263 self.ch = Some(ch);
264 Ok(self.ch)
265 }
266 None => Ok(None),
267 },
268 }
269 }
270
271 #[cfg(not(feature = "raw_value"))]
272 #[inline]
273 fn discard(&mut self) {
274 self.ch = None;
275 }
276
277 #[cfg(feature = "raw_value")]
278 fn discard(&mut self) {
279 if let Some(ch) = self.ch.take() {
280 if let Some(ref mut buf) = self.raw_buffer {
281 buf.push(ch);
282 }
283 }
284 }
285
286 fn position(&self) -> Position {
287 Position {
288 line: self.iter.line(),
289 column: self.iter.col(),
290 }
291 }
292
293 fn peek_position(&self) -> Position {
294 self.position()
297 }
298
299 fn byte_offset(&self) -> usize {
300 match self.ch {
301 Some(_) => self.iter.byte_offset() - 1,
302 None => self.iter.byte_offset(),
303 }
304 }
305
306 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
307 self.parse_str_bytes(scratch, true, as_str)
308 .map(Reference::Copied)
309 }
310
311 fn parse_str_raw<'s>(
312 &'s mut self,
313 scratch: &'s mut Vec<u8>,
314 ) -> Result<Reference<'de, 's, [u8]>> {
315 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
316 .map(Reference::Copied)
317 }
318
319 fn ignore_str(&mut self) -> Result<()> {
320 loop {
321 let ch = try!(next_or_eof(self));
322 if !ESCAPE[ch as usize] {
323 continue;
324 }
325 match ch {
326 b'"' => {
327 return Ok(());
328 }
329 b'\\' => {
330 try!(ignore_escape(self));
331 }
332 _ => {
333 return error(self, ErrorCode::ControlCharacterWhileParsingString);
334 }
335 }
336 }
337 }
338
339 fn decode_hex_escape(&mut self) -> Result<u16> {
340 let mut n = 0;
341 for _ in 0..4 {
342 match decode_hex_val(try!(next_or_eof(self))) {
343 None => return error(self, ErrorCode::InvalidEscape),
344 Some(val) => {
345 n = (n << 4) + val;
346 }
347 }
348 }
349 Ok(n)
350 }
351
352 #[cfg(feature = "raw_value")]
353 fn begin_raw_buffering(&mut self) {
354 self.raw_buffer = Some(Vec::new());
355 }
356
357 #[cfg(feature = "raw_value")]
358 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
359 where
360 V: Visitor<'de>,
361 {
362 let raw = self.raw_buffer.take().unwrap();
363 let raw = String::from_utf8(raw).unwrap();
364 visitor.visit_map(OwnedRawDeserializer {
365 raw_value: Some(raw),
366 })
367 }
368}
369
370impl<'a> SliceRead<'a> {
373 pub fn new(slice: &'a [u8]) -> Self {
375 #[cfg(not(feature = "raw_value"))]
376 {
377 SliceRead {
378 slice: slice,
379 index: 0,
380 }
381 }
382 #[cfg(feature = "raw_value")]
383 {
384 SliceRead {
385 slice: slice,
386 index: 0,
387 raw_buffering_start_index: 0,
388 }
389 }
390 }
391
392 fn position_of_index(&self, i: usize) -> Position {
393 let mut position = Position { line: 1, column: 0 };
394 for ch in &self.slice[..i] {
395 match *ch {
396 b'\n' => {
397 position.line += 1;
398 position.column = 0;
399 }
400 _ => {
401 position.column += 1;
402 }
403 }
404 }
405 position
406 }
407
408 fn parse_str_bytes<'s, T: ?Sized, F>(
412 &'s mut self,
413 scratch: &'s mut Vec<u8>,
414 validate: bool,
415 result: F,
416 ) -> Result<Reference<'a, 's, T>>
417 where
418 T: 's,
419 F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
420 {
421 let mut start = self.index;
423
424 loop {
425 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
426 self.index += 1;
427 }
428 if self.index == self.slice.len() {
429 return error(self, ErrorCode::EofWhileParsingString);
430 }
431 match self.slice[self.index] {
432 b'"' => {
433 if scratch.is_empty() {
434 let borrowed = &self.slice[start..self.index];
437 self.index += 1;
438 return result(self, borrowed).map(Reference::Borrowed);
439 } else {
440 scratch.extend_from_slice(&self.slice[start..self.index]);
441 self.index += 1;
442 return result(self, scratch).map(Reference::Copied);
443 }
444 }
445 b'\\' => {
446 scratch.extend_from_slice(&self.slice[start..self.index]);
447 self.index += 1;
448 try!(parse_escape(self, scratch));
449 start = self.index;
450 }
451 _ => {
452 self.index += 1;
453 if validate {
454 return error(self, ErrorCode::ControlCharacterWhileParsingString);
455 }
456 }
457 }
458 }
459 }
460}
461
462impl<'a> private::Sealed for SliceRead<'a> {}
463
464impl<'a> Read<'a> for SliceRead<'a> {
465 #[inline]
466 fn next(&mut self) -> Result<Option<u8>> {
467 Ok(if self.index < self.slice.len() {
470 let ch = self.slice[self.index];
471 self.index += 1;
472 Some(ch)
473 } else {
474 None
475 })
476 }
477
478 #[inline]
479 fn peek(&mut self) -> Result<Option<u8>> {
480 Ok(if self.index < self.slice.len() {
483 Some(self.slice[self.index])
484 } else {
485 None
486 })
487 }
488
489 #[inline]
490 fn discard(&mut self) {
491 self.index += 1;
492 }
493
494 fn position(&self) -> Position {
495 self.position_of_index(self.index)
496 }
497
498 fn peek_position(&self) -> Position {
499 self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
502 }
503
504 fn byte_offset(&self) -> usize {
505 self.index
506 }
507
508 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
509 self.parse_str_bytes(scratch, true, as_str)
510 }
511
512 fn parse_str_raw<'s>(
513 &'s mut self,
514 scratch: &'s mut Vec<u8>,
515 ) -> Result<Reference<'a, 's, [u8]>> {
516 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
517 }
518
519 fn ignore_str(&mut self) -> Result<()> {
520 loop {
521 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
522 self.index += 1;
523 }
524 if self.index == self.slice.len() {
525 return error(self, ErrorCode::EofWhileParsingString);
526 }
527 match self.slice[self.index] {
528 b'"' => {
529 self.index += 1;
530 return Ok(());
531 }
532 b'\\' => {
533 self.index += 1;
534 try!(ignore_escape(self));
535 }
536 _ => {
537 return error(self, ErrorCode::ControlCharacterWhileParsingString);
538 }
539 }
540 }
541 }
542
543 fn decode_hex_escape(&mut self) -> Result<u16> {
544 if self.index + 4 > self.slice.len() {
545 self.index = self.slice.len();
546 return error(self, ErrorCode::EofWhileParsingString);
547 }
548
549 let mut n = 0;
550 for _ in 0..4 {
551 let ch = decode_hex_val(self.slice[self.index]);
552 self.index += 1;
553 match ch {
554 None => return error(self, ErrorCode::InvalidEscape),
555 Some(val) => {
556 n = (n << 4) + val;
557 }
558 }
559 }
560 Ok(n)
561 }
562
563 #[cfg(feature = "raw_value")]
564 fn begin_raw_buffering(&mut self) {
565 self.raw_buffering_start_index = self.index;
566 }
567
568 #[cfg(feature = "raw_value")]
569 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
570 where
571 V: Visitor<'a>,
572 {
573 let raw = &self.slice[self.raw_buffering_start_index..self.index];
574 let raw = str::from_utf8(raw).unwrap();
575 visitor.visit_map(BorrowedRawDeserializer {
576 raw_value: Some(raw),
577 })
578 }
579}
580
581impl<'a> StrRead<'a> {
584 pub fn new(s: &'a str) -> Self {
586 #[cfg(not(feature = "raw_value"))]
587 {
588 StrRead {
589 delegate: SliceRead::new(s.as_bytes()),
590 }
591 }
592 #[cfg(feature = "raw_value")]
593 {
594 StrRead {
595 delegate: SliceRead::new(s.as_bytes()),
596 data: s,
597 }
598 }
599 }
600}
601
602impl<'a> private::Sealed for StrRead<'a> {}
603
604impl<'a> Read<'a> for StrRead<'a> {
605 #[inline]
606 fn next(&mut self) -> Result<Option<u8>> {
607 self.delegate.next()
608 }
609
610 #[inline]
611 fn peek(&mut self) -> Result<Option<u8>> {
612 self.delegate.peek()
613 }
614
615 #[inline]
616 fn discard(&mut self) {
617 self.delegate.discard();
618 }
619
620 fn position(&self) -> Position {
621 self.delegate.position()
622 }
623
624 fn peek_position(&self) -> Position {
625 self.delegate.peek_position()
626 }
627
628 fn byte_offset(&self) -> usize {
629 self.delegate.byte_offset()
630 }
631
632 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
633 self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
634 Ok(unsafe { str::from_utf8_unchecked(bytes) })
637 })
638 }
639
640 fn parse_str_raw<'s>(
641 &'s mut self,
642 scratch: &'s mut Vec<u8>,
643 ) -> Result<Reference<'a, 's, [u8]>> {
644 self.delegate.parse_str_raw(scratch)
645 }
646
647 fn ignore_str(&mut self) -> Result<()> {
648 self.delegate.ignore_str()
649 }
650
651 fn decode_hex_escape(&mut self) -> Result<u16> {
652 self.delegate.decode_hex_escape()
653 }
654
655 #[cfg(feature = "raw_value")]
656 fn begin_raw_buffering(&mut self) {
657 self.delegate.begin_raw_buffering()
658 }
659
660 #[cfg(feature = "raw_value")]
661 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
662 where
663 V: Visitor<'a>,
664 {
665 let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
666 visitor.visit_map(BorrowedRawDeserializer {
667 raw_value: Some(raw),
668 })
669 }
670}
671
672static ESCAPE: [bool; 256] = {
677 const CT: bool = true; const QU: bool = true; const BS: bool = true; const __: bool = false; [
682 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, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
700};
701
702fn next_or_eof<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<u8> {
703 match try!(read.next()) {
704 Some(b) => Ok(b),
705 None => error(read, ErrorCode::EofWhileParsingString),
706 }
707}
708
709fn error<'de, R: ?Sized + Read<'de>, T>(read: &R, reason: ErrorCode) -> Result<T> {
710 let position = read.position();
711 Err(Error::syntax(reason, position.line, position.column))
712}
713
714fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
715 str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
716}
717
718fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()> {
721 let ch = try!(next_or_eof(read));
722
723 match ch {
724 b'"' => scratch.push(b'"'),
725 b'\\' => scratch.push(b'\\'),
726 b'/' => scratch.push(b'/'),
727 b'b' => scratch.push(b'\x08'),
728 b'f' => scratch.push(b'\x0c'),
729 b'n' => scratch.push(b'\n'),
730 b'r' => scratch.push(b'\r'),
731 b't' => scratch.push(b'\t'),
732 b'u' => {
733 let c = match try!(read.decode_hex_escape()) {
734 0xDC00...0xDFFF => {
735 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
736 }
737
738 n1 @ 0xD800...0xDBFF => {
741 if try!(next_or_eof(read)) != b'\\' {
742 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
743 }
744 if try!(next_or_eof(read)) != b'u' {
745 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
746 }
747
748 let n2 = try!(read.decode_hex_escape());
749
750 if n2 < 0xDC00 || n2 > 0xDFFF {
751 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
752 }
753
754 let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
755
756 match char::from_u32(n) {
757 Some(c) => c,
758 None => {
759 return error(read, ErrorCode::InvalidUnicodeCodePoint);
760 }
761 }
762 }
763
764 n => match char::from_u32(n as u32) {
765 Some(c) => c,
766 None => {
767 return error(read, ErrorCode::InvalidUnicodeCodePoint);
768 }
769 },
770 };
771
772 scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
773 }
774 _ => {
775 return error(read, ErrorCode::InvalidEscape);
776 }
777 }
778
779 Ok(())
780}
781
782fn ignore_escape<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<()> {
785 let ch = try!(next_or_eof(read));
786
787 match ch {
788 b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
789 b'u' => {
790 let n = match try!(read.decode_hex_escape()) {
791 0xDC00...0xDFFF => {
792 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
793 }
794
795 n1 @ 0xD800...0xDBFF => {
798 if try!(next_or_eof(read)) != b'\\' {
799 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
800 }
801 if try!(next_or_eof(read)) != b'u' {
802 return error(read, ErrorCode::UnexpectedEndOfHexEscape);
803 }
804
805 let n2 = try!(read.decode_hex_escape());
806
807 if n2 < 0xDC00 || n2 > 0xDFFF {
808 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
809 }
810
811 (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000
812 }
813
814 n => n as u32,
815 };
816
817 if char::from_u32(n).is_none() {
818 return error(read, ErrorCode::InvalidUnicodeCodePoint);
819 }
820 }
821 _ => {
822 return error(read, ErrorCode::InvalidEscape);
823 }
824 }
825
826 Ok(())
827}
828
829static HEX: [u8; 256] = {
830 const __: u8 = 255; [
832 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
850};
851
852fn decode_hex_val(val: u8) -> Option<u16> {
853 let n = HEX[val as usize] as u16;
854 if n == 255 {
855 None
856 } else {
857 Some(n)
858 }
859}