machine

package
v0.0.0-...-a26ebea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2022 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStatesMustBeSet = validation.NewError(
	"machine_states_must_be_set",
	"the machine states must be set and cannot be an empty",
)

ErrStatesMustBeSet is the validation check error returned when the machine states are empty.

View Source
var NoOp = func() error { return nil }

Functions

This section is empty.

Types

type Action

type Action func() error

type ErrAlreadyInState

type ErrAlreadyInState[T state.State] struct {
	State T
}

ErrAlreadyInState returned when machine attempts to transition into its current state.

func (*ErrAlreadyInState[T]) Error

func (e *ErrAlreadyInState[T]) Error() string

type ErrInvalidTransition

type ErrInvalidTransition[T state.State] struct {
	Current T
	Next    T
}

ErrInvalidTransition returned when call requests a transition between states that isn't registered.

func (*ErrInvalidTransition[T]) Error

func (e *ErrInvalidTransition[T]) Error() string

type ErrNotInState

type ErrNotInState[T state.State] struct {
	State T
}

ErrNotInState returned when caller asserts a machine to be in a specific state and it isn't.

func (*ErrNotInState[T]) Error

func (e *ErrNotInState[T]) Error() string

type Machine

type Machine[T state.State] struct {
	// contains filtered or unexported fields
}

Machine represents thread-safe state machine.

func Generate

func Generate[T state.State](rand *rand.Rand) *Machine[T]

Generate a random Machine value.

func New

func New[T state.State](
	initial T,
	states []T,
	table transition.Table[T],
) (*Machine[T], validation.ErrorGroup)

New creates and validates a new Machine.

func (*Machine[T]) Check

func (m *Machine[T]) Check() validation.ErrorGroup

Check runs default validation checks for the Machine.

func (*Machine[T]) Current

func (m *Machine[T]) Current() T

Current returns the current state of the machine.

func (*Machine[T]) Generate

func (*Machine[T]) Generate(rand *rand.Rand, size int) reflect.Value

Generate random Machine values.

Interface: quick.Generator

func (*Machine[T]) In

func (m *Machine[T]) In(s T) bool

In is true when the machine is in the given state.

func (*Machine[T]) MustBe

func (m *Machine[T]) MustBe(s T, a Action) error

MustBe invokes the given action if the machine is in the expected state.

If not in the expected state, ErrNotInState is returned.

func (*Machine[T]) String

func (m *Machine[T]) String() string

String value of the Machine.

Interface: fmt.Stringer.

func (*Machine[T]) To

func (m *Machine[T]) To(s T, t Transition) error

To transitions the state machine to the given state.

type Transition

type Transition func() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL