Skip to main content

MachineBuilder

Struct MachineBuilder 

Source
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

Source

pub 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

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.

Source

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:

§Examples
use statekit::{Machine, StateError};

let result = Machine::builder().try_allow("a", "a");

assert!(matches!(
    result,
    Err(StateError::SelfTransition { state }) if state == "a"
));
Source

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)
));
Source

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);
Source

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);

Trait Implementations§

Source§

impl Debug for MachineBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.