From 3cb044cde277d55897734ad50f44d2181c3504db Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 8 Mar 2024 17:42:32 +0000 Subject: [PATCH] Refactor rune modules --- src/scripting/api.rs | 41 ------------------------------------ src/scripting/mod.rs | 2 +- src/scripting/modules/log.rs | 39 ++++++++++++++++++++++++++++++++++ src/scripting/modules/mod.rs | 1 + src/scripting/runtime.rs | 4 ++-- 5 files changed, 43 insertions(+), 44 deletions(-) delete mode 100644 src/scripting/api.rs create mode 100644 src/scripting/modules/log.rs create mode 100644 src/scripting/modules/mod.rs diff --git a/src/scripting/api.rs b/src/scripting/api.rs deleted file mode 100644 index 653eddb..0000000 --- a/src/scripting/api.rs +++ /dev/null @@ -1,41 +0,0 @@ -pub mod log { - use anyhow::Result; - use rune::Module; - - #[rune::module(::log)] - pub fn module() -> Result { - let mut module = Module::from_meta(self::module_meta)?.with_unique("log"); - module.function_meta(info)?; - module.function_meta(warn)?; - module.function_meta(error)?; - module.function_meta(trace)?; - module.function_meta(debug)?; - - Ok(module) - } - - #[rune::function] - pub fn info(message: &str) { - log::info!("{message}"); - } - - #[rune::function] - pub fn warn(message: &str) { - log::warn!("{message}"); - } - - #[rune::function] - pub fn error(message: &str) { - log::error!("{message}"); - } - - #[rune::function] - pub fn trace(message: &str) { - log::trace!("{message}"); - } - - #[rune::function] - pub fn debug(message: &str) { - log::debug!("{message}"); - } -} diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index 8cfccbd..fd46712 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -1,4 +1,4 @@ -pub mod api; +pub mod modules; mod runtime; pub use runtime::Runtime; diff --git a/src/scripting/modules/log.rs b/src/scripting/modules/log.rs new file mode 100644 index 0000000..9fc0191 --- /dev/null +++ b/src/scripting/modules/log.rs @@ -0,0 +1,39 @@ +use anyhow::Result; +use rune::Module; + +#[rune::module(::log)] +pub fn module() -> Result { + let mut module = Module::from_meta(self::module_meta)?.with_unique("log"); + module.function_meta(info)?; + module.function_meta(warn)?; + module.function_meta(error)?; + module.function_meta(trace)?; + module.function_meta(debug)?; + + Ok(module) +} + +#[rune::function] +pub fn info(message: &str) { + log::info!("{message}"); +} + +#[rune::function] +pub fn warn(message: &str) { + log::warn!("{message}"); +} + +#[rune::function] +pub fn error(message: &str) { + log::error!("{message}"); +} + +#[rune::function] +pub fn trace(message: &str) { + log::trace!("{message}"); +} + +#[rune::function] +pub fn debug(message: &str) { + log::debug!("{message}"); +} diff --git a/src/scripting/modules/mod.rs b/src/scripting/modules/mod.rs new file mode 100644 index 0000000..f4ee9bc --- /dev/null +++ b/src/scripting/modules/mod.rs @@ -0,0 +1 @@ +pub mod log; diff --git a/src/scripting/runtime.rs b/src/scripting/runtime.rs index fa05724..1fb3ba9 100644 --- a/src/scripting/runtime.rs +++ b/src/scripting/runtime.rs @@ -7,7 +7,7 @@ use rune::{ }; use walkdir::WalkDir; -use super::api; +use super::modules; pub struct Runtime { pub vm: Vm, @@ -17,7 +17,7 @@ pub struct Runtime { impl Runtime { pub fn new(source_dir: &str) -> Result { let mut context = Context::with_default_modules()?; - context.install(api::log::module()?)?; + context.install(modules::log::module()?)?; let runtime = Arc::new(context.runtime()?); let mut diagnostics = Diagnostics::new();