bencode inspired tight self describing serialization format
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..6f70009
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,52 @@
+use std::fmt::Display;
+
+use thiserror::Error;
+
+use crate::serde::T;
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+// This is a bare-bones implementation. A real library would provide additional
+// information in its error type, for example the line and column at which the
+// error occurred, the byte offset into the input, or the current key being
+// processed.
+#[derive(Debug, Error)]
+pub enum Error {
+ #[error("{0}")]
+ Message(String),
+ #[error(transparent)]
+ Io(#[from] std::io::Error),
+ #[error("expected {expected:?}, found {found}")]
+ Expected { expected: T, found: u8 },
+ #[error(transparent)]
+ FromUtf8Error(#[from] std::string::FromUtf8Error),
+ #[error(transparent)]
+ Utf8Error(#[from] std::str::Utf8Error),
+ #[error("a length is required for most things")]
+ LenLess,
+ #[error("char {0} was not in the valid range of chars")]
+ NotChar(u32),
+ #[error("out of bounds index")]
+ OOB,
+ #[error("{0} is not a tag")]
+ NotTag(u8),
+ #[error("out of range")]
+ Overflow,
+}
+
+impl serde::ser::Error for Error {
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display,
+ {
+ Self::Message(msg.to_string())
+ }
+}
+impl serde::de::Error for Error {
+ fn custom<T>(msg: T) -> Self
+ where
+ T: Display,
+ {
+ Self::Message(msg.to_string())
+ }
+}