diff --git a/Cargo.toml b/Cargo.toml index 0d5a38e..3305424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -143,6 +143,8 @@ optional = true [dependencies.serde] workspace = true optional = true +[dependencies.thiserror] +workspace = true [dev-dependencies.float-cmp] workspace = true diff --git a/src/error.rs b/src/error.rs index 7827c42..33496b8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,13 @@ +use thiserror::Error; + /// Indicates the transducer value is known to be impossible. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Error)] +#[error("invalid value")] pub struct InvalidValue; /// Indicates that the encoded data is not valid for the type. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Error)] +#[error("invalid encoding")] pub struct InvalidEncoding; /// An error that it is likely impossible to recover from. This error should only be created in /// situations where attempts to recover have already been attempted and have failed. Error handling @@ -12,11 +16,13 @@ pub struct InvalidEncoding; /// /// In many systems there can be a single function for handling any critical error as a critical /// error always means everything needs to be stopped. -#[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[derive(Copy, Clone, Eq, PartialEq, Debug, Error)] pub enum CriticalError { /// Critical communication failed and retries are either impossible or also failed. + #[error("critical communication failure")] Communication, - InvalidValue(InvalidValue), + #[error(transparent)] + InvalidValue(#[from] InvalidValue), } /// A state of this type may mean the program has encountered an error that prevents it from continuing to run