#[derive(Clone, Default)]
pub struct HostState {
capabilities: Vec<String>,
logs: Vec<String>,
}
impl HostState {
pub fn new(capabilities: Vec<String>) -> Self {
Self {
capabilities,
logs: Vec::new(),
}
}
pub fn has_capability(&self, cap: &str) -> bool {
self.capabilities.contains(&cap.to_string())
}
pub fn log_message(&mut self, msg: &str) {
self.logs.push(msg.to_string());
}
pub fn logs(&self) -> &[String] {
&self.logs
}
pub fn clear_logs(&mut self) {
self.logs.clear();
}
}
pub fn register_host_functions(
_linker: &mut HostState,
) -> Result<(), String> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_host_state() {
let state = HostState::new(vec!["log".to_string(), "hash".to_string()]);
assert!(state.has_capability("log"));
assert!(!state.has_capability("fs_write"));
}
#[test]
fn test_host_logging() {
let mut state = HostState::new(vec!["log".to_string()]);
state.log_message("test");
assert_eq!(state.logs(), &["test"]);
}
}