pub struct MachineBuilder { /* private fields */ }Expand description
A builder for constructing a Machine.
Transitions are added with MachineBuilder::try_allow and validated
immediately before being stored.
MachineBuilder::build only succeeds when at least one valid transition
has been configured.
State names are case-sensitive. Empty or whitespace-only state names, names with leading or trailing whitespace, and self-transitions are rejected. Cycles are allowed.
Implementations§
Source§impl MachineBuilder
impl MachineBuilder
Sourcepub fn allow(self, from: impl AsRef<str>, to: impl AsRef<str>) -> Self
👎Deprecated since 0.2.0: use try_allow to handle invalid transitions without panicking
pub fn allow(self, from: impl AsRef<str>, to: impl AsRef<str>) -> Self
use try_allow to handle invalid transitions without panicking
Adds an allowed transition from one state to another.
Adding the same transition more than once has no additional effect.
§Panics
Panics if either state name is invalid or the transition is a self-transition.
Sourcepub fn try_allow(
self,
from: impl AsRef<str>,
to: impl AsRef<str>,
) -> Result<Self, StateError>
pub fn try_allow( self, from: impl AsRef<str>, to: impl AsRef<str>, ) -> Result<Self, StateError>
Adds an allowed transition from one state to another.
Adding the same transition more than once has no additional effect.
§Errors
Returns:
StateError::AmbiguousStateNameif an endpoint has leading or trailing Unicode whitespace.StateError::SelfTransitionif a transition has identical endpoints.StateError::EmptyStateif an endpoint is empty or whitespace-only.
§Examples
use statekit::{Machine, StateError};
let result = Machine::builder().try_allow("a", "a");
assert!(matches!(
result,
Err(StateError::SelfTransition { state }) if state == "a"
));Sourcepub fn build(self) -> Result<Machine, StateError>
pub fn build(self) -> Result<Machine, StateError>
Builds an immutable Machine.
§Errors
Returns StateError::NoTransitions if no transitions were configured.
§Examples
use statekit::{Machine, StateError};
let builder = Machine::builder();
assert!(matches!(
builder.build(),
Err(StateError::NoTransitions)
));Sourcepub fn transition_count(&self) -> usize
pub fn transition_count(&self) -> usize
Returns the number of unique transitions added to the state machine.
§Examples
use statekit::{Machine, StateError};
let builder = Machine::builder()
.try_allow("a", "b")?
.try_allow("a", "b")?
.try_allow("b", "c")?;
assert_eq!(builder.transition_count(), 2);Sourcepub fn state_count(&self) -> usize
pub fn state_count(&self) -> usize
Returns the number of unique states used by the configured transitions.
§Examples
use statekit::{Machine, StateError};
let builder = Machine::builder()
.try_allow("a", "b")?
.try_allow("a", "c")?
.try_allow("b", "c")?;
assert_eq!(builder.state_count(), 3);