state_machines/lib.rs
1#![no_std]
2#![allow(clippy::needless_doctest_main)]
3#![cfg_attr(test, allow(non_camel_case_types, non_snake_case))]
4#![doc = include_str!("../README.md")]
5
6pub mod core {
7 pub use state_machines_core::*;
8}
9
10pub use state_machines_core::{
11 AroundOutcome, AroundStage, DynamicError, EventDefinition, Machine, MachineDefinition,
12 MachineState, SubstateOf, SuperstateDefinition, TransitionContext, TransitionDefinition,
13 TransitionError, TransitionErrorKind, TransitionResult,
14};
15pub use state_machines_macro::state_machine;
16
17/// Convenience macro for aborting an around callback with a guard-style error.
18///
19/// ```rust,ignore
20/// use state_machines::{abort_guard, core::{AroundOutcome, AroundStage, TransitionContext}};
21///
22/// fn guard(
23/// ctx: &TransitionContext<MyState>,
24/// stage: AroundStage,
25/// ) -> AroundOutcome<MyState> {
26/// if matches!(stage, AroundStage::Before) && !check_resources() {
27/// return abort_guard!(ctx, check_resources);
28/// }
29/// AroundOutcome::Proceed
30/// }
31/// ```
32#[macro_export]
33macro_rules! abort_guard {
34 ($ctx:expr, $guard:ident) => {
35 $crate::core::AroundOutcome::Abort($crate::core::TransitionError::guard_failed(
36 $ctx.from,
37 $ctx.event,
38 stringify!($guard),
39 ))
40 };
41 ($ctx:expr, $guard:expr) => {
42 $crate::core::AroundOutcome::Abort($crate::core::TransitionError::guard_failed(
43 $ctx.from, $ctx.event, $guard,
44 ))
45 };
46}
47
48/// Build a custom transition error from within an around callback.
49///
50/// ```rust,ignore
51/// use state_machines::{abort_with, core::{AroundOutcome, TransitionContext, TransitionErrorKind}};
52///
53/// fn guard(ctx: &TransitionContext<MyState>) -> AroundOutcome<MyState> {
54/// if quota_exceeded() {
55/// return abort_with!(ctx, TransitionErrorKind::ActionFailed { action: "quota_check" });
56/// }
57/// AroundOutcome::Proceed
58/// }
59/// ```
60#[macro_export]
61macro_rules! abort_with {
62 ($ctx:expr, $kind:expr) => {
63 $crate::core::AroundOutcome::Abort($crate::core::TransitionError {
64 from: $ctx.from,
65 event: $ctx.event,
66 kind: $kind,
67 })
68 };
69}