diff --git a/crates/cashu/src/nuts/nut02.rs b/crates/cashu/src/nuts/nut02.rs index 918800e3..8f036da8 100644 --- a/crates/cashu/src/nuts/nut02.rs +++ b/crates/cashu/src/nuts/nut02.rs @@ -2,33 +2,24 @@ // https://github.com/cashubtc/nuts/blob/main/02.md use std::collections::HashSet; -use std::fmt; use base64::engine::general_purpose; use base64::Engine as _; use bitcoin::hashes::{sha256, Hash}; use itertools::Itertools; use serde::{Deserialize, Serialize}; +use thiserror::Error; use super::nut01::Keys; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Error, PartialEq, Eq)] pub enum Error { - Base64(base64::DecodeError), + #[error("`{0}`")] + Base64(#[from] base64::DecodeError), + #[error("NUT01: ID length invalid")] Length, } -impl std::error::Error for Error {} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Length => write!(f, "NUT02: Id invalid length"), - Self::Base64(err) => write!(f, "NUT02: {:?}", err), - } - } -} - /// A keyset ID is an identifier for a specific keyset. It can be derived by /// anyone who knows the set of public keys of a mint. The keyset ID **CAN** /// be stored in a Cashu token such that the token can be used to identify diff --git a/crates/cashu/src/secret.rs b/crates/cashu/src/secret.rs index dc2a3bdb..d50437a7 100644 --- a/crates/cashu/src/secret.rs +++ b/crates/cashu/src/secret.rs @@ -5,14 +5,16 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; +use thiserror::Error; /// The secret data that allows spending ecash #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct Secret(String); -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { + #[error("Invalid secret length: `{0}`")] InvalidLength(u64), } diff --git a/crates/cashu/src/url.rs b/crates/cashu/src/url.rs index 8a01b1ef..5065822c 100644 --- a/crates/cashu/src/url.rs +++ b/crates/cashu/src/url.rs @@ -7,29 +7,15 @@ use core::fmt; use core::str::FromStr; use serde::{Deserialize, Serialize}; +use thiserror::Error; use url::{ParseError, Url}; /// Url Error -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Error, PartialEq, Eq)] pub enum Error { /// Url error - Url(ParseError), -} - -impl std::error::Error for Error {} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Url(e) => write!(f, "Url: {e}"), - } - } -} - -impl From for Error { - fn from(e: ParseError) -> Self { - Self::Url(e) - } + #[error("`{0}`")] + Url(#[from] ParseError), } /// Unchecked Url