From bf0137c3fc6093df085b9d95cd251b7f67d94815 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 29 May 2019 21:51:49 +0200 Subject: Create webhogg project directory --- WebInterface/wasm/webhogg/src/app.rs | 15 +++++++ WebInterface/wasm/webhogg/src/client_logger.rs | 46 ++++++++++++++++++++++ WebInterface/wasm/webhogg/src/lib.rs | 25 ++++++++++++ WebInterface/wasm/webhogg/src/webhogg_exception.rs | 25 ++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 WebInterface/wasm/webhogg/src/app.rs create mode 100644 WebInterface/wasm/webhogg/src/client_logger.rs create mode 100644 WebInterface/wasm/webhogg/src/lib.rs create mode 100644 WebInterface/wasm/webhogg/src/webhogg_exception.rs (limited to 'WebInterface/wasm/webhogg/src') diff --git a/WebInterface/wasm/webhogg/src/app.rs b/WebInterface/wasm/webhogg/src/app.rs new file mode 100644 index 0000000..bee7c08 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/app.rs @@ -0,0 +1,15 @@ +use crate::webhogg_exception::WebhoggException; + +pub struct WebhoggApplication { +} + +impl WebhoggApplication { + pub fn new() -> Result { + Ok(Self { + }) + } + + pub fn run(&mut self) -> Result<(), WebhoggException> { + Ok(()) + } +} diff --git a/WebInterface/wasm/webhogg/src/client_logger.rs b/WebInterface/wasm/webhogg/src/client_logger.rs new file mode 100644 index 0000000..f71918f --- /dev/null +++ b/WebInterface/wasm/webhogg/src/client_logger.rs @@ -0,0 +1,46 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace=console, js_name=debug)] + fn __console_debug_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=info)] + fn __console_info_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=warn)] + fn __console_warn_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=error)] + fn __console_error_colored2(f: &str, c1: &str, c2: &str); +} + +fn log(rec: &log::Record) { + let log_fn = match rec.level() { + log::Level::Trace | log::Level::Debug => __console_debug_colored2, + log::Level::Info => __console_info_colored2, + log::Level::Warn => __console_warn_colored2, + log::Level::Error => __console_error_colored2, + }; + log_fn(&format!("{}", rec.args()), + &format!("color: {}", match rec.level() { + log::Level::Trace => "violet", + log::Level::Debug => "blue", + log::Level::Info => "green", + log::Level::Warn => "orange", + log::Level::Error => "red" + }), ""); +} + +pub fn init_logger() { + fern::Dispatch::new().format(|out, message, record|{ + out.finish(format_args!( + "%c{}%c {} > {}", + record.level(), + record.target(), + message + ) + ) + }) + .level(log::LevelFilter::Debug) + .chain(fern::Output::call(log)) + .apply().unwrap(); +} + diff --git a/WebInterface/wasm/webhogg/src/lib.rs b/WebInterface/wasm/webhogg/src/lib.rs new file mode 100644 index 0000000..8b75ee1 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/lib.rs @@ -0,0 +1,25 @@ +mod client_logger; +mod webhogg_exception; +mod app; + +use wasm_bindgen::prelude::*; +use app::WebhoggApplication as App; + +#[macro_use] +extern crate log; + +fn run_application() { + match App::new().and_then(|mut app| app.run()) { + Ok(_) => info!("program terminated successfully"), + Err(e) => error!("program terminated with failure > {}", e) + } +} + +#[wasm_bindgen(start)] +pub fn entry() { + client_logger::init_logger(); + + info!("begin running wasm application"); + + run_application() +} diff --git a/WebInterface/wasm/webhogg/src/webhogg_exception.rs b/WebInterface/wasm/webhogg/src/webhogg_exception.rs new file mode 100644 index 0000000..eac67c7 --- /dev/null +++ b/WebInterface/wasm/webhogg/src/webhogg_exception.rs @@ -0,0 +1,25 @@ +use std::error::Error; + +#[derive(Debug)] +pub enum WebhoggException { +} + +impl Error for WebhoggException { + fn description(&self) -> &str { + "webhogg exception" + } + + fn cause(&self) -> Option<&dyn Error> { + self.source() + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + None + } +} + +impl std::fmt::Display for WebhoggException { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "WebhoggException: {}", self.description()) + } +} -- cgit v1.2.3-54-g00ecf