remove unused error trait

This commit is contained in:
Edward Shen 2019-12-22 20:05:01 -05:00
parent ea586013c9
commit ffe2457380
Signed by: edward
GPG key ID: F350507060ED6C90
2 changed files with 26 additions and 18 deletions

View file

@ -1,2 +1,3 @@
tab_spaces = 2 tab_spaces = 2
use_field_init_shorthand = true use_field_init_shorthand = true
max_width = 80

View file

@ -8,7 +8,6 @@ use handlebars::Handlebars;
use hotwatch::{Event, Hotwatch}; use hotwatch::{Event, Hotwatch};
use serde::Deserialize; use serde::Deserialize;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::error::Error;
use std::fmt; use std::fmt;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::Write; 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 { macro_rules! from_error {
($from:ty, $to:ident) => { ($from:ty, $to:ident) => {
impl From<$from> for BunBunError { impl From<$from> for BunBunError {
@ -63,7 +62,10 @@ struct SearchQuery {
} }
#[get("/hop")] #[get("/hop")]
fn hop(data: Data<Arc<RwLock<State>>>, query: Query<SearchQuery>) -> impl Responder { fn hop(
data: Data<Arc<RwLock<State>>>,
query: Query<SearchQuery>,
) -> impl Responder {
let data = data.read().unwrap(); let data = data.read().unwrap();
let mut raw_args = query.to.split_ascii_whitespace(); let mut raw_args = query.to.split_ascii_whitespace();
let command = raw_args.next(); let command = raw_args.next();
@ -125,7 +127,8 @@ fn index(data: Data<Arc<RwLock<State>>>) -> impl Responder {
let data = data.read().unwrap(); let data = data.read().unwrap();
let mut template_args = HashMap::new(); let mut template_args = HashMap::new();
template_args.insert("hostname", &data.public_address); 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")] #[get("/bunbunsearch.xml")]
@ -141,14 +144,8 @@ fn opensearch(data: Data<Arc<RwLock<State>>>) -> impl Responder {
.body(data.renderer.render("opensearch", &template_args).unwrap()) .body(data.renderer.render("opensearch", &template_args).unwrap())
} }
#[derive(Deserialize)] /// Dynamic variables that either need to be present at runtime, or can be
struct Config { /// changed during runtime.
bind_address: String,
public_address: String,
default_route: Option<String>,
routes: BTreeMap<String, String>,
}
struct State { struct State {
public_address: String, public_address: String,
default_route: Option<String>, default_route: Option<String>,
@ -197,6 +194,16 @@ fn main() -> Result<(), BunBunError> {
Ok(()) Ok(())
} }
#[derive(Deserialize)]
struct Config {
bind_address: String,
public_address: String,
default_route: Option<String>,
routes: BTreeMap<String, String>,
}
/// 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<Config, BunBunError> { fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
let config_file = match File::open(config_file_path) { let config_file = match File::open(config_file_path) {
Ok(file) => file, Ok(file) => file,
@ -217,11 +224,13 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
Ok(serde_yaml::from_reader(config_file)?) 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 { fn compile_templates() -> Handlebars {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
macro_rules! register_template { macro_rules! register_template {
( $( $template:expr ),* ) => { [ $( $template:expr ),* ] => {
$( $(
handlebars handlebars
.register_template_string( .register_template_string(
@ -233,8 +242,6 @@ fn compile_templates() -> Handlebars {
)* )*
}; };
} }
register_template!["index", "list", "opensearch"];
register_template!("index", "list", "opensearch");
handlebars handlebars
} }