From b835e5b2e964c71a75fc8ac0f694c59eccb79aa0 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Thu, 26 Dec 2019 17:01:45 -0500 Subject: [PATCH] move error to own file --- src/error.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 40 ++-------------------------------------- 2 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..ea184c8 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,38 @@ +use std::fmt; + +#[derive(Debug)] +#[allow(clippy::enum_variant_names)] +pub enum BunBunError { + IoError(std::io::Error), + ParseError(serde_yaml::Error), + WatchError(hotwatch::Error), + LoggerInitError(log::SetLoggerError), +} + +impl fmt::Display for BunBunError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + BunBunError::IoError(e) => e.fmt(f), + BunBunError::ParseError(e) => e.fmt(f), + BunBunError::WatchError(e) => e.fmt(f), + BunBunError::LoggerInitError(e) => e.fmt(f), + } + } +} + +/// 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 { + fn from(e: $from) -> Self { + BunBunError::$to(e) + } + } + }; +} + +from_error!(std::io::Error, IoError); +from_error!(serde_yaml::Error, ParseError); +from_error!(hotwatch::Error, WatchError); +from_error!(log::SetLoggerError, LoggerInitError); diff --git a/src/main.rs b/src/main.rs index a914f71..fb52431 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use actix_web::middleware::Logger; use actix_web::{App, HttpServer}; use clap::{crate_authors, crate_version, load_yaml, App as ClapApp}; +use error::BunBunError; use handlebars::Handlebars; use hotwatch::{Event, Hotwatch}; use libc::daemon; @@ -8,54 +9,17 @@ use log::{debug, error, info, trace, warn}; use serde::{Deserialize, Serialize}; use std::cmp::min; use std::collections::HashMap; -use std::fmt; use std::fs::{read_to_string, OpenOptions}; use std::io::Write; use std::sync::{Arc, RwLock}; use std::time::Duration; +mod error; mod routes; mod template_args; const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml"); -#[derive(Debug)] -#[allow(clippy::enum_variant_names)] -enum BunBunError { - IoError(std::io::Error), - ParseError(serde_yaml::Error), - WatchError(hotwatch::Error), - LoggerInitError(log::SetLoggerError), -} - -impl fmt::Display for BunBunError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - BunBunError::IoError(e) => e.fmt(f), - BunBunError::ParseError(e) => e.fmt(f), - BunBunError::WatchError(e) => e.fmt(f), - BunBunError::LoggerInitError(e) => e.fmt(f), - } - } -} - -/// 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 { - fn from(e: $from) -> Self { - BunBunError::$to(e) - } - } - }; -} - -from_error!(std::io::Error, IoError); -from_error!(serde_yaml::Error, ParseError); -from_error!(hotwatch::Error, WatchError); -from_error!(log::SetLoggerError, LoggerInitError); - /// Dynamic variables that either need to be present at runtime, or can be /// changed during runtime. pub struct State {