#[derive(Debug, Clone)]
pub struct SmtpResponse {
pub code: String,
pub message: String,
}
impl SmtpResponse {
pub fn new(code: &str, message: &str) -> Self {
Self {
code: code.to_string(),
message: message.to_string(),
}
}
pub fn ok() -> Self {
Self::new("250", "OK")
}
pub fn greeting() -> Self {
Self::new("220", "Welcome to MogiMail")
}
pub fn helo(hostname: &str, client_domain: &str) -> Self {
Self::new("250", &format!("{hostname} Hello {client_domain}"))
}
pub fn data_start() -> Self {
Self::new("354", "End data with <CR><LF>.<CR><LF>")
}
pub fn quit() -> Self {
Self::new("221", "Bye")
}
pub fn error(code: &str, message: &str) -> Self {
Self::new(code, message)
}
pub fn format(&self) -> String {
format!("{} {}\r\n", self.code, self.message)
}
pub fn is_success(&self) -> bool {
self.code.starts_with('2')
}
pub fn is_error(&self) -> bool {
self.code.starts_with('4') || self.code.starts_with('5')
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_response_creation() {
let response = SmtpResponse::new("250", "OK");
assert_eq!(response.code, "250");
assert_eq!(response.message, "OK");
}
#[test]
fn test_ok_response() {
let response = SmtpResponse::ok();
assert_eq!(response.code, "250");
assert_eq!(response.message, "OK");
}
#[test]
fn test_greeting_response() {
let response = SmtpResponse::greeting();
assert_eq!(response.code, "220");
assert_eq!(response.message, "Welcome to MogiMail");
}
#[test]
fn test_helo_response() {
let response = SmtpResponse::helo("server.local", "client.local");
assert_eq!(response.code, "250");
assert_eq!(response.message, "server.local Hello client.local");
}
#[test]
fn test_data_start_response() {
let response = SmtpResponse::data_start();
assert_eq!(response.code, "354");
assert_eq!(response.message, "End data with <CR><LF>.<CR><LF>");
}
#[test]
fn test_quit_response() {
let response = SmtpResponse::quit();
assert_eq!(response.code, "221");
assert_eq!(response.message, "Bye");
}
#[test]
fn test_error_response() {
let response = SmtpResponse::error("500", "Syntax error");
assert_eq!(response.code, "500");
assert_eq!(response.message, "Syntax error");
}
#[test]
fn test_format() {
let response = SmtpResponse::new("250", "OK");
assert_eq!(response.format(), "250 OK\r\n");
}
#[test]
fn test_is_success() {
let success_response = SmtpResponse::new("250", "OK");
assert!(success_response.is_success());
let error_response = SmtpResponse::new("500", "Error");
assert!(!error_response.is_success());
}
#[test]
fn test_is_error() {
let error_response = SmtpResponse::new("500", "Error");
assert!(error_response.is_error());
let client_error_response = SmtpResponse::new("421", "Service not available");
assert!(client_error_response.is_error());
let success_response = SmtpResponse::new("250", "OK");
assert!(!success_response.is_error());
}
}