1use core::fmt;
40
41use alloc::{
42 string::{String, ToString},
43 vec::Vec,
44};
45
46use log::trace;
47use thiserror::Error;
48
49use crate::{coroutine::*, rfc5321::types::reply_code::ReplyCode, send::*, smtp_try};
50
51pub struct SmtpRsetCommand;
53
54impl From<SmtpRsetCommand> for Vec<u8> {
55 fn from(_: SmtpRsetCommand) -> Vec<u8> {
56 b"RSET\r\n".to_vec()
57 }
58}
59
60#[derive(Clone, Debug, Error)]
62pub enum SmtpRsetError {
63 #[error("SMTP RSET failed: rejected {code} {message}")]
64 Rejected { code: u16, message: String },
65 #[error("SMTP RSET failed: {0}")]
66 Send(#[from] SendSmtpCommandError),
67}
68
69pub struct SmtpRset {
71 state: State,
72}
73
74impl SmtpRset {
75 pub fn new() -> Self {
76 Self {
77 state: State::Send(SendSmtpCommand::new(SmtpRsetCommand)),
78 }
79 }
80}
81
82impl Default for SmtpRset {
83 fn default() -> Self {
84 Self::new()
85 }
86}
87
88impl SmtpCoroutine for SmtpRset {
89 type Yield = SmtpYield;
90 type Return = Result<(), SmtpRsetError>;
91
92 fn resume(&mut self, arg: Option<&[u8]>) -> SmtpCoroutineState<Self::Yield, Self::Return> {
93 loop {
94 trace!("rset: {}", self.state);
95
96 match &mut self.state {
97 State::Send(send) => {
98 let out = smtp_try!(send, arg);
99
100 if out.response.code == ReplyCode::OK {
101 return SmtpCoroutineState::Complete(Ok(()));
102 }
103
104 let code = out.response.code.code();
105 let message = out.response.text().to_string();
106 return SmtpCoroutineState::Complete(Err(SmtpRsetError::Rejected {
107 code,
108 message,
109 }));
110 }
111 }
112 }
113 }
114}
115
116enum State {
117 Send(SendSmtpCommand<SmtpRsetCommand>),
118}
119
120impl fmt::Display for State {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 match self {
123 Self::Send(_) => f.write_str("send rset"),
124 }
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 #[test]
133 fn success_returns_ok() {
134 let mut rset = SmtpRset::new();
135
136 let bytes = expect_wants_write(&mut rset, None);
137 assert_eq!(bytes, b"RSET\r\n");
138
139 expect_wants_read(&mut rset);
140 expect_complete_ok(&mut rset, b"250 OK\r\n");
141 }
142
143 #[test]
144 fn rejected_returns_rejected_error() {
145 let mut rset = SmtpRset::new();
146 let _ = expect_wants_write(&mut rset, None);
147 expect_wants_read(&mut rset);
148
149 let err = expect_complete_err(&mut rset, b"500 syntax error\r\n");
150 let SmtpRsetError::Rejected { code, message } = err else {
151 panic!("expected SmtpRsetError::Rejected, got {err:?}");
152 };
153 assert_eq!(code, 500);
154 assert_eq!(message, "syntax error");
155 }
156
157 #[test]
158 fn eof_returns_eof_error() {
159 let mut rset = SmtpRset::new();
160 let _ = expect_wants_write(&mut rset, None);
161 expect_wants_read(&mut rset);
162
163 let err = expect_complete_err(&mut rset, b"");
164 assert!(matches!(
165 err,
166 SmtpRsetError::Send(SendSmtpCommandError::Eof)
167 ));
168 }
169
170 fn expect_wants_write(cor: &mut SmtpRset, arg: Option<&[u8]>) -> Vec<u8> {
173 match cor.resume(arg) {
174 SmtpCoroutineState::Yielded(SmtpYield::WantsWrite(bytes)) => bytes,
175 state => panic!("expected WantsWrite, got {state:?}"),
176 }
177 }
178
179 fn expect_wants_read(cor: &mut SmtpRset) {
180 match cor.resume(None) {
181 SmtpCoroutineState::Yielded(SmtpYield::WantsRead) => {}
182 state => panic!("expected WantsRead, got {state:?}"),
183 }
184 }
185
186 fn expect_complete_ok(cor: &mut SmtpRset, reply: &[u8]) {
187 match cor.resume(Some(reply)) {
188 SmtpCoroutineState::Complete(Ok(())) => {}
189 state => panic!("expected Complete(Ok), got {state:?}"),
190 }
191 }
192
193 fn expect_complete_err(cor: &mut SmtpRset, reply: &[u8]) -> SmtpRsetError {
194 match cor.resume(Some(reply)) {
195 SmtpCoroutineState::Complete(Err(err)) => err,
196 state => panic!("expected Complete(Err), got {state:?}"),
197 }
198 }
199}