//! Convenience macros. /// Shorthand to create a token from the provided string. #[macro_export] macro_rules! token { ($s:expr) => {{ use std::str::FromStr; $crate::token::Token::from_str($s).unwrap() }}; } /// Shorthand to construct a `Transition`. /// /// Give just a name for a simple transition, or a name and a guard expression for a guarded /// transition. #[macro_export] macro_rules! transition { ($name:expr) => { $crate::transition::Transition::new($name) }; ($name:expr, $guard:expr) => { $crate::transition::Transition::new($name) .with_guard($crate::transition::Guard::new($guard)) }; } /// Shorthand to construct an `ArcSpec`. This exists mostly just to be used by the `arcs!` macro. #[macro_export] macro_rules! arc { ($from:expr => $to:expr, $expression:expr) => { $crate::arc::ArcSpec { from: $from, to: $to, inner: $crate::arc::BindingOrExpression::from($expression), } }; } /// Shorthand to construct an iterable of `ArcSpec`s. /// /// # Examples /// /// Simple arcs with default expressions: /// ``` /// # use cpnets::prelude::*; /// # fn main() -> Result<(), Box> { /// let mut net = Net::new("N"); /// net.add_place("P1")?; /// net.add_place("P2")?; /// net.add_transition(Transition::new("T1"))?; /// net.add_arcs(arcs! { /// "P1" => "T1"; "x", /// "T1" => "P2"; expression!("x", &["x"]), /// })?; /// # Ok(()) /// # } /// ``` /// /// Arcs with optionally more advanced expressions: /// ``` /// # use cpnets::prelude::*; /// # fn main() -> Result<(), Box> { /// let mut net = Net::new("N"); /// net.add_place("P1")?; /// net.add_place("P2")?; /// net.add_transition(Transition::new("T1"))?; /// net.add_arcs(arcs! { /// "P1" => "T1"; "x", /// "T1" => "P2"; expression!("x % 42", &["x"]), /// })?; /// # Ok(()) /// # } /// ``` #[macro_export] macro_rules! arcs { ($($from:expr => $to:expr; $expression:expr),+ $(,)?) => {{ [$(arc! { $from => $to, $expression }),+] }} } /// Shorthand to create an `Expression`. /// /// An expression is attached to an arc from a transition to a place. It may modify or replace its /// input(s) freely. #[macro_export] macro_rules! expression { ($expression:expr, $inputs:expr) => { $crate::expression::Expression::new($expression, $inputs) }; } #[cfg(test)] mod tests { #[test] fn basic_token_test() { let token = token!("42"); let value: i32 = token.extract().unwrap(); assert_eq!(value, 42); } }