aboutsummaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..d5d6f56
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,80 @@
+use std::fmt;
+use std::io;
+
+#[derive(Debug)]
+pub enum Error {
+ Io(io::Error),
+
+ /// Bad magic number in header.
+ BadMagic(u16),
+
+ /// Protocol version not supported.
+ UnsupportedVersion(u8),
+
+ /// Unknown message type byte.
+ UnknownMessageType(u8),
+
+ /// Packet or buffer too small for the operation.
+ BufferTooSmall,
+
+ /// Payload length doesn't match declared length.
+ PayloadMismatch,
+
+ /// Generic invalid message (malformed content).
+ InvalidMessage,
+
+ /// Not connected to the network.
+ NotConnected,
+
+ /// Operation timed out.
+ Timeout,
+
+ /// Signature verification failed.
+ BadSignature,
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Error::Io(e) => write!(f, "I/O error: {e}"),
+ Error::BadMagic(m) => {
+ write!(f, "bad magic: 0x{m:04x}")
+ }
+ Error::UnsupportedVersion(v) => {
+ write!(f, "unsupported version: {v}")
+ }
+ Error::UnknownMessageType(t) => {
+ write!(f, "unknown message type: 0x{t:02x}")
+ }
+ Error::BufferTooSmall => {
+ write!(f, "buffer too small")
+ }
+ Error::PayloadMismatch => {
+ write!(f, "payload length mismatch")
+ }
+ Error::InvalidMessage => {
+ write!(f, "invalid message")
+ }
+ Error::NotConnected => write!(f, "not connected"),
+ Error::Timeout => write!(f, "timeout"),
+ Error::BadSignature => {
+ write!(f, "signature verification failed")
+ }
+ }
+ }
+}
+
+impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Error::Io(e) => Some(e),
+ _ => None,
+ }
+ }
+}
+
+impl From<io::Error> for Error {
+ fn from(e: io::Error) -> Self {
+ Error::Io(e)
+ }
+}