converted main library module to a more structural architecture

This commit is contained in:
Jolimaitre Matthieu 2021-11-12 15:27:39 +01:00
parent b95003257d
commit 3c9f92a1ab
5 changed files with 80 additions and 38 deletions

31
src/lib/event.rs Normal file
View file

@ -0,0 +1,31 @@
///
trait Eventable {
key() ->
}
struct EventManager<Events> {}
impl<Events> EventManager<Events> {
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::<Events>();
event_manager.on(Events::A, || {})
}

View file

@ -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) {}
}

View file

@ -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 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!()
}
}

View file

@ -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<Logger> = 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);
}
}

View file

@ -1,5 +1,6 @@
pub mod config;
pub mod database;
pub mod event;
pub mod harsh;
pub mod http;
pub mod log;