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

This commit is contained in:
thesimplekid
2023-11-10 18:51:34 +00:00
parent 660c767df4
commit 99dd07ed0b
3 changed files with 12 additions and 33 deletions

View File

@@ -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

View File

@@ -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),
}

View File

@@ -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