feat: Introduce the unrankable error variant

This commit is contained in:
Clément Renault 2019-04-26 14:59:35 +02:00
parent 0547671246
commit 9023a12ad4
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 19 additions and 15 deletions

View File

@ -25,7 +25,7 @@ impl ser::Serializer for ConvertToNumber {
} }
fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> { fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> {
unimplemented!() Err(SerializerError::UnrankableType { type_name: "char" })
} }
fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> { fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
@ -73,25 +73,25 @@ impl ser::Serializer for ConvertToNumber {
} }
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> { fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "&[u8]" }) Err(SerializerError::UnrankableType { type_name: "&[u8]" })
} }
fn serialize_none(self) -> Result<Self::Ok, Self::Error> { fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "Option" }) Err(SerializerError::UnrankableType { type_name: "Option" })
} }
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error> fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
where T: Serialize, where T: Serialize,
{ {
Err(SerializerError::UnserializableType { type_name: "Option" }) Err(SerializerError::UnrankableType { type_name: "Option" })
} }
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "()" }) Err(SerializerError::UnrankableType { type_name: "()" })
} }
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "unit struct" }) Err(SerializerError::UnrankableType { type_name: "unit struct" })
} }
fn serialize_unit_variant( fn serialize_unit_variant(
@ -101,7 +101,7 @@ impl ser::Serializer for ConvertToNumber {
_variant: &'static str _variant: &'static str
) -> Result<Self::Ok, Self::Error> ) -> Result<Self::Ok, Self::Error>
{ {
Err(SerializerError::UnserializableType { type_name: "unit variant" }) Err(SerializerError::UnrankableType { type_name: "unit variant" })
} }
fn serialize_newtype_struct<T: ?Sized>( fn serialize_newtype_struct<T: ?Sized>(
@ -123,15 +123,15 @@ impl ser::Serializer for ConvertToNumber {
) -> Result<Self::Ok, Self::Error> ) -> Result<Self::Ok, Self::Error>
where T: Serialize, where T: Serialize,
{ {
Err(SerializerError::UnserializableType { type_name: "newtype variant" }) Err(SerializerError::UnrankableType { type_name: "newtype variant" })
} }
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "sequence" }) Err(SerializerError::UnrankableType { type_name: "sequence" })
} }
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "tuple" }) Err(SerializerError::UnrankableType { type_name: "tuple" })
} }
fn serialize_tuple_struct( fn serialize_tuple_struct(
@ -140,7 +140,7 @@ impl ser::Serializer for ConvertToNumber {
_len: usize _len: usize
) -> Result<Self::SerializeTupleStruct, Self::Error> ) -> Result<Self::SerializeTupleStruct, Self::Error>
{ {
Err(SerializerError::UnserializableType { type_name: "tuple struct" }) Err(SerializerError::UnrankableType { type_name: "tuple struct" })
} }
fn serialize_tuple_variant( fn serialize_tuple_variant(
@ -151,11 +151,11 @@ impl ser::Serializer for ConvertToNumber {
_len: usize _len: usize
) -> Result<Self::SerializeTupleVariant, Self::Error> ) -> Result<Self::SerializeTupleVariant, Self::Error>
{ {
Err(SerializerError::UnserializableType { type_name: "tuple variant" }) Err(SerializerError::UnrankableType { type_name: "tuple variant" })
} }
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(SerializerError::UnserializableType { type_name: "map" }) Err(SerializerError::UnrankableType { type_name: "map" })
} }
fn serialize_struct( fn serialize_struct(
@ -164,7 +164,7 @@ impl ser::Serializer for ConvertToNumber {
_len: usize _len: usize
) -> Result<Self::SerializeStruct, Self::Error> ) -> Result<Self::SerializeStruct, Self::Error>
{ {
Err(SerializerError::UnserializableType { type_name: "struct" }) Err(SerializerError::UnrankableType { type_name: "struct" })
} }
fn serialize_struct_variant( fn serialize_struct_variant(
@ -175,6 +175,6 @@ impl ser::Serializer for ConvertToNumber {
_len: usize _len: usize
) -> Result<Self::SerializeStructVariant, Self::Error> ) -> Result<Self::SerializeStructVariant, Self::Error>
{ {
Err(SerializerError::UnserializableType { type_name: "struct variant" }) Err(SerializerError::UnrankableType { type_name: "struct variant" })
} }
} }

View File

@ -35,6 +35,7 @@ pub enum SerializerError {
ParseNumberError(ParseNumberError), ParseNumberError(ParseNumberError),
UnserializableType { type_name: &'static str }, UnserializableType { type_name: &'static str },
UnindexableType { type_name: &'static str }, UnindexableType { type_name: &'static str },
UnrankableType { type_name: &'static str },
Custom(String), Custom(String),
} }
@ -61,6 +62,9 @@ impl fmt::Display for SerializerError {
SerializerError::UnindexableType { type_name } => { SerializerError::UnindexableType { type_name } => {
write!(f, "{} are not an indexable type", type_name) write!(f, "{} are not an indexable type", type_name)
}, },
SerializerError::UnrankableType { type_name } => {
write!(f, "{} types can not be used for ranking", type_name)
},
SerializerError::Custom(s) => f.write_str(s), SerializerError::Custom(s) => f.write_str(s),
} }
} }