diff options
author | Joey Sacchini <joey@sacchini.net> | 2020-09-30 14:50:23 -0400 |
---|---|---|
committer | Joey Sacchini <joey@sacchini.net> | 2020-09-30 14:50:23 -0400 |
commit | 1827ef6ae21798d1f8882f1be5d445b42efd4f9f (patch) | |
tree | c494d499ba5d81ae3e9792e8e808b74e37f8f8e1 /src | |
parent | 3b6ddcb69918984ea122350b580c962a69eea017 (diff) | |
download | mcproto-rs-1827ef6ae21798d1f8882f1be5d445b42efd4f9f.tar.gz mcproto-rs-1827ef6ae21798d1f8882f1be5d445b42efd4f9f.tar.bz2 mcproto-rs-1827ef6ae21798d1f8882f1be5d445b42efd4f9f.zip |
implement error correctly for the error types
Diffstat (limited to 'src')
-rw-r--r-- | src/deserialize.rs | 26 | ||||
-rw-r--r-- | src/serialize.rs | 19 |
2 files changed, 42 insertions, 3 deletions
diff --git a/src/deserialize.rs b/src/deserialize.rs index f87cb49..f6eca35 100644 --- a/src/deserialize.rs +++ b/src/deserialize.rs @@ -1,7 +1,7 @@ use crate::types::VarInt; use std::string::FromUtf8Error; +use std::fmt; -#[derive(Debug)] pub enum DeserializeErr { Eof, VarNumTooLong(Vec<u8>), @@ -15,6 +15,30 @@ pub enum DeserializeErr { FailedJsonDeserialize(String), } +impl fmt::Display for DeserializeErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use DeserializeErr::*; + match self { + Eof => f.write_str("unexpected eof"), + VarNumTooLong(data) => f.write_fmt(format_args!("var num is too long: data={:?}", data)), + NegativeLength(data) => f.write_fmt(format_args!("negative length encountered {:?}", data)), + BadStringEncoding(data) => f.write_fmt(format_args!("failed to decode string, utf error: {:?}", data)), + InvalidBool(value) => f.write_fmt(format_args!("could not decode boolean, unexpected byte: {:?}", value)), + NbtUnknownTagType(data) => f.write_fmt(format_args!("nbt: bad tag type {}", data)), + NbtBadLength(data) => f.write_fmt(format_args!("nbt: bad length {:?}", data)), + NbtInvalidStartTag(data) => f.write_fmt(format_args!("nbt: unexpected start tag id: {:?}", data)), + CannotUnderstandValue(data) => f.write_fmt(format_args!("cannot understand value: {:?}", data)), + FailedJsonDeserialize(data) => f.write_fmt(format_args!("failed to deserialize json: {:?}", data)), + } + } +} + +impl fmt::Debug for DeserializeErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + <dyn fmt::Display>::fmt(self, f) + } +} + impl<'b, R> Into<DeserializeResult<'b, R>> for DeserializeErr { #[inline] fn into(self) -> DeserializeResult<'b, R> { diff --git a/src/serialize.rs b/src/serialize.rs index 3239b66..65e2d80 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -1,7 +1,22 @@ -#[derive(Debug)] +use std::fmt; + pub enum SerializeErr { FailedJsonEncode(String), - InconsistentPlayerActions(String), +} + +impl fmt::Display for SerializeErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use SerializeErr::*; + match self { + FailedJsonEncode(data) => f.write_fmt(format_args!("failed to serialize json: {:?}", data)), + } + } +} + +impl fmt::Debug for SerializeErr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + <dyn fmt::Display>::fmt(self, f) + } } pub type SerializeResult = Result<(), SerializeErr>; |