use macros
This commit is contained in:
parent
320e41c6f2
commit
996924582a
1 changed files with 43 additions and 37 deletions
52
src/main.rs
52
src/main.rs
|
@ -22,6 +22,7 @@ static CONFIG_FILE: &str = "bunbun.toml";
|
||||||
enum BunBunError {
|
enum BunBunError {
|
||||||
IoError(std::io::Error),
|
IoError(std::io::Error),
|
||||||
ParseError(serde_yaml::Error),
|
ParseError(serde_yaml::Error),
|
||||||
|
WatchError(hotwatch::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for BunBunError {
|
impl fmt::Display for BunBunError {
|
||||||
|
@ -29,23 +30,26 @@ impl fmt::Display for BunBunError {
|
||||||
match self {
|
match self {
|
||||||
BunBunError::IoError(e) => e.fmt(f),
|
BunBunError::IoError(e) => e.fmt(f),
|
||||||
BunBunError::ParseError(e) => e.fmt(f),
|
BunBunError::ParseError(e) => e.fmt(f),
|
||||||
|
BunBunError::WatchError(e) => e.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for BunBunError {}
|
impl Error for BunBunError {}
|
||||||
|
|
||||||
impl From<std::io::Error> for BunBunError {
|
macro_rules! from_error {
|
||||||
fn from(error: std::io::Error) -> Self {
|
($from:ty, $to:ident) => {
|
||||||
BunBunError::IoError(error)
|
impl From<$from> for BunBunError {
|
||||||
|
fn from(e: $from) -> Self {
|
||||||
|
BunBunError::$to(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl From<serde_yaml::Error> for BunBunError {
|
from_error!(std::io::Error, IoError);
|
||||||
fn from(error: serde_yaml::Error) -> Self {
|
from_error!(serde_yaml::Error, ParseError);
|
||||||
BunBunError::ParseError(error)
|
from_error!(hotwatch::Error, WatchError);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/ls")]
|
#[get("/ls")]
|
||||||
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
fn list(data: Data<Arc<RwLock<State>>>) -> impl Responder {
|
||||||
|
@ -163,11 +167,9 @@ fn main() -> Result<(), BunBunError> {
|
||||||
}));
|
}));
|
||||||
let state_ref = state.clone();
|
let state_ref = state.clone();
|
||||||
|
|
||||||
let mut watch =
|
let mut watch = Hotwatch::new_with_custom_delay(Duration::from_millis(500))?;
|
||||||
Hotwatch::new_with_custom_delay(Duration::from_millis(500)).expect("Failed to init watch");
|
|
||||||
|
|
||||||
watch
|
watch.watch(CONFIG_FILE, move |e: Event| {
|
||||||
.watch(CONFIG_FILE, move |e: Event| {
|
|
||||||
if let Event::Write(_) = e {
|
if let Event::Write(_) = e {
|
||||||
let mut state = state.write().unwrap();
|
let mut state = state.write().unwrap();
|
||||||
match read_config(CONFIG_FILE) {
|
match read_config(CONFIG_FILE) {
|
||||||
|
@ -179,8 +181,7 @@ fn main() -> Result<(), BunBunError> {
|
||||||
Err(e) => eprintln!("Config is malformed: {}", e),
|
Err(e) => eprintln!("Config is malformed: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})?;
|
||||||
.expect("failed to watch");
|
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -218,22 +219,27 @@ fn read_config(config_file_path: &str) -> Result<Config, BunBunError> {
|
||||||
|
|
||||||
fn compile_templates() -> Handlebars {
|
fn compile_templates() -> Handlebars {
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
|
|
||||||
|
macro_rules! register_template {
|
||||||
|
( $( $template:expr ),* ) => {
|
||||||
|
$(
|
||||||
handlebars
|
handlebars
|
||||||
.register_template_string(
|
.register_template_string(
|
||||||
"index",
|
$template,
|
||||||
String::from_utf8_lossy(include_bytes!("templates/index.hbs")),
|
String::from_utf8_lossy(
|
||||||
|
include_bytes!(concat!("templates/", $template, ".hbs")))
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
)*
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
register_template!("index", "list");
|
||||||
|
|
||||||
handlebars
|
handlebars
|
||||||
.register_template_string(
|
.register_template_string(
|
||||||
"opensearch",
|
"opensearch",
|
||||||
String::from_utf8_lossy(include_bytes!("templates/bunbunsearch.xml")),
|
String::from_utf8_lossy(include_bytes!(concat!("templates/", "bunbunsearch.xml"))),
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
handlebars
|
|
||||||
.register_template_string(
|
|
||||||
"list",
|
|
||||||
String::from_utf8_lossy(include_bytes!("templates/list.hbs")),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handlebars
|
handlebars
|
||||||
|
|
Loading…
Reference in a new issue