#proc-macro #rigz #traits #module #parse-input #generate #compile-time

macro rigz_ast_derive

Procedural macro to generate ParsedModules for rigz, generate a trait for the module implementation and parse input at compile time

3 releases (breaking)

new 0.4.0 Nov 4, 2024
0.2.0 Oct 30, 2024
0.1.0 Oct 29, 2024

#8 in #rigz

Download history 271/week @ 2024-10-28

271 downloads per month
Used in 2 crates (via rigz_runtime)

MIT license

28KB
685 lines

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}"))),
        }
    }
}

Dependencies

~4.5MB
~63K SLoC