use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Invalid or missing API key")]
InvalidCredentials,
#[error("Email template ID is required")]
MissingTemplateId,
#[error("Recipient email address is required")]
MissingRecipientAddress,
#[error("Invalid API endpoint: {0}")]
InvalidEndpoint(String),
#[error("Connection to SendWithUs API failed")]
ConnectionFailed,
#[error("SendWithUs API rejected request: {0}")]
InvalidRequest(String),
#[error("SendWithUs API error: {status} - {message}")]
ApiError { status: u16, message: String },
#[error("API communication error: {0}")]
RequestFailed(#[from] reqwest::Error),
#[error("Data serialization error: {0}")]
SerializationFailed(#[from] serde_json::Error),
#[error("File access error: {0}")]
FileAccessFailed(#[from] std::io::Error),
#[error("Invalid SendWithUs API URL")]
InvalidApiUrl,
#[error("Unexpected error: {0}")]
Unexpected(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let error = Error::InvalidCredentials;
assert_eq!(error.to_string(), "Invalid or missing API key");
let error = Error::MissingTemplateId;
assert_eq!(error.to_string(), "Email template ID is required");
let error = Error::MissingRecipientAddress;
assert_eq!(error.to_string(), "Recipient email address is required");
let error = Error::InvalidEndpoint("custom/endpoint".to_string());
assert_eq!(error.to_string(), "Invalid API endpoint: custom/endpoint");
let error = Error::InvalidRequest("Invalid parameter".to_string());
assert_eq!(
error.to_string(),
"SendWithUs API rejected request: Invalid parameter"
);
let error = Error::ApiError {
status: 500,
message: "Server error".to_string(),
};
assert_eq!(
error.to_string(),
"SendWithUs API error: 500 - Server error"
);
let error = Error::Unexpected("Something unexpected".to_string());
assert_eq!(error.to_string(), "Unexpected error: Something unexpected");
let error = Error::InvalidApiUrl;
assert_eq!(error.to_string(), "Invalid SendWithUs API URL");
let error = Error::ConnectionFailed;
assert_eq!(error.to_string(), "Connection to SendWithUs API failed");
let error = Error::FileAccessFailed(std::io::Error::new(
std::io::ErrorKind::NotFound,
"File not found",
));
assert!(error.to_string().contains("File access error"));
let io_error = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "Connection refused");
let error = Error::FileAccessFailed(io_error);
assert!(error.to_string().contains("File access error"));
}
}