oxigdal_algorithms/dsl/macro_support.rs
1//! Macro support for compile-time DSL
2//!
3//! This module provides the `raster!` macro for compile-time DSL evaluation.
4
5/// Macro for compile-time raster algebra expressions
6///
7/// # Examples
8///
9/// ```ignore
10/// use oxigdal_algorithms::raster;
11///
12/// // Simple NDVI calculation
13/// let ndvi = raster!((NIR - RED) / (NIR + RED));
14///
15/// // Complex multi-band analysis with conditions
16/// let result = raster! {
17/// let ndvi = (B8 - B4) / (B8 + B4);
18/// if ndvi > 0.6 then 1 else 0
19/// };
20/// ```
21#[macro_export]
22macro_rules! raster {
23 // Single expression
24 ($expr:expr) => {{
25 use $crate::dsl::{parse_expression, CompiledProgram};
26 use $crate::dsl::ast::{Program, Statement};
27
28 let expr_str = stringify!($expr);
29 let parsed = parse_expression(expr_str)?;
30 let program = Program {
31 statements: vec![Statement::Expr(Box::new(parsed))],
32 };
33 CompiledProgram::new(program)
34 }};
35
36 // Block with multiple statements
37 ({ $($stmt:stmt);+ }) => {{
38 use $crate::dsl::{parse_program, CompiledProgram};
39
40 let program_str = stringify!({ $($stmt);+ });
41 let parsed = parse_program(program_str)?;
42 CompiledProgram::new(parsed)
43 }};
44}
45
46/// Helper macro for defining custom DSL functions
47///
48/// # Examples
49///
50/// ```ignore
51/// dsl_function! {
52/// fn ndvi(nir, red) = (nir - red) / (nir + red);
53/// }
54/// ```
55#[macro_export]
56macro_rules! dsl_function {
57 (fn $name:ident ( $($param:ident),* ) = $body:expr ;) => {
58 pub fn $name() -> &'static str {
59 concat!(
60 "fn ",
61 stringify!($name),
62 "(",
63 $( stringify!($param), "," ),*
64 ") = ",
65 stringify!($body),
66 ";"
67 )
68 }
69 };
70}
71
72#[cfg(test)]
73mod tests {
74 #[test]
75 fn test_macro_expansion() {
76 // This test just ensures the macros compile
77 // Actual functionality is tested in integration tests
78 }
79}