From 996924582afe7500af7d1ef3ba6b1ad6b80ebc57 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Sat, 21 Dec 2019 23:34:03 -0500 Subject: [PATCH] use macros --- src/main.rs | 80 ++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index 85267df..b1b1c15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ static CONFIG_FILE: &str = "bunbun.toml"; enum BunBunError { IoError(std::io::Error), ParseError(serde_yaml::Error), + WatchError(hotwatch::Error), } impl fmt::Display for BunBunError { @@ -29,23 +30,26 @@ impl fmt::Display for BunBunError { match self { BunBunError::IoError(e) => e.fmt(f), BunBunError::ParseError(e) => e.fmt(f), + BunBunError::WatchError(e) => e.fmt(f), } } } impl Error for BunBunError {} -impl From for BunBunError { - fn from(error: std::io::Error) -> Self { - BunBunError::IoError(error) - } +macro_rules! from_error { + ($from:ty, $to:ident) => { + impl From<$from> for BunBunError { + fn from(e: $from) -> Self { + BunBunError::$to(e) + } + } + }; } -impl From for BunBunError { - fn from(error: serde_yaml::Error) -> Self { - BunBunError::ParseError(error) - } -} +from_error!(std::io::Error, IoError); +from_error!(serde_yaml::Error, ParseError); +from_error!(hotwatch::Error, WatchError); #[get("/ls")] fn list(data: Data>>) -> impl Responder { @@ -163,24 +167,21 @@ fn main() -> Result<(), BunBunError> { })); let state_ref = state.clone(); - let mut watch = - Hotwatch::new_with_custom_delay(Duration::from_millis(500)).expect("Failed to init watch"); + let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?; - watch - .watch(CONFIG_FILE, move |e: Event| { - if let Event::Write(_) = e { - let mut state = state.write().unwrap(); - match read_config(CONFIG_FILE) { - Ok(conf) => { - state.public_address = conf.public_address; - state.default_route = conf.default_route; - state.routes = conf.routes; - } - Err(e) => eprintln!("Config is malformed: {}", e), + watch.watch(CONFIG_FILE, move |e: Event| { + if let Event::Write(_) = e { + let mut state = state.write().unwrap(); + match read_config(CONFIG_FILE) { + Ok(conf) => { + state.public_address = conf.public_address; + state.default_route = conf.default_route; + state.routes = conf.routes; } + Err(e) => eprintln!("Config is malformed: {}", e), } - }) - .expect("failed to watch"); + } + })?; HttpServer::new(move || { App::new() @@ -218,22 +219,27 @@ fn read_config(config_file_path: &str) -> Result { fn compile_templates() -> Handlebars { let mut handlebars = Handlebars::new(); - handlebars - .register_template_string( - "index", - String::from_utf8_lossy(include_bytes!("templates/index.hbs")), - ) - .unwrap(); + + macro_rules! register_template { + ( $( $template:expr ),* ) => { + $( + handlebars + .register_template_string( + $template, + String::from_utf8_lossy( + include_bytes!(concat!("templates/", $template, ".hbs"))) + ) + .unwrap(); + )* + }; + } + + register_template!("index", "list"); + handlebars .register_template_string( "opensearch", - String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")), - ) - .unwrap(); - handlebars - .register_template_string( - "list", - String::from_utf8_lossy(include_bytes!("templates/list.hbs")), + String::from_utf8_lossy(include_bytes!(concat!("templates/", "bunbunsearch.xml"))), ) .unwrap(); handlebars