Better handling for deserializing invalid slices

This commit is contained in:
Luke Horsley 2018-04-08 12:16:58 +01:00
parent 033e46b1a2
commit 3bd1f4e348

View File

@ -3,12 +3,12 @@
extern crate serde; extern crate serde;
use self::serde::ser::{Serialize, Serializer}; use self::serde::ser::{Serialize, Serializer};
use self::serde::de::{Deserialize, DeserializeSeed, Deserializer, Visitor, EnumAccess, use self::serde::de::{Deserialize, DeserializeSeed, Deserializer, Visitor, EnumAccess,
VariantAccess, Error}; Unexpected, VariantAccess, Error};
use {Level, LevelFilter, LOG_LEVEL_NAMES}; use {Level, LevelFilter, LOG_LEVEL_NAMES};
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::{self, FromStr};
// The Deserialize impls are handwritten to be case insensitive using FromStr. // The Deserialize impls are handwritten to be case insensitive using FromStr.
@ -53,7 +53,10 @@ impl<'de> Deserialize<'de> for Level {
where where
E: Error, E: Error,
{ {
self.visit_str(&*String::from_utf8_lossy(value)) let variant = str::from_utf8(value)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
self.visit_str(variant)
} }
} }
@ -134,7 +137,10 @@ impl<'de> Deserialize<'de> for LevelFilter {
where where
E: Error, E: Error,
{ {
self.visit_str(&*String::from_utf8_lossy(value)) let variant = str::from_utf8(value)
.map_err(|_| Error::invalid_value(Unexpected::Bytes(value), &self))?;
self.visit_str(variant)
} }
} }
@ -176,7 +182,7 @@ impl<'de> Deserialize<'de> for LevelFilter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate serde_test; extern crate serde_test;
use self::serde_test::{Token, assert_tokens, assert_de_tokens, assert_de_tokens_error}; use self::serde_test::{assert_de_tokens, assert_de_tokens_error, assert_tokens, Token};
use {Level, LevelFilter}; use {Level, LevelFilter};