| Crates.io | rigz_ast_derive |
| lib.rs | rigz_ast_derive |
| version | 0.5.0 |
| created_at | 2024-10-29 04:57:59.929933+00 |
| updated_at | 2025-01-10 21:16:12.354663+00 |
| 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 | 44,153 |
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;
// These imports are only required if an extension function is used
use std::rc::Rc;
use std::cell::RefCell;
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}"))),
}
}
}