1use std::fmt;
4
5#[derive(Debug)]
7pub enum VectorError {
8 Db(String),
10 Nostr(String),
12 Crypto(String),
14 Network(String),
16 Io(std::io::Error),
18 NotInitialized(String),
20 Other(String),
22}
23
24impl fmt::Display for VectorError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 VectorError::Db(msg) => write!(f, "Database error: {}", msg),
28 VectorError::Nostr(msg) => write!(f, "Nostr error: {}", msg),
29 VectorError::Crypto(msg) => write!(f, "Crypto error: {}", msg),
30 VectorError::Network(msg) => write!(f, "Network error: {}", msg),
31 VectorError::Io(err) => write!(f, "I/O error: {}", err),
32 VectorError::NotInitialized(msg) => write!(f, "Not initialized: {}", msg),
33 VectorError::Other(msg) => write!(f, "{}", msg),
34 }
35 }
36}
37
38impl std::error::Error for VectorError {}
39
40#[derive(Debug)]
42pub enum DownloadError {
43 TooLarge(usize),
45 Unusable(String),
47 Unreachable(String),
49}
50
51impl fmt::Display for DownloadError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 DownloadError::TooLarge(cap) => write!(f, "attachment exceeds the {} cap", crate::crypto::format_bytes(*cap as u64)),
55 DownloadError::Unusable(msg) | DownloadError::Unreachable(msg) => write!(f, "{msg}"),
56 }
57 }
58}
59
60impl std::error::Error for DownloadError {}
61
62impl From<String> for VectorError {
63 fn from(s: String) -> Self {
64 VectorError::Other(s)
65 }
66}
67
68impl From<&str> for VectorError {
69 fn from(s: &str) -> Self {
70 VectorError::Other(s.to_string())
71 }
72}
73
74impl From<std::io::Error> for VectorError {
75 fn from(err: std::io::Error) -> Self {
76 VectorError::Io(err)
77 }
78}
79
80impl From<rusqlite::Error> for VectorError {
81 fn from(err: rusqlite::Error) -> Self {
82 VectorError::Db(err.to_string())
83 }
84}
85
86impl From<nostr_sdk::prelude::Error> for VectorError {
87 fn from(err: nostr_sdk::prelude::Error) -> Self {
88 VectorError::Nostr(err.to_string())
89 }
90}
91
92impl From<reqwest::Error> for VectorError {
93 fn from(err: reqwest::Error) -> Self {
94 VectorError::Network(err.to_string())
95 }
96}
97
98pub type Result<T> = std::result::Result<T, VectorError>;
100
101impl From<VectorError> for String {
103 fn from(err: VectorError) -> String {
104 err.to_string()
105 }
106}