use core::fmt;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use log::trace;
use thiserror::Error;
use crate::{coroutine::*, rfc5321::types::reply_code::ReplyCode, send::*, smtp_try};
pub struct SmtpQuitCommand;
impl From<SmtpQuitCommand> for Vec<u8> {
fn from(_: SmtpQuitCommand) -> Vec<u8> {
b"QUIT\r\n".to_vec()
}
}
#[derive(Clone, Debug, Error)]
pub enum SmtpQuitError {
#[error("SMTP QUIT failed: rejected {code} {message}")]
Rejected { code: u16, message: String },
#[error("SMTP QUIT failed: {0}")]
Send(#[from] SendSmtpCommandError),
}
pub struct SmtpQuit {
state: State,
}
impl SmtpQuit {
pub fn new() -> Self {
Self {
state: State::Send(SendSmtpCommand::new(SmtpQuitCommand)),
}
}
}
impl Default for SmtpQuit {
fn default() -> Self {
Self::new()
}
}
impl SmtpCoroutine for SmtpQuit {
type Yield = SmtpYield;
type Return = Result<(), SmtpQuitError>;
fn resume(&mut self, arg: Option<&[u8]>) -> SmtpCoroutineState<Self::Yield, Self::Return> {
loop {
trace!("quit: {}", self.state);
match &mut self.state {
State::Send(send) => {
let out = smtp_try!(send, arg);
if out.response.code == ReplyCode::SERVICE_CLOSING {
return SmtpCoroutineState::Complete(Ok(()));
}
let code = out.response.code.code();
let message = out.response.text().to_string();
return SmtpCoroutineState::Complete(Err(SmtpQuitError::Rejected {
code,
message,
}));
}
}
}
}
}
enum State {
Send(SendSmtpCommand<SmtpQuitCommand>),
}
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Send(_) => f.write_str("send quit"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn success_returns_ok() {
let mut quit = SmtpQuit::new();
let bytes = expect_wants_write(&mut quit, None);
assert_eq!(bytes, b"QUIT\r\n");
expect_wants_read(&mut quit);
expect_complete_ok(&mut quit, b"221 service closing\r\n");
}
#[test]
fn rejected_returns_rejected_error() {
let mut quit = SmtpQuit::new();
let _ = expect_wants_write(&mut quit, None);
expect_wants_read(&mut quit);
let err = expect_complete_err(&mut quit, b"500 syntax error\r\n");
let SmtpQuitError::Rejected { code, message } = err else {
panic!("expected SmtpQuitError::Rejected, got {err:?}");
};
assert_eq!(code, 500);
assert_eq!(message, "syntax error");
}
#[test]
fn eof_returns_eof_error() {
let mut quit = SmtpQuit::new();
let _ = expect_wants_write(&mut quit, None);
expect_wants_read(&mut quit);
let err = expect_complete_err(&mut quit, b"");
assert!(matches!(
err,
SmtpQuitError::Send(SendSmtpCommandError::Eof)
));
}
fn expect_wants_write(cor: &mut SmtpQuit, arg: Option<&[u8]>) -> Vec<u8> {
match cor.resume(arg) {
SmtpCoroutineState::Yielded(SmtpYield::WantsWrite(bytes)) => bytes,
state => panic!("expected WantsWrite, got {state:?}"),
}
}
fn expect_wants_read(cor: &mut SmtpQuit) {
match cor.resume(None) {
SmtpCoroutineState::Yielded(SmtpYield::WantsRead) => {}
state => panic!("expected WantsRead, got {state:?}"),
}
}
fn expect_complete_ok(cor: &mut SmtpQuit, reply: &[u8]) {
match cor.resume(Some(reply)) {
SmtpCoroutineState::Complete(Ok(())) => {}
state => panic!("expected Complete(Ok), got {state:?}"),
}
}
fn expect_complete_err(cor: &mut SmtpQuit, reply: &[u8]) -> SmtpQuitError {
match cor.resume(Some(reply)) {
SmtpCoroutineState::Complete(Err(err)) => err,
state => panic!("expected Complete(Err), got {state:?}"),
}
}
}