error.rs (2989B)
1 use std::{fmt, io}; 2 3 #[derive(Debug, Clone, Copy, Eq, PartialEq)] 4 pub enum FilterError { 5 EmptyContactList, 6 } 7 8 #[derive(Debug, Eq, PartialEq, Copy, Clone)] 9 pub enum SubscriptionError { 10 //#[error("No active subscriptions")] 11 NoActive, 12 13 /// When a timeline has an unexpected number 14 /// of active subscriptions. Should only happen if there 15 /// is a bug in notedeck 16 //#[error("Unexpected subscription count")] 17 UnexpectedSubscriptionCount(i32), 18 } 19 20 impl Error { 21 pub fn unexpected_sub_count(c: i32) -> Self { 22 Error::SubscriptionError(SubscriptionError::UnexpectedSubscriptionCount(c)) 23 } 24 25 pub fn no_active_sub() -> Self { 26 Error::SubscriptionError(SubscriptionError::NoActive) 27 } 28 } 29 30 impl fmt::Display for SubscriptionError { 31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 32 match self { 33 Self::NoActive => write!(f, "No active subscriptions"), 34 Self::UnexpectedSubscriptionCount(c) => { 35 write!(f, "Unexpected subscription count: {}", c) 36 } 37 } 38 } 39 } 40 41 #[derive(Debug)] 42 pub enum Error { 43 SubscriptionError(SubscriptionError), 44 Filter(FilterError), 45 LoadFailed, 46 Io(io::Error), 47 Nostr(enostr::Error), 48 Ndb(nostrdb::Error), 49 Image(image::error::ImageError), 50 Generic(String), 51 } 52 53 impl Error { 54 pub fn empty_contact_list() -> Self { 55 Error::Filter(FilterError::EmptyContactList) 56 } 57 } 58 59 impl fmt::Display for FilterError { 60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 61 match self { 62 Self::EmptyContactList => { 63 write!(f, "empty contact list") 64 } 65 } 66 } 67 } 68 69 impl fmt::Display for Error { 70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 71 match self { 72 Self::SubscriptionError(e) => { 73 write!(f, "{e}") 74 } 75 Self::LoadFailed => { 76 write!(f, "load failed") 77 } 78 Self::Filter(e) => { 79 write!(f, "{e}") 80 } 81 Self::Nostr(e) => write!(f, "{e}"), 82 Self::Ndb(e) => write!(f, "{e}"), 83 Self::Image(e) => write!(f, "{e}"), 84 Self::Generic(e) => write!(f, "{e}"), 85 Self::Io(e) => write!(f, "{e}"), 86 } 87 } 88 } 89 90 impl From<String> for Error { 91 fn from(s: String) -> Self { 92 Error::Generic(s) 93 } 94 } 95 96 impl From<nostrdb::Error> for Error { 97 fn from(e: nostrdb::Error) -> Self { 98 Error::Ndb(e) 99 } 100 } 101 102 impl From<image::error::ImageError> for Error { 103 fn from(err: image::error::ImageError) -> Self { 104 Error::Image(err) 105 } 106 } 107 108 impl From<enostr::Error> for Error { 109 fn from(err: enostr::Error) -> Self { 110 Error::Nostr(err) 111 } 112 } 113 114 impl From<io::Error> for Error { 115 fn from(err: io::Error) -> Self { 116 Error::Io(err) 117 } 118 } 119 120 impl From<FilterError> for Error { 121 fn from(err: FilterError) -> Self { 122 Error::Filter(err) 123 } 124 }