فهرست منبع

improve: 'cashu' use thiserror for 'NUT02', 'SECRET' and 'URL'

thesimplekid 1 سال پیش
والد
کامیت
99dd07ed0b
3فایلهای تغییر یافته به همراه12 افزوده شده و 33 حذف شده
  1. 5 14
      crates/cashu/src/nuts/nut02.rs
  2. 3 1
      crates/cashu/src/secret.rs
  3. 4 18
      crates/cashu/src/url.rs

+ 5 - 14
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

+ 3 - 1
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),
 }
 

+ 4 - 18
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<ParseError> for Error {
-    fn from(e: ParseError) -> Self {
-        Self::Url(e)
-    }
+    #[error("`{0}`")]
+    Url(#[from] ParseError),
 }
 
 /// Unchecked Url