Crates.io | rigz_ast_derive |
lib.rs | rigz_ast_derive |
version | 0.4.0 |
source | src |
created_at | 2024-10-29 04:57:59.929933 |
updated_at | 2024-11-04 20:09:29.138413 |
description | Procedural macro to generate ParsedModules for rigz, generate a trait for the module implementation and parse input at compile time. |
homepage | |
repository | https://gitlab.com/inapinch/rigz/crates/ast_derive |
max_upload_size | |
id | 1426574 |
size | 34,381 |
Generate a trait, Module impl, and ParsedModule impl for static rigz input at compile time, otherwise modules are parsed and validated at runtime.
Shown below is the JSONModule used by the rigz_runtime.
Functions with a default implementation will not appear in the trait as they are handled by the runtime directly.
use rigz_ast::*;
use rigz_ast_derive::derive_module;
derive_module!(
r#"trait JSON
fn Any.to_json -> String!
fn parse(input: String) -> Any!
end"#
);
impl RigzJSON for JSONModule {
fn any_to_json(&self, value: Value) -> Result<String, VMError> {
match serde_json::to_string(&value) {
Ok(s) => Ok(s),
Err(e) => Err(VMError::RuntimeError(format!("Failed to write json - {e}"))),
}
}
fn parse(&self, input: String) -> Result<Value, VMError> {
match serde_json::from_str(input.as_str()) {
Ok(v) => Ok(v),
Err(e) => Err(VMError::RuntimeError(format!("Failed to parse json - {e}"))),
}
}
}