notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

error.rs (2779B)


      1 //use nostr::prelude::secp256k1;
      2 use std::array::TryFromSliceError;
      3 use std::fmt;
      4 
      5 #[derive(Debug)]
      6 pub enum Error {
      7     Empty,
      8     DecodeFailed,
      9     HexDecodeFailed,
     10     InvalidBech32,
     11     InvalidByteSize,
     12     InvalidSignature,
     13     InvalidPublicKey,
     14     // Secp(secp256k1::Error),
     15     Json(serde_json::Error),
     16     Nostrdb(nostrdb::Error),
     17     Generic(String),
     18 }
     19 
     20 impl std::cmp::PartialEq for Error {
     21     fn eq(&self, other: &Self) -> bool {
     22         match (self, other) {
     23             (Error::Empty, Error::Empty) => true,
     24             (Error::DecodeFailed, Error::DecodeFailed) => true,
     25             (Error::HexDecodeFailed, Error::HexDecodeFailed) => true,
     26             (Error::InvalidSignature, Error::InvalidSignature) => true,
     27             (Error::InvalidByteSize, Error::InvalidByteSize) => true,
     28             (Error::InvalidPublicKey, Error::InvalidPublicKey) => true,
     29             // This is slightly wrong but whatevs
     30             (Error::Json(..), Error::Json(..)) => true,
     31             (Error::Generic(left), Error::Generic(right)) => left == right,
     32             (Error::Nostrdb(left), Error::Nostrdb(right)) => left == right,
     33             //(Error::Secp(left), Error::Secp(right)) => left == right,
     34             _ => false,
     35         }
     36     }
     37 }
     38 
     39 impl fmt::Display for Error {
     40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     41         match self {
     42             Self::Empty => write!(f, "message is empty"),
     43             Self::DecodeFailed => write!(f, "decoding failed"),
     44             Self::InvalidSignature => write!(f, "invalid signature"),
     45             Self::HexDecodeFailed => write!(f, "hex decoding failed"),
     46             Self::InvalidByteSize => write!(f, "invalid byte size"),
     47             Self::InvalidBech32 => write!(f, "invalid bech32 string"),
     48             Self::InvalidPublicKey => write!(f, "invalid public key"),
     49             //Self::Secp(e) => write!(f, "{e}"),
     50             Self::Json(e) => write!(f, "{e}"),
     51             Self::Nostrdb(e) => write!(f, "{e}"),
     52             Self::Generic(e) => write!(f, "{e}"),
     53         }
     54     }
     55 }
     56 
     57 impl std::cmp::Eq for Error {}
     58 
     59 impl From<String> for Error {
     60     fn from(s: String) -> Self {
     61         Error::Generic(s)
     62     }
     63 }
     64 
     65 impl From<TryFromSliceError> for Error {
     66     fn from(_e: TryFromSliceError) -> Self {
     67         Error::InvalidByteSize
     68     }
     69 }
     70 
     71 impl From<hex::FromHexError> for Error {
     72     fn from(_e: hex::FromHexError) -> Self {
     73         Error::HexDecodeFailed
     74     }
     75 }
     76 
     77 /*
     78 impl From<secp256k1::Error> for Error {
     79     fn from(e: secp256k1::Error) -> Self {
     80         Error::Secp(e)
     81     }
     82 }
     83 */
     84 
     85 impl From<serde_json::Error> for Error {
     86     fn from(e: serde_json::Error) -> Self {
     87         Error::Json(e)
     88     }
     89 }
     90 
     91 impl From<nostrdb::Error> for Error {
     92     fn from(e: nostrdb::Error) -> Self {
     93         Error::Nostrdb(e)
     94     }
     95 }