#![cfg_attr(not(feature = "std"), no_std)]
use vm_core::{
errors::LibraryError,
utils::{collections::BTreeMap, string::ToString},
Library,
};
mod asm;
use asm::MODULES;
const VERSION: &str = env!("CARGO_PKG_VERSION");
type ModuleMap = BTreeMap<&'static str, &'static str>;
pub struct StdLibrary {
modules: ModuleMap,
}
impl Library for StdLibrary {
fn root_ns(&self) -> &str {
"std"
}
fn version(&self) -> &str {
VERSION
}
fn get_module_source(&self, module_path: &str) -> Result<&str, LibraryError> {
let source = self
.modules
.get(module_path)
.ok_or_else(|| LibraryError::ModuleNotFound(module_path.to_string()))?;
Ok(source)
}
}
impl Default for StdLibrary {
fn default() -> Self {
let mut modules = BTreeMap::new();
for (ns, source) in MODULES {
modules.insert(ns, source);
}
Self { modules }
}
}
#[cfg(test)]
mod tests {
use super::Library;
#[test]
fn lib_version() {
let stdlib = super::StdLibrary::default();
assert_eq!("0.1.0", stdlib.version())
}
}