From ffe24573809fcac6bbbebb7e1cb43c430e7a7d0a Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sun, 22 Dec 2019 20:05:01 -0500 Subject: [PATCH] remove unused error trait --- rustfmt.toml | 1 + src/main.rs | 43 +++++++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index de995b8..2489c66 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,3 @@ tab_spaces = 2 use_field_init_shorthand = true +max_width = 80 diff --git a/src/main.rs b/src/main.rs index 3a9d783..ca09e3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use handlebars::Handlebars; use hotwatch::{Event, Hotwatch}; use serde::Deserialize; use std::collections::{BTreeMap, HashMap}; -use std::error::Error; use std::fmt; use std::fs::{File, OpenOptions}; use std::io::Write; @@ -35,8 +34,8 @@ impl fmt::Display for BunBunError { } } -impl Error for BunBunError {} - +/// Generates a from implementation from the specified type to the provided +/// bunbun error. macro_rules! from_error { ($from:ty, $to:ident) => { impl From<$from> for BunBunError { @@ -63,7 +62,10 @@ struct SearchQuery { } #[get("/hop")] -fn hop(data: Data>>, query: Query) -> impl Responder { +fn hop( + data: Data>>, + query: Query, +) -> impl Responder { let data = data.read().unwrap(); let mut raw_args = query.to.split_ascii_whitespace(); let command = raw_args.next(); @@ -125,7 +127,8 @@ fn index(data: Data>>) -> impl Responder { let data = data.read().unwrap(); let mut template_args = HashMap::new(); template_args.insert("hostname", &data.public_address); - HttpResponse::Ok().body(data.renderer.render("index", &template_args).unwrap()) + HttpResponse::Ok() + .body(data.renderer.render("index", &template_args).unwrap()) } #[get("/bunbunsearch.xml")] @@ -141,14 +144,8 @@ fn opensearch(data: Data>>) -> impl Responder { .body(data.renderer.render("opensearch", &template_args).unwrap()) } -#[derive(Deserialize)] -struct Config { - bind_address: String, - public_address: String, - default_route: Option, - routes: BTreeMap, -} - +/// Dynamic variables that either need to be present at runtime, or can be +/// changed during runtime. struct State { public_address: String, default_route: Option, @@ -197,6 +194,16 @@ fn main() -> Result<(), BunBunError> { Ok(()) } +#[derive(Deserialize)] +struct Config { + bind_address: String, + public_address: String, + default_route: Option, + routes: BTreeMap, +} + +/// Attempts to read the config file. If it doesn't exist, generate one a +/// default config file before attempting to parse it. fn read_config(config_file_path: &str) -> Result { let config_file = match File::open(config_file_path) { Ok(file) => file, @@ -217,11 +224,13 @@ fn read_config(config_file_path: &str) -> Result { Ok(serde_yaml::from_reader(config_file)?) } +/// Returns an instance with all pre-generated templates included into the +/// binary. This allows for users to have a portable binary without needed the +/// templates at runtime. fn compile_templates() -> Handlebars { let mut handlebars = Handlebars::new(); - macro_rules! register_template { - ( $( $template:expr ),* ) => { + [ $( $template:expr ),* ] => { $( handlebars .register_template_string( @@ -233,8 +242,6 @@ fn compile_templates() -> Handlebars { )* }; } - - register_template!("index", "list", "opensearch"); - + register_template!["index", "list", "opensearch"]; handlebars }