Skip to content

Commit e542eff

Browse files
juntyrkvark
authored andcommitted
Serializer now also only returns Error(Code)
1 parent 46051dd commit e542eff

17 files changed

+280
-295
lines changed

src/de/id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use serde::de::{self, Visitor};
22

3-
use super::{Deserializer, ErrorCode};
4-
5-
type Result<T> = std::result::Result<T, ErrorCode>;
3+
use super::{Deserializer, Error, Result};
64

75
pub struct IdDeserializer<'a, 'b: 'a> {
86
d: &'a mut Deserializer<'b>,
@@ -15,7 +13,7 @@ impl<'a, 'b: 'a> IdDeserializer<'a, 'b> {
1513
}
1614

1715
impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut IdDeserializer<'a, 'b> {
18-
type Error = ErrorCode;
16+
type Error = Error;
1917

2018
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
2119
where

src/de/mod.rs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/// Deserialization module.
2-
pub use crate::error::{Error, ErrorCode, Position};
2+
pub use crate::error::{Error, Position, SpannedError};
33

44
use serde::de::{self, DeserializeSeed, Deserializer as SerdeError, Visitor};
55
use std::{borrow::Cow, io, str};
66

77
use self::{id::IdDeserializer, tag::TagDeserializer};
88
use crate::{
9+
error::{Result, SpannedResult},
910
extensions::Extensions,
1011
options::Options,
1112
parse::{AnyNum, Bytes, ParsedStr},
@@ -17,8 +18,6 @@ mod tag;
1718
mod tests;
1819
mod value;
1920

20-
type Result<T> = std::result::Result<T, ErrorCode>;
21-
2221
/// The RON deserializer.
2322
///
2423
/// If you just want to simply deserialize a value,
@@ -31,25 +30,19 @@ pub struct Deserializer<'de> {
3130
impl<'de> Deserializer<'de> {
3231
// Cannot implement trait here since output is tied to input lifetime 'de.
3332
#[allow(clippy::should_implement_trait)]
34-
pub fn from_str(input: &'de str) -> std::result::Result<Self, Error> {
33+
pub fn from_str(input: &'de str) -> SpannedResult<Self> {
3534
Self::from_str_with_options(input, Options::default())
3635
}
3736

38-
pub fn from_bytes(input: &'de [u8]) -> std::result::Result<Self, Error> {
37+
pub fn from_bytes(input: &'de [u8]) -> SpannedResult<Self> {
3938
Self::from_bytes_with_options(input, Options::default())
4039
}
4140

42-
pub fn from_str_with_options(
43-
input: &'de str,
44-
options: Options,
45-
) -> std::result::Result<Self, Error> {
41+
pub fn from_str_with_options(input: &'de str, options: Options) -> SpannedResult<Self> {
4642
Self::from_bytes_with_options(input.as_bytes(), options)
4743
}
4844

49-
pub fn from_bytes_with_options(
50-
input: &'de [u8],
51-
options: Options,
52-
) -> std::result::Result<Self, Error> {
45+
pub fn from_bytes_with_options(input: &'de [u8], options: Options) -> SpannedResult<Self> {
5346
let mut deserializer = Deserializer {
5447
bytes: Bytes::new(input)?,
5548
newtype_variant: false,
@@ -64,14 +57,14 @@ impl<'de> Deserializer<'de> {
6457
String::from_utf8_lossy(self.bytes.bytes())
6558
}
6659

67-
pub fn error(&self, code: ErrorCode) -> Error {
68-
self.bytes.error(code)
60+
pub fn span_error(&self, code: Error) -> SpannedError {
61+
self.bytes.span_error(code)
6962
}
7063
}
7164

7265
/// A convenience function for building a deserializer
7366
/// and deserializing a value of type `T` from a reader.
74-
pub fn from_reader<R, T>(rdr: R) -> std::result::Result<T, Error>
67+
pub fn from_reader<R, T>(rdr: R) -> SpannedResult<T>
7568
where
7669
R: io::Read,
7770
T: de::DeserializeOwned,
@@ -81,7 +74,7 @@ where
8174

8275
/// A convenience function for building a deserializer
8376
/// and deserializing a value of type `T` from a string.
84-
pub fn from_str<'a, T>(s: &'a str) -> std::result::Result<T, Error>
77+
pub fn from_str<'a, T>(s: &'a str) -> SpannedResult<T>
8578
where
8679
T: de::Deserialize<'a>,
8780
{
@@ -90,7 +83,7 @@ where
9083

9184
/// A convenience function for building a deserializer
9285
/// and deserializing a value of type `T` from bytes.
93-
pub fn from_bytes<'a, T>(s: &'a [u8]) -> std::result::Result<T, Error>
86+
pub fn from_bytes<'a, T>(s: &'a [u8]) -> SpannedResult<T>
9487
where
9588
T: de::Deserialize<'a>,
9689
{
@@ -106,7 +99,7 @@ impl<'de> Deserializer<'de> {
10699
if self.bytes.bytes().is_empty() {
107100
Ok(())
108101
} else {
109-
Err(ErrorCode::TrailingCharacters)
102+
Err(Error::TrailingCharacters)
110103
}
111104
}
112105

@@ -139,7 +132,7 @@ impl<'de> Deserializer<'de> {
139132
}
140133

141134
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
142-
type Error = ErrorCode;
135+
type Error = Error;
143136

144137
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
145138
where
@@ -203,7 +196,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
203196
b'.' => self.deserialize_f64(visitor),
204197
b'"' | b'r' => self.deserialize_string(visitor),
205198
b'\'' => self.deserialize_char(visitor),
206-
other => Err(ErrorCode::UnexpectedByte(other as char)),
199+
other => Err(Error::UnexpectedByte(other as char)),
207200
}
208201
}
209202

@@ -346,7 +339,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
346339

347340
match res {
348341
Ok(byte_buf) => visitor.visit_byte_buf(byte_buf),
349-
Err(err) => Err(ErrorCode::Base64Error(err)),
342+
Err(err) => Err(Error::Base64Error(err)),
350343
}
351344
}
352345

@@ -371,10 +364,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
371364
if self.bytes.consume(")") {
372365
Ok(v)
373366
} else {
374-
Err(ErrorCode::ExpectedOptionEnd)
367+
Err(Error::ExpectedOptionEnd)
375368
}
376369
} else {
377-
Err(ErrorCode::ExpectedOption)
370+
Err(Error::ExpectedOption)
378371
}
379372
}
380373

@@ -388,7 +381,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
388381

389382
visitor.visit_unit()
390383
} else {
391-
Err(ErrorCode::ExpectedUnit)
384+
Err(Error::ExpectedUnit)
392385
}
393386
}
394387

@@ -427,12 +420,12 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
427420
if self.bytes.consume(")") {
428421
Ok(value)
429422
} else {
430-
Err(ErrorCode::ExpectedStructLikeEnd)
423+
Err(Error::ExpectedStructLikeEnd)
431424
}
432425
} else if name.is_empty() {
433-
Err(ErrorCode::ExpectedStructLike)
426+
Err(Error::ExpectedStructLike)
434427
} else {
435-
Err(ErrorCode::ExpectedNamedStructLike(name))
428+
Err(Error::ExpectedNamedStructLike(name))
436429
}
437430
}
438431

@@ -449,10 +442,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
449442
if self.bytes.consume("]") {
450443
Ok(value)
451444
} else {
452-
Err(ErrorCode::ExpectedArrayEnd)
445+
Err(Error::ExpectedArrayEnd)
453446
}
454447
} else {
455-
Err(ErrorCode::ExpectedArray)
448+
Err(Error::ExpectedArray)
456449
}
457450
}
458451

@@ -470,10 +463,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
470463
if old_newtype_variant || self.bytes.consume(")") {
471464
Ok(value)
472465
} else {
473-
Err(ErrorCode::ExpectedStructLikeEnd)
466+
Err(Error::ExpectedStructLikeEnd)
474467
}
475468
} else {
476-
Err(ErrorCode::ExpectedStructLike)
469+
Err(Error::ExpectedStructLike)
477470
}
478471
}
479472

@@ -491,9 +484,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
491484
}
492485

493486
self.deserialize_tuple(len, visitor).map_err(|e| match e {
494-
ErrorCode::ExpectedStructLike if !name.is_empty() => {
495-
ErrorCode::ExpectedNamedStructLike(name)
496-
}
487+
Error::ExpectedStructLike if !name.is_empty() => Error::ExpectedNamedStructLike(name),
497488
e => e,
498489
})
499490
}
@@ -511,10 +502,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
511502
if self.bytes.consume("}") {
512503
Ok(value)
513504
} else {
514-
Err(ErrorCode::ExpectedMapEnd)
505+
Err(Error::ExpectedMapEnd)
515506
}
516507
} else {
517-
Err(ErrorCode::ExpectedMap)
508+
Err(Error::ExpectedMap)
518509
}
519510
}
520511

@@ -543,12 +534,12 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
543534
if old_newtype_variant || self.bytes.consume(")") {
544535
Ok(value)
545536
} else {
546-
Err(ErrorCode::ExpectedStructLikeEnd)
537+
Err(Error::ExpectedStructLikeEnd)
547538
}
548539
} else if name.is_empty() {
549-
Err(ErrorCode::ExpectedStructLike)
540+
Err(Error::ExpectedStructLike)
550541
} else {
551-
Err(ErrorCode::ExpectedNamedStructLike(name))
542+
Err(Error::ExpectedNamedStructLike(name))
552543
}
553544
}
554545

@@ -570,7 +561,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
570561
where
571562
V: Visitor<'de>,
572563
{
573-
visitor.visit_str(str::from_utf8(self.bytes.identifier()?).map_err(ErrorCode::from)?)
564+
visitor.visit_str(str::from_utf8(self.bytes.identifier()?).map_err(Error::from)?)
574565
}
575566

576567
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
@@ -608,13 +599,13 @@ impl<'a, 'de> CommaSeparated<'a, 'de> {
608599
// No trailing comma but terminator
609600
(false, false) => Ok(false),
610601
// No trailing comma or terminator
611-
(false, true) => Err(ErrorCode::ExpectedComma),
602+
(false, true) => Err(Error::ExpectedComma),
612603
}
613604
}
614605
}
615606

616607
impl<'de, 'a> de::SeqAccess<'de> for CommaSeparated<'a, 'de> {
617-
type Error = ErrorCode;
608+
type Error = Error;
618609

619610
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
620611
where
@@ -633,7 +624,7 @@ impl<'de, 'a> de::SeqAccess<'de> for CommaSeparated<'a, 'de> {
633624
}
634625

635626
impl<'de, 'a> de::MapAccess<'de> for CommaSeparated<'a, 'de> {
636-
type Error = ErrorCode;
627+
type Error = Error;
637628

638629
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
639630
where
@@ -666,7 +657,7 @@ impl<'de, 'a> de::MapAccess<'de> for CommaSeparated<'a, 'de> {
666657

667658
Ok(res)
668659
} else {
669-
Err(ErrorCode::ExpectedMapColon)
660+
Err(Error::ExpectedMapColon)
670661
}
671662
}
672663
}
@@ -682,7 +673,7 @@ impl<'a, 'de> Enum<'a, 'de> {
682673
}
683674

684675
impl<'de, 'a> de::EnumAccess<'de> for Enum<'a, 'de> {
685-
type Error = ErrorCode;
676+
type Error = Error;
686677
type Variant = Self;
687678

688679
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
@@ -698,7 +689,7 @@ impl<'de, 'a> de::EnumAccess<'de> for Enum<'a, 'de> {
698689
}
699690

700691
impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> {
701-
type Error = ErrorCode;
692+
type Error = Error;
702693

703694
fn unit_variant(self) -> Result<()> {
704695
Ok(())
@@ -728,10 +719,10 @@ impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> {
728719
if self.de.bytes.consume(")") {
729720
Ok(val)
730721
} else {
731-
Err(ErrorCode::ExpectedStructLikeEnd)
722+
Err(Error::ExpectedStructLikeEnd)
732723
}
733724
} else {
734-
Err(ErrorCode::ExpectedStructLike)
725+
Err(Error::ExpectedStructLike)
735726
}
736727
}
737728

src/de/tag.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use serde::de::{self, Visitor};
22

3-
use super::{Deserializer, ErrorCode};
4-
5-
type Result<T> = std::result::Result<T, ErrorCode>;
3+
use super::{Deserializer, Error, Result};
64

75
pub struct TagDeserializer<'a, 'b: 'a> {
86
d: &'a mut Deserializer<'b>,
@@ -15,7 +13,7 @@ impl<'a, 'b: 'a> TagDeserializer<'a, 'b> {
1513
}
1614

1715
impl<'a, 'b: 'a, 'c> de::Deserializer<'b> for &'c mut TagDeserializer<'a, 'b> {
18-
type Error = ErrorCode;
16+
type Error = Error;
1917

2018
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
2119
where

0 commit comments

Comments
 (0)