rigz_ast_derive

Crates.iorigz_ast_derive
lib.rsrigz_ast_derive
version0.4.0
sourcesrc
created_at2024-10-29 04:57:59.929933
updated_at2024-11-04 20:09:29.138413
descriptionProcedural macro to generate ParsedModules for rigz, generate a trait for the module implementation and parse input at compile time.
homepage
repositoryhttps://gitlab.com/inapinch/rigz/crates/ast_derive
max_upload_size
id1426574
size34,381
(inapinch-io)

documentation

README

rigz_ast_derive

Generate a trait, Module impl, and ParsedModule impl for static rigz input at compile time, otherwise modules are parsed and validated at runtime.

Example

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}"))),
        }
    }
}
Commit count: 204

cargo fmt