Refactor rune modules

This commit is contained in:
Jarrod Doyle 2024-03-08 17:42:32 +00:00
parent 6f8c99ed4c
commit 3cb044cde2
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
5 changed files with 43 additions and 44 deletions

View File

@ -1,41 +0,0 @@
pub mod log {
use anyhow::Result;
use rune::Module;
#[rune::module(::log)]
pub fn module() -> Result<Module> {
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}");
}
}

View File

@ -1,4 +1,4 @@
pub mod api; pub mod modules;
mod runtime; mod runtime;
pub use runtime::Runtime; pub use runtime::Runtime;

View File

@ -0,0 +1,39 @@
use anyhow::Result;
use rune::Module;
#[rune::module(::log)]
pub fn module() -> Result<Module> {
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}");
}

View File

@ -0,0 +1 @@
pub mod log;

View File

@ -7,7 +7,7 @@ use rune::{
}; };
use walkdir::WalkDir; use walkdir::WalkDir;
use super::api; use super::modules;
pub struct Runtime { pub struct Runtime {
pub vm: Vm, pub vm: Vm,
@ -17,7 +17,7 @@ pub struct Runtime {
impl Runtime { impl Runtime {
pub fn new(source_dir: &str) -> Result<Self> { pub fn new(source_dir: &str) -> Result<Self> {
let mut context = Context::with_default_modules()?; let mut context = Context::with_default_modules()?;
context.install(api::log::module()?)?; context.install(modules::log::module()?)?;
let runtime = Arc::new(context.runtime()?); let runtime = Arc::new(context.runtime()?);
let mut diagnostics = Diagnostics::new(); let mut diagnostics = Diagnostics::new();