1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::{ContextError, Module, Panic};
use std::fmt;
pub fn module() -> Result<Module, ContextError> {
let mut module = Module::new(&["std", "test"]);
module.function(&["assert"], assert)?;
Ok(module)
}
#[derive(Debug)]
struct AssertionFailed(String);
impl fmt::Display for AssertionFailed {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "assertion failed `{}`", self.0)
}
}
fn assert(value: bool, message: &str) -> Result<(), Panic> {
if !value {
return Err(Panic::custom(AssertionFailed(message.to_string())));
}
Ok(())
}