error.rs (3082B)
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 TimelineNotFound, 44 LoadFailed, 45 SubscriptionError(SubscriptionError), 46 Filter(FilterError), 47 Io(io::Error), 48 Nostr(enostr::Error), 49 Ndb(nostrdb::Error), 50 Image(image::error::ImageError), 51 Generic(String), 52 } 53 54 impl Error { 55 pub fn empty_contact_list() -> Self { 56 Error::Filter(FilterError::EmptyContactList) 57 } 58 } 59 60 impl fmt::Display for FilterError { 61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 62 match self { 63 Self::EmptyContactList => { 64 write!(f, "empty contact list") 65 } 66 } 67 } 68 } 69 70 impl fmt::Display for Error { 71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 72 match self { 73 Self::SubscriptionError(e) => { 74 write!(f, "{e}") 75 } 76 Self::TimelineNotFound => write!(f, "Timeline not found"), 77 Self::LoadFailed => { 78 write!(f, "load failed") 79 } 80 Self::Filter(e) => { 81 write!(f, "{e}") 82 } 83 Self::Nostr(e) => write!(f, "{e}"), 84 Self::Ndb(e) => write!(f, "{e}"), 85 Self::Image(e) => write!(f, "{e}"), 86 Self::Generic(e) => write!(f, "{e}"), 87 Self::Io(e) => write!(f, "{e}"), 88 } 89 } 90 } 91 92 impl From<String> for Error { 93 fn from(s: String) -> Self { 94 Error::Generic(s) 95 } 96 } 97 98 impl From<nostrdb::Error> for Error { 99 fn from(e: nostrdb::Error) -> Self { 100 Error::Ndb(e) 101 } 102 } 103 104 impl From<image::error::ImageError> for Error { 105 fn from(err: image::error::ImageError) -> Self { 106 Error::Image(err) 107 } 108 } 109 110 impl From<enostr::Error> for Error { 111 fn from(err: enostr::Error) -> Self { 112 Error::Nostr(err) 113 } 114 } 115 116 impl From<io::Error> for Error { 117 fn from(err: io::Error) -> Self { 118 Error::Io(err) 119 } 120 } 121 122 impl From<FilterError> for Error { 123 fn from(err: FilterError) -> Self { 124 Error::Filter(err) 125 } 126 }