Fix var swapping in finalize_message

This commit is contained in:
Martin 2023-07-02 20:15:10 +02:00
parent 263dbda46a
commit 543f56d904
Signed by: mawalu
GPG Key ID: BF556F989760A7C8
1 changed files with 18 additions and 11 deletions

View File

@ -1,4 +1,5 @@
use std::io::{BufRead, BufReader, Write};
use std::mem;
use std::net::{TcpListener, TcpStream};
pub fn start_server() {
@ -12,7 +13,13 @@ pub fn start_server() {
let reader = BufReader::new(tcp_stream.try_clone().unwrap());
let mut connection = Connection::new();
let _ = connection.handle(tcp_stream, reader);
if let Err(e) = connection.handle(tcp_stream, reader) {
eprintln!("System error: {}", e)
} else {
for mail in connection.messages {
println!("Mail from {} for {}\n{}", mail.sender, mail.recipients.join(", "), mail.data.unwrap_or("<empty>".to_string()))
}
}
}
Err(e) => {
eprintln!("Failed to accept connection: {}", e)
@ -102,14 +109,14 @@ impl Connection {
send(&mut writer, out)?;
if out == MSG_BYE {
break
break;
}
}
line.clear()
}
return Ok(())
return Ok(());
}
fn handle_line(&mut self, line: &String) -> Result<Option<&str>, &'static str> {
@ -147,7 +154,7 @@ impl Connection {
ConnectionState::Data => {
if line.trim_end() == CMD_DOT {
self.state = ConnectionState::Done;
return Ok(Some(MSG_OK))
return Ok(Some(MSG_OK));
}
return match self.current_message {
@ -163,18 +170,17 @@ impl Connection {
_ => {
Err(MSG_FAIL)
}
}
};
}
ConnectionState::Done => {
if line.starts_with(CMD_QUIT) {
self.finalize_mail();
return Ok(Some(MSG_BYE))
return Ok(Some(MSG_BYE));
}
if line.starts_with(CMD_MAIL_FROM) {
self.finalize_mail();
return Ok(Some(MSG_OK))
return Ok(Some(MSG_OK));
}
}
}
@ -183,9 +189,10 @@ impl Connection {
}
fn finalize_mail(&mut self) {
if let Some(mail) = &self.current_message {
self.current_message = None;
self.messages.push(*mail)
let current_message = mem::replace(&mut self.current_message, None);
if let Some(mail) = current_message {
self.messages.push(mail)
}
}