[bencode/de] Improved syntax error messages
This commit is contained in:
parent
00e84033b4
commit
6eb7f08388
@ -85,11 +85,13 @@ impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>
|
V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match self.peek_byte()? as char {
|
let b = self.peek_byte()? as char;
|
||||||
|
match b {
|
||||||
'i' => self.deserialize_i64(visitor),
|
'i' => self.deserialize_i64(visitor),
|
||||||
'l' => self.deserialize_seq(visitor),
|
'l' => self.deserialize_seq(visitor),
|
||||||
'd' => self.deserialize_map(visitor),
|
'd' => self.deserialize_map(visitor),
|
||||||
_ => Err(Error::Syntax),
|
'0'..='9' => self.deserialize_str(visitor),
|
||||||
|
_ => Err(Error::Syntax(format!("any: invalid start char: {}", b))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,14 +280,15 @@ impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
|||||||
where
|
where
|
||||||
V: Visitor<'de>
|
V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match self.peek_byte()? as char{
|
let b = self.peek_byte()? as char;
|
||||||
|
match b {
|
||||||
'l' => self.deserialize_seq(visitor),
|
'l' => self.deserialize_seq(visitor),
|
||||||
'0'..='9' => {
|
'0'..='9' => {
|
||||||
let str = self.parse_byte_string()?;
|
let str = self.parse_byte_string()?;
|
||||||
let ba = ByteAccess::new(self, str);
|
let ba = ByteAccess::new(self, str);
|
||||||
visitor.visit_seq(ba)
|
visitor.visit_seq(ba)
|
||||||
},
|
},
|
||||||
_ => Err(Error::Syntax),
|
_ => Err(Error::Syntax(format!("tuple: invalid start char: {}", b))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -397,12 +400,13 @@ impl<'a, 'de> SeqAccess<'de> for RegularAccess<'a, 'de> {
|
|||||||
where
|
where
|
||||||
T: DeserializeSeed<'de>
|
T: DeserializeSeed<'de>
|
||||||
{
|
{
|
||||||
match self.de.peek_byte()? as char {
|
let b = self.de.peek_byte()? as char;
|
||||||
|
match b {
|
||||||
'd' | 'i' | 'l' | '0'..='9' => {
|
'd' | 'i' | 'l' | '0'..='9' => {
|
||||||
seed.deserialize(&mut *self.de).map(Some)
|
seed.deserialize(&mut *self.de).map(Some)
|
||||||
},
|
},
|
||||||
'e' => Ok(None),
|
'e' => Ok(None),
|
||||||
_ => Err(Error::Syntax)
|
_ => Err(Error::Syntax(format!("seq: next_element: invalid start/end char: {}", b)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -427,11 +431,12 @@ impl<'a, 'de> MapAccess<'de> for RegularAccess<'a, 'de> {
|
|||||||
where
|
where
|
||||||
V: DeserializeSeed<'de>
|
V: DeserializeSeed<'de>
|
||||||
{
|
{
|
||||||
match self.de.peek_byte()? as char {
|
let b = self.de.peek_byte()? as char;
|
||||||
|
match b {
|
||||||
'd' | 'i' | 'l' | '0'..='9' => {
|
'd' | 'i' | 'l' | '0'..='9' => {
|
||||||
seed.deserialize(&mut *self.de)
|
seed.deserialize(&mut *self.de)
|
||||||
},
|
},
|
||||||
_ => Err(Error::Syntax)
|
_ => Err(Error::Syntax(format!("map: next_value: invalid start char: {}", b)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ pub enum Error {
|
|||||||
|
|
||||||
WontImplement,
|
WontImplement,
|
||||||
Eof,
|
Eof,
|
||||||
Syntax,
|
Syntax(String),
|
||||||
InvalidUtf8,
|
InvalidUtf8,
|
||||||
ExpectedBytes,
|
ExpectedBytes,
|
||||||
ExpectedBytesSep,
|
ExpectedBytesSep,
|
||||||
@ -48,8 +48,8 @@ impl Display for Error {
|
|||||||
Error::Message(msg) => formatter.write_str(msg),
|
Error::Message(msg) => formatter.write_str(msg),
|
||||||
Error::WontImplement => formatter.write_str("there is no reasonable way to (de)serialize this type"),
|
Error::WontImplement => formatter.write_str("there is no reasonable way to (de)serialize this type"),
|
||||||
Error::Eof => formatter.write_str("unexpected end of input"),
|
Error::Eof => formatter.write_str("unexpected end of input"),
|
||||||
Error::Syntax => formatter.write_str("syntax error"),
|
Error::Syntax(msg) => formatter.write_fmt(format_args!("syntax error: {}", msg)),
|
||||||
Error::InvalidUtf8 => formatter.write_str("could not decoded as UTF-8"),
|
Error::InvalidUtf8 => formatter.write_str("could not decode as UTF-8"),
|
||||||
Error::ExpectedBytes => formatter.write_str("expected byte string start char: any number"),
|
Error::ExpectedBytes => formatter.write_str("expected byte string start char: any number"),
|
||||||
Error::ExpectedBytesSep => formatter.write_str("expected byte separator char: ':'"),
|
Error::ExpectedBytesSep => formatter.write_str("expected byte separator char: ':'"),
|
||||||
Error::ExpectedInteger => formatter.write_str("expected integer start char 'i'"),
|
Error::ExpectedInteger => formatter.write_str("expected integer start char 'i'"),
|
||||||
|
Loading…
Reference in New Issue
Block a user