1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum CurrencyError {
8 #[error("Network error: {0}")]
10 Network(#[from] reqwest::Error),
11
12 #[error("JSON parsing error: {0}")]
14 Json(#[from] serde_json::Error),
15
16 #[error("API error: {message}")]
18 Api { message: String },
19
20 #[error("Invalid currency code: {code}")]
22 InvalidCurrency { code: String },
23
24 #[error("Invalid amount: {amount}")]
26 InvalidAmount { amount: f64 },
27
28 #[error("Configuration error: {message}")]
30 Configuration { message: String },
31
32 #[error("Conversion error: {message}")]
34 Conversion { message: String },
35}
36
37impl CurrencyError {
38 pub fn api<S: Into<String>>(message: S) -> Self {
40 CurrencyError::Api {
41 message: message.into(),
42 }
43 }
44
45 pub fn configuration<S: Into<String>>(message: S) -> Self {
47 CurrencyError::Configuration {
48 message: message.into(),
49 }
50 }
51
52 pub fn conversion<S: Into<String>>(message: S) -> Self {
54 CurrencyError::Conversion {
55 message: message.into(),
56 }
57 }
58
59 pub fn invalid_currency<S: Into<String>>(code: S) -> Self {
61 CurrencyError::InvalidCurrency { code: code.into() }
62 }
63
64 pub fn invalid_amount(amount: f64) -> Self {
66 CurrencyError::InvalidAmount { amount }
67 }
68}
69
70pub type Result<T> = std::result::Result<T, CurrencyError>;