diff --git a/src/errors.rs b/src/errors.rs index a6b32c7..f6e6aec 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,8 @@ use thiserror::Error; +// TODO add NotFound and InternalServerError response here, +// return it in NotFound and InternalError + #[derive(Debug, Error)] pub enum RequestError { #[error("request unable to be parsed as a form")] @@ -22,8 +25,8 @@ pub enum InternalError { #[derive(Debug, Error)] pub enum HandlingError { - #[error("client error")] - ClientError(#[from] RequestError), - #[error("server error")] - ServerError(#[from] InternalError), + #[error("client error: {0}")] + ClientError(RequestError), + #[error("server error: {0}")] + ServerError(InternalError), } diff --git a/src/http.rs b/src/http.rs index a72e6a8..ac0e07b 100644 --- a/src/http.rs +++ b/src/http.rs @@ -48,7 +48,7 @@ impl Request { for mut x in self.body.split("&").map(|x| x.split("=")) { hashmap.insert( x.next().ok_or(RequestError::NotAForm)?, - x.next().ok_or(RequestError::NotAForm)? + x.next().ok_or(RequestError::NotAForm)?, ); } Ok(hashmap) @@ -61,7 +61,9 @@ impl FromStr for Request { fn from_str(s: &str) -> Result { //dbg!(&s); let mut iter = s.lines(); - let mut first = iter.next().ok_or(RequestError::BadRequest)? + let mut first = iter + .next() + .ok_or(RequestError::BadRequest)? .split_whitespace(); let method = first.next().ok_or(RequestError::BadRequest)?; let uri = first.next().ok_or(RequestError::BadRequest)?; @@ -114,4 +116,3 @@ impl fmt::Display for Response { ) } } - diff --git a/src/main.rs b/src/main.rs index d688c5a..0d36a8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,11 @@ mod errors; mod http; mod post; -use crate::errors::{RequestError, HandlingError, InternalError}; +use crate::errors::{HandlingError, InternalError, RequestError}; use crate::http::{Request, Response, Status}; use crate::post::{Post, Thread}; +use std::error::Error; use std::io::{Read, Write}; use std::net::{SocketAddr, TcpListener, TcpStream}; use std::path::PathBuf; @@ -16,10 +17,10 @@ use std::sync::{Arc, Mutex, MutexGuard}; use bincode::{deserialize, serialize}; use cached::proc_macro::cached; use lazy_static::lazy_static; +use log::*; use simplelog::*; use structopt::StructOpt; use threadpool::ThreadPool; -use log::*; // statically linked index and favicon const INDEX: &'static str = include_str!("www/index.html"); @@ -66,16 +67,14 @@ impl State { fn handle(request: &str, state: Arc>) -> Response { let request = match request.parse::() { Ok(s) => s, - Err(e) => { - return Response::new(Status::BadRequest, vec![], "".to_string()) - }, + Err(e) => return Response::new(Status::BadRequest, vec![], e.to_string()), }; let state = state.lock().unwrap(); match request.method.as_str() { "GET" => match get(&request.uri, state) { Ok(s) => s, - Err(e) => Response::new(Status::NotFound, vec![], "".to_string()), + Err(e) => Response::new(Status::NotFound, vec![], e.to_string()), }, "POST" => post(request, state), // check admin hash @@ -114,11 +113,14 @@ fn get(path: &str, state: MutexGuard) -> Result let content = content( s, &deserialize::( - &state.db.get(&s.as_bytes()) - .map_err(|_| HandlingError::ServerError(InternalError::DatabaseReadError))? - .ok_or(HandlingError::ClientError(RequestError::NotFound))?) - .unwrap() - .to_string(), + &state + .db + .get(&s.as_bytes()) + .map_err(|_| HandlingError::ServerError(InternalError::DatabaseReadError))? + .ok_or(HandlingError::ClientError(RequestError::NotFound))?, + ) + .unwrap() + .to_string(), ); Ok(Response::new(Status::Ok, vec![], content)) @@ -141,7 +143,8 @@ fn post(request: Request, mut state: MutexGuard) -> Response { state .db - .insert(id.to_ne_bytes(), serialize(&thread).unwrap()); + .insert(id.to_ne_bytes(), serialize(&thread).unwrap()) + .unwrap(); Response::new(Status::Ok, vec![], String::from("")) } @@ -168,8 +171,12 @@ fn main() { let pool = ThreadPool::new(num_cpus::get()); // setup logger TermLogger::init( - LevelFilter::Warn, Config::default(), TerminalMode::Mixed, ColorChoice::Auto - ).unwrap(); + LevelFilter::Warn, + Config::default(), + TerminalMode::Mixed, + ColorChoice::Auto, + ) + .unwrap(); // open database let state = Arc::new(Mutex::new(State { db: sled::open(&OPT.database).unwrap(), diff --git a/src/post.rs b/src/post.rs index 33b09c0..ff1313c 100644 --- a/src/post.rs +++ b/src/post.rs @@ -11,7 +11,7 @@ pub struct Image { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Post { pub id: u64, - pub img: Option, + //pub img: Option, pub body: String, } @@ -31,7 +31,11 @@ impl Thread { impl Post { pub fn new(id: u64, img: Option, body: String) -> Self { - Self { id, img, body } + Self { + id, + //img, + body, + } } } @@ -60,11 +64,12 @@ impl fmt::Display for Post { self.id, "", self.id, - if let Some(s) = &self.img { - &s.filename - } else { - "" - }, + //if let Some(s) = &self.img { + // &s.filename + //} else { + // "" + //}, + "", self.body, ) } diff --git a/src/www/index.html b/src/www/index.html index 4743312..4f85e63 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -11,7 +11,7 @@