Quick Start

Install

Add the core library (and ZIO, which Mechanoid marks as provided).

libraryDependencies += "rocks.earlyeffect" %% "mechanoid" % "0.8.0"
libraryDependencies += "dev.zio" %% "zio" % "2.1.26"

// Optional PostgreSQL persistence
libraryDependencies += "rocks.earlyeffect" %% "mechanoid-postgres" % "0.8.0"

Release tags are v*. Maven Central badges on the GitHub repo also track the latest publish.

Define and run

assembly validates transitions at compile time. Machine(...) makes the assembly runnable. start gives you an in-memory FSMRuntime scoped to the ZIO scope.

{
  import mechanoid.*
  import zio.*

  enum OrderState derives Finite:
    case Pending, Paid, Shipped

  enum OrderEvent derives Finite:
    case Pay, Ship

  import OrderState.*, OrderEvent.*

  val orderMachine = Machine(
    assembly[OrderState, OrderEvent](
      Pending via Pay to Paid,
      Paid via Ship to Shipped,
    )
  )

  ZIO.scoped {
    for
      fsm   <- orderMachine.start(Pending)
      _     <- fsm.send(Pay)
      _     <- fsm.send(Ship)
      state <- fsm.currentState
    yield state
  }.asDoc
}
Shipped

Next: Core Concepts for states, events, and hierarchy, or Testing for how DocSpecs assert.