diff --git a/src/lib/event.rs b/src/lib/event.rs new file mode 100644 index 0000000..acd46e9 --- /dev/null +++ b/src/lib/event.rs @@ -0,0 +1,31 @@ +/// + + +trait Eventable { + key() -> +} + +struct EventManager {} + +impl EventManager { + fn new() -> Self { + EventManager {} + } + + fn on(&mut self, event: E, callback: dyn Fn(E)) { + // + } + + fn send(&self, event: E) {} +} + +fn test() { + enum Events { + A, + B(i32), + C(f64), + } + + let event_manager = EventManager::new::(); + event_manager.on(Events::A, || {}) +} diff --git a/src/lib/harsh.rs b/src/lib/harsh.rs index 92b6bda..2f7dbb0 100644 --- a/src/lib/harsh.rs +++ b/src/lib/harsh.rs @@ -1,6 +1,6 @@ use super::config::Configuration; use super::database::DbManager; -use super::http::serve; +use super::http::HttpManager; use super::log::Logger; /// @@ -8,7 +8,26 @@ use super::log::Logger; /// pub fn main() { let configuration = Configuration::read(); - Logger::configure(&configuration); - let mut db_manager = DbManager::new(&configuration); - serve(&configuration, &mut db_manager); +} + +pub struct Harsh { + db_manager: DbManager, + logger: Logger, + http_manager: HttpManager, +} + +impl Harsh { + pub fn new(configuration: Configuration) -> Self { + let db_manager = DbManager::new(&configuration); + let logger = Logger::new(&configuration); + let http_manager = HttpManager::new(); + + Harsh { + logger, + db_manager, + http_manager, + } + } + + fn serve(&mut self) {} } diff --git a/src/lib/http.rs b/src/lib/http.rs index 5da81d1..ebdca62 100644 --- a/src/lib/http.rs +++ b/src/lib/http.rs @@ -2,8 +2,19 @@ use super::config::Configuration; use super::database::DbManager; /// -/// listen for and handle received requests +/// listen for incoming requests and handle both incoming and outgoing requests /// -pub fn serve(_config: &Configuration, _db_manager: &mut DbManager) { - todo!() +pub struct HttpManager { + // +} + +impl HttpManager { + pub fn new() -> Self { + HttpManager {} + } + + /// listen for and handle received requests + pub fn serve(_config: &Configuration, _db_manager: &mut DbManager) { + todo!() + } } diff --git a/src/lib/log.rs b/src/lib/log.rs index a8e5884..c889214 100644 --- a/src/lib/log.rs +++ b/src/lib/log.rs @@ -1,59 +1,39 @@ -use std::cell::RefCell; - use super::config::Configuration; use chrono::prelude::*; use colored::Colorize; /// -/// a static custom logger +/// configurable structure with methods to log informations in different ways /// pub struct Logger { // } -/// static Logger instance -pub const LOGGER: RefCell = RefCell::new(Logger {}); - impl Logger { /// adapt the static instance according to parameters specified by the configuration - pub fn configure(_config: &Configuration) { - let refcell = LOGGER; - let _logger = refcell.try_borrow_mut().unwrap(); - todo!() + pub fn new(_config: &Configuration) -> Self { + Logger {} } - fn log_info(&self, msg: String) { + pub fn info(&self, msg: String) { let tag_section = "[info]".blue(); - println!("{} {} {}", time_section(), tag_section, msg); + println!("{} {} {}", time_segment(), tag_section, msg); } - fn log_warn(&self, msg: String) { + pub fn warn(&self, msg: String) { let tag_section = "[warn]".yellow(); - println!("{} {} {}", time_section(), tag_section, msg); + println!("{} {} {}", time_segment(), tag_section, msg); } - fn log_fail(&self, msg: String) { + pub fn fail(&self, msg: String) { let tag_section = "[fail]".red(); - println!("{} {} {}", time_section(), tag_section, msg); + println!("{} {} {}", time_segment(), tag_section, msg); } } -fn time_section() -> String { +/// make the time segment of a log +fn time_segment() -> String { let now = Local::now(); let result = now.format("[%Y.%m.%d-%H:%M:%S]").to_string(); result } - -pub mod shorthands { - pub fn log_info(msg: String) { - super::LOGGER.try_borrow().unwrap().log_info(msg); - } - - pub fn log_warn(msg: String) { - super::LOGGER.try_borrow().unwrap().log_warn(msg); - } - - pub fn log_fail(msg: String) { - super::LOGGER.try_borrow().unwrap().log_fail(msg); - } -} diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 4091b14..c0f56db 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -1,5 +1,6 @@ pub mod config; pub mod database; +pub mod event; pub mod harsh; pub mod http; pub mod log;