1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
use std::{fmt, fs, path::Path}; #[derive(Debug)] pub struct Response { pub code: usize, pub body: String, } impl Default for Response { fn default() -> Response { Response { code: 200, body: String::new(), } } } impl Response { pub fn new() -> Response { Response::default() } pub fn from<T: Into<Response>>(from: T) -> Response { from.into() } pub fn with_code(mut self, code: usize) -> Response { self.code = code; self } pub fn with_body(mut self, body: &str) -> Response { self.body = body.to_string(); self } pub fn from_file<P: AsRef<Path>>(mut self, path: P) -> Response { match fs::read_to_string(path) { Ok(body) => self.body = body, Err(e) => { self.body = format!("<h1>500 Internal Error</h1><pre>{:?}", e); self.code = 500; } } self } } impl fmt::Display for Response { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.body) } } impl From<&str> for Response { fn from(s: &str) -> Response { Response { body: s.to_string(), ..Response::default() } } } impl From<&String> for Response { fn from(s: &String) -> Response { Response { body: s.clone(), ..Response::default() } } } impl From<String> for Response { fn from(body: String) -> Response { Response { body, ..Response::default() } } } impl From<usize> for Response { fn from(i: usize) -> Response { Response { code: i.into(), ..Response::default() } } }