Apply clippy feedback
This commit is contained in:
parent
543f56d904
commit
0e2736f2b4
|
@ -1,6 +1,6 @@
|
|||
use rouille::{router, Request, Response, try_or_400, post_input};
|
||||
use crate::random_names::get_name;
|
||||
use crate::state::{Mailbox, State};
|
||||
use rouille::{post_input, router, try_or_400, Request, Response};
|
||||
|
||||
pub fn http_handler(request: &Request, state: &State) -> Response {
|
||||
router!(request,
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
extern crate rouille;
|
||||
extern crate rand;
|
||||
extern crate rouille;
|
||||
|
||||
use std::thread;
|
||||
|
||||
mod http;
|
||||
mod random_names;
|
||||
mod state;
|
||||
mod smtp;
|
||||
mod state;
|
||||
|
||||
fn main() {
|
||||
println!("Starting on localhost:8005");
|
||||
|
|
2748
src/random_names.rs
2748
src/random_names.rs
File diff suppressed because one or more lines are too long
39
src/smtp.rs
39
src/smtp.rs
|
@ -1,5 +1,5 @@
|
|||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::mem;
|
||||
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
|
||||
pub fn start_server() {
|
||||
|
@ -17,7 +17,12 @@ pub fn start_server() {
|
|||
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()))
|
||||
println!(
|
||||
"Mail from {} for {}\n{}",
|
||||
mail.sender,
|
||||
mail.recipients.join(", "),
|
||||
mail.data.unwrap_or("<empty>".to_string())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,7 +90,11 @@ impl Connection {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle(&mut self, mut writer: TcpStream, mut reader: BufReader<TcpStream>) -> Result<(), &str> {
|
||||
fn handle(
|
||||
&mut self,
|
||||
mut writer: TcpStream,
|
||||
mut reader: BufReader<TcpStream>,
|
||||
) -> Result<(), &str> {
|
||||
send(&mut writer, MSG_BANNER)?;
|
||||
|
||||
let mut line = String::new();
|
||||
|
@ -93,7 +102,7 @@ impl Connection {
|
|||
loop {
|
||||
let read_result = reader.read_line(&mut line);
|
||||
|
||||
if read_result.is_err() || line.len() == 0 {
|
||||
if read_result.is_err() || line.is_empty() {
|
||||
return Err(ERR_READ_FAILED);
|
||||
}
|
||||
|
||||
|
@ -101,9 +110,7 @@ impl Connection {
|
|||
|
||||
let response = self.handle_line(&line);
|
||||
|
||||
if let Err(_e) = response {
|
||||
return Err(_e); // _e
|
||||
}
|
||||
response?;
|
||||
|
||||
if let Ok(Some(out)) = response {
|
||||
send(&mut writer, out)?;
|
||||
|
@ -116,10 +123,10 @@ impl Connection {
|
|||
line.clear()
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_line(&mut self, line: &String) -> Result<Option<&str>, &'static str> {
|
||||
fn handle_line(&mut self, line: &str) -> Result<Option<&str>, &'static str> {
|
||||
match self.state {
|
||||
ConnectionState::PreHello => {
|
||||
if line.starts_with(CMD_HELLO) {
|
||||
|
@ -129,7 +136,7 @@ impl Connection {
|
|||
}
|
||||
ConnectionState::Hello => {
|
||||
if line.starts_with(CMD_MAIL_FROM) {
|
||||
self.current_message = Some(Message::new(line.clone()));
|
||||
self.current_message = Some(Message::new(line.to_owned()));
|
||||
self.state = ConnectionState::Mail;
|
||||
return Ok(Some(MSG_OK));
|
||||
}
|
||||
|
@ -167,9 +174,7 @@ impl Connection {
|
|||
|
||||
Ok(None)
|
||||
}
|
||||
_ => {
|
||||
Err(MSG_FAIL)
|
||||
}
|
||||
_ => Err(MSG_FAIL),
|
||||
};
|
||||
}
|
||||
ConnectionState::Done => {
|
||||
|
@ -189,17 +194,17 @@ impl Connection {
|
|||
}
|
||||
|
||||
fn finalize_mail(&mut self) {
|
||||
let current_message = mem::replace(&mut self.current_message, None);
|
||||
let current_message = self.current_message.take();
|
||||
|
||||
if let Some(mail) = current_message {
|
||||
self.messages.push(mail)
|
||||
}
|
||||
}
|
||||
|
||||
fn add_recipient(&mut self, line: &String) -> Result<(), &'static str> {
|
||||
fn add_recipient(&mut self, line: &str) -> Result<(), &'static str> {
|
||||
match self.current_message {
|
||||
Some(ref mut message) => {
|
||||
message.recipients.push(line.clone());
|
||||
message.recipients.push(line.to_owned());
|
||||
self.state = ConnectionState::PreData
|
||||
}
|
||||
_ => {
|
||||
|
@ -217,4 +222,4 @@ fn send(writer: &mut TcpStream, message: &str) -> Result<(), &'static str> {
|
|||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
18
src/state.rs
18
src/state.rs
|
@ -1,6 +1,6 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
pub type State = Mutex<HashMap<String, Mailbox>>;
|
||||
|
||||
|
@ -12,21 +12,21 @@ pub struct Message {
|
|||
pub from: String,
|
||||
pub received: DateTime<Utc>,
|
||||
pub subject: String,
|
||||
pub body: String
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
pub struct Mailbox {
|
||||
pub email: String,
|
||||
pub messages: Vec<Message>,
|
||||
pub created_at: DateTime<Utc>
|
||||
pub created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Mailbox {
|
||||
pub fn new(email: String) -> Self {
|
||||
Self {
|
||||
email,
|
||||
messages: Vec::new(),
|
||||
created_at: Utc::now()
|
||||
}
|
||||
Self {
|
||||
email,
|
||||
messages: Vec::new(),
|
||||
created_at: Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue