1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::fmt::Display;

/// Represents all things that could go wrong
/// while trying to send an email.
#[derive(Debug)]
#[allow(dead_code)]
pub enum EmailError {
  Config(String),
  Request(Box<reqwest::Error>),
  Io(String),
  Other(String),
}

impl From<reqwest::Error> for EmailError {
  fn from(e: reqwest::Error) -> Self {
    Self::Request(Box::new(e))
  }
}

impl From<resend_rs::error::Error> for EmailError {
  fn from(value: resend_rs::error::Error) -> Self {
    match value {
      resend_rs::error::Error::ReqwestError(e) => Self::from(e),
      resend_rs::error::Error::ResendError(e) => Self::Other(e),
    }
  }
}

impl Display for EmailError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match &self {
      Self::Request(e) => write!(f, "{e}"),
      Self::Config(e) | Self::Io(e) | Self::Other(e) => write!(f, "{e}"),
    }
  }
}