Skip to main content

pattern

Macro pattern 

Source
macro_rules! pattern {
    {
        id: $id:expr;
        numeric: $numeric:expr;
        knots {
            $( $knot:ident $(as $alias:literal)? = $kind:expr; )*
        }
        exports {
            $( input $input_name:ident = $input_knot:ident . $input_port:ident; )*
            $( output $output_name:ident = $output_knot:ident . $output_port:ident; )*
        }
        threads {
            $( $from:ident . $from_port:ident -> $to:ident . $to_port:ident; )*
        }
    } => { ... };
    {
        id: $id:expr;
        knots {
            $( $knot:ident $(as $alias:literal)? = $kind:expr; )*
        }
        exports {
            $( input $input_name:ident = $input_knot:ident . $input_port:ident; )*
            $( output $output_name:ident = $output_knot:ident . $output_port:ident; )*
        }
        threads {
            $( $from:ident . $from_port:ident -> $to:ident . $to_port:ident; )*
        }
    } => { ... };
}
Expand description

Builds and validates a reusable Pattern.

The declaration order is id, optional numeric, knots, exports, then threads. Exports name input and output ports that the enclosing weave! can connect through in(name) and out(name). Knot aliases control the authored ids used by exports and threads.

The macro evaluates each expression once in source order and delegates all structural and export validation to Pattern::try_from. It returns Result<Pattern, BuildError>.

use wyrd::{pattern, KnotKind, Pattern};

fn pulse() -> Result<Pattern, wyrd::BuildError> {
    pattern! {
        id: "pulse";
        knots {
            edge as "edge.detect" = KnotKind::rising_from_zero();
            timer = KnotKind::timer(wyrd::TimerMode::PulseHold, 2);
        }
        exports {
            input start = edge.in;
            output active = timer.active;
        }
        threads {
            edge.out -> timer.start;
        }
    }
}