Skip to content

Commit 9a141c1

Browse files
committed
Removed all floating point references in Display and Debug
1 parent af5f712 commit 9a141c1

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

src/de.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,34 @@ impl<'a> From<de::Unexpected<'a>> for Unexpected {
5353
}
5454
}
5555

56+
impl fmt::Display for Unexpected {
57+
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
58+
use self::Unexpected::*;
59+
match self {
60+
Bool(b) => write!(formatter, "boolean `{}`", b),
61+
Unsigned(i) => write!(formatter, "integer `{}`", i),
62+
Signed(i) => write!(formatter, "integer `{}`", i),
63+
Float => write!(formatter, "floating point"),
64+
Char(c) => write!(formatter, "character `{}`", c),
65+
Str(s) => write!(formatter, "string {:?}", s),
66+
Bytes(_) => write!(formatter, "byte array"),
67+
Unit => write!(formatter, "unit value"),
68+
Option => write!(formatter, "Option value"),
69+
NewtypeStruct => write!(formatter, "newtype struct"),
70+
Seq => write!(formatter, "sequence"),
71+
Map => write!(formatter, "map"),
72+
Enum => write!(formatter, "enum"),
73+
UnitVariant => write!(formatter, "unit variant"),
74+
NewtypeVariant => write!(formatter, "newtype variant"),
75+
TupleVariant => write!(formatter, "tuple variant"),
76+
StructVariant => write!(formatter, "struct variant"),
77+
Other(other) => formatter.write_str(other),
78+
}
79+
}
80+
}
81+
5682
impl Unexpected {
57-
pub fn to_unexpected<'a>(&'a self) -> de::Unexpected<'a> {
83+
pub fn to_unexpected(&self) -> de::Unexpected {
5884
match *self {
5985
Unexpected::Bool(v) => de::Unexpected::Bool(v),
6086
Unexpected::Unsigned(v) => de::Unexpected::Unsigned(v),
@@ -83,7 +109,7 @@ pub enum DeserializerError {
83109
Custom(String),
84110
InvalidType(Unexpected, String),
85111
InvalidValue(Unexpected, String),
86-
InvalidLength(usize, String),
112+
InvalidLength(u32, String),
87113
UnknownVariant(String, &'static [&'static str]),
88114
UnknownField(String, &'static [&'static str]),
89115
MissingField(&'static str),
@@ -104,7 +130,7 @@ impl de::Error for DeserializerError {
104130
}
105131

106132
fn invalid_length(len: usize, exp: &dyn de::Expected) -> Self {
107-
DeserializerError::InvalidLength(len, exp.to_string())
133+
DeserializerError::InvalidLength(len as u32, exp.to_string())
108134
}
109135

110136
fn unknown_variant(field: &str, expected: &'static [&'static str]) -> Self {
@@ -134,7 +160,9 @@ impl DeserializerError {
134160
DeserializerError::InvalidValue(ref unexp, ref exp) => {
135161
E::invalid_value(unexp.to_unexpected(), &&**exp)
136162
}
137-
DeserializerError::InvalidLength(len, ref exp) => E::invalid_length(len, &&**exp),
163+
DeserializerError::InvalidLength(len, ref exp) => {
164+
E::invalid_length(len as usize, &&**exp)
165+
}
138166
DeserializerError::UnknownVariant(ref field, exp) => E::unknown_variant(field, exp),
139167
DeserializerError::UnknownField(ref field, exp) => E::unknown_field(field, exp),
140168
DeserializerError::MissingField(field) => E::missing_field(field),
@@ -155,36 +183,26 @@ impl Error for DeserializerError {
155183

156184
impl fmt::Display for DeserializerError {
157185
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158-
match *self {
159-
DeserializerError::Custom(ref msg) => write!(f, "{}", msg),
160-
DeserializerError::InvalidType(ref unexp, ref exp) => {
161-
write!(
162-
f,
163-
"Invalid type {}. Expected {}",
164-
unexp.to_unexpected(),
165-
exp
166-
)
186+
match self {
187+
DeserializerError::Custom(msg) => write!(f, "{}", msg),
188+
DeserializerError::InvalidType(unexp, exp) => {
189+
write!(f, "Invalid type {}. Expected {}", unexp, exp)
167190
}
168-
DeserializerError::InvalidValue(ref unexp, ref exp) => {
169-
write!(
170-
f,
171-
"Invalid value {}. Expected {}",
172-
unexp.to_unexpected(),
173-
exp
174-
)
191+
DeserializerError::InvalidValue(unexp, exp) => {
192+
write!(f, "Invalid value {}. Expected {}", unexp, exp)
175193
}
176-
DeserializerError::InvalidLength(len, ref exp) => {
194+
DeserializerError::InvalidLength(len, exp) => {
177195
write!(f, "Invalid length {}. Expected {}", len, exp)
178196
}
179-
DeserializerError::UnknownVariant(ref field, exp) => {
197+
DeserializerError::UnknownVariant(field, exp) => {
180198
write!(
181199
f,
182200
"Unknown variant {}. Expected one of {}",
183201
field,
184202
exp.join(", ")
185203
)
186204
}
187-
DeserializerError::UnknownField(ref field, exp) => {
205+
DeserializerError::UnknownField(field, exp) => {
188206
write!(
189207
f,
190208
"Unknown field {}. Expected one of {}",
@@ -433,8 +451,8 @@ where
433451
};
434452

435453
let d = EnumDeserializer {
436-
variant: variant,
437-
value: value,
454+
variant,
455+
value,
438456
error: Default::default(),
439457
};
440458
visitor.visit_enum(d)

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl Hash for Value {
5252
Value::I64(v) => v.hash(hasher),
5353
Value::Char(v) => v.hash(hasher),
5454
Value::String(ref v) => v.hash(hasher),
55+
#[allow(clippy::unit_hash)]
5556
Value::Unit => ().hash(hasher),
5657
Value::Option(ref v) => v.hash(hasher),
5758
Value::Newtype(ref v) => v.hash(hasher),
@@ -107,7 +108,7 @@ impl Ord for Value {
107108
(&Value::Seq(ref v0), &Value::Seq(ref v1)) => v0.cmp(v1),
108109
(&Value::Map(ref v0), &Value::Map(ref v1)) => v0.cmp(v1),
109110
(&Value::Bytes(ref v0), &Value::Bytes(ref v1)) => v0.cmp(v1),
110-
(ref v0, ref v1) => v0.discriminant().cmp(&v1.discriminant()),
111+
(v0, v1) => v0.discriminant().cmp(&v1.discriminant()),
111112
}
112113
}
113114
}
@@ -206,7 +207,7 @@ fn ser_smoke_test() {
206207
c: Vec<bool>,
207208
}
208209

209-
let foo = Foo {
210+
let item = Foo {
210211
a: 15,
211212
b: "hello".into(),
212213
c: vec![true, false],
@@ -225,7 +226,7 @@ fn ser_smoke_test() {
225226
.collect(),
226227
);
227228

228-
let value = to_value(&foo).unwrap();
229+
let value = to_value(&item).unwrap();
229230
assert_eq!(expected, value);
230231
}
231232

@@ -261,9 +262,9 @@ fn serialize_from_enum() {
261262
let bar = Foo::Bar;
262263
assert_eq!(to_value(&bar).unwrap(), Value::String("Bar".into()));
263264

264-
let baz = Foo::Baz(1);
265+
let item = Foo::Baz(1);
265266
assert_eq!(
266-
to_value(&baz).unwrap(),
267+
to_value(&item).unwrap(),
267268
Value::Map(
268269
vec![(Value::String("Baz".into()), Value::U8(1))]
269270
.into_iter()
@@ -383,8 +384,8 @@ fn deserialize_newtype() {
383384
struct Foo(i32);
384385

385386
let input = Value::I32(5);
386-
let foo = Foo::deserialize(input).unwrap();
387-
assert_eq!(foo, Foo(5));
387+
let item = Foo::deserialize(input).unwrap();
388+
assert_eq!(item, Foo(5));
388389
}
389390

390391
#[test]

0 commit comments

Comments
 (0)