mindset 0.1.1

A pure functional state machine library built on Stillwater's Effect system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# Mindset Builder API Guide

A comprehensive guide to using Mindset's builder API for ergonomic state machine construction.

## Table of Contents

- [Getting Started]#getting-started
- [Core Concepts]#core-concepts
- [StateMachineBuilder]#statemachinebuilder
- [TransitionBuilder]#transitionbuilder
- [Common Patterns]#common-patterns
- [Macro Reference]#macro-reference
- [Advanced Usage]#advanced-usage
- [Error Handling]#error-handling

## Getting Started

The builder API provides a fluent interface for constructing state machines with minimal boilerplate while maintaining type safety. Instead of manually constructing state machine components, builders guide you through the required fields and validate at build time.

### Quick Example

```rust
use mindset::builder::{StateMachineBuilder, TransitionBuilder};
use mindset::state_enum;

// Define your states using the state_enum macro
state_enum! {
    enum OrderState {
        Draft,
        Submitted,
        Processing,
        Completed,
        Failed,
    }
    final: [Completed, Failed]
    error: [Failed]
}

// Build your state machine
let machine = StateMachineBuilder::new()
    .initial(OrderState::Draft)
    .add_transition(
        TransitionBuilder::new()
            .from(OrderState::Draft)
            .to(OrderState::Submitted)
            .succeeds()
            .build()
            .unwrap()
    )
    .add_transition(
        TransitionBuilder::new()
            .from(OrderState::Submitted)
            .to(OrderState::Processing)
            .succeeds()
            .build()
            .unwrap()
    )
    .build()
    .unwrap();
```

## Core Concepts

### Builder Pattern Benefits

1. **Compile-time validation**: Required fields are checked at build time
2. **Method chaining**: Fluent API for readable construction
3. **Type safety**: Generics ensure state and environment types match
4. **Clear errors**: Descriptive error messages for missing fields

### Builder Lifecycle

All builders follow a consistent lifecycle:

1. **Create** - Call `::new()` to create a builder
2. **Configure** - Chain methods to set required and optional fields
3. **Build** - Call `.build()` to validate and construct the final object

If required fields are missing, `.build()` returns a descriptive error.

## StateMachineBuilder

The `StateMachineBuilder` constructs complete state machines with initial states and transitions.

### Type Signature

```rust
pub struct StateMachineBuilder<S: State + 'static, Env: Clone + Send + Sync + 'static>
```

- `S`: Your state enum type (must implement `State` trait)
- `Env`: Environment type for effectful transitions (use `()` for pure machines)

### Required Fields

- **initial**: The initial state of the machine (required)
- **transitions**: At least one transition (required)

### Methods

#### `new() -> Self`

Creates a new builder instance.

```rust
let builder = StateMachineBuilder::<MyState, ()>::new();
```

#### `initial(state: S) -> Self`

Sets the initial state (required).

```rust
builder.initial(MyState::Start)
```

#### `add_transition(transition: Transition<S, Env>) -> Self`

Adds a pre-built transition to the machine.

```rust
builder.add_transition(my_transition)
```

#### `transition(builder: TransitionBuilder<S, Env>) -> Result<Self, BuildError>`

Adds a transition using a builder. Returns an error if the builder fails validation.

```rust
builder.transition(
    TransitionBuilder::new()
        .from(State::A)
        .to(State::B)
        .succeeds()
)?
```

#### `transitions(transitions: Vec<Transition<S, Env>>) -> Self`

Adds multiple transitions at once.

```rust
builder.transitions(vec![transition1, transition2])
```

#### `build() -> Result<StateMachine<S, Env>, BuildError>`

Validates and builds the state machine. Returns errors if:
- Initial state is not set
- No transitions are defined

### Complete Example

```rust
use mindset::builder::{StateMachineBuilder, TransitionBuilder};

state_enum! {
    enum LightState {
        Red,
        Yellow,
        Green,
    }
}

let machine = StateMachineBuilder::new()
    .initial(LightState::Red)
    .add_transition(
        TransitionBuilder::new()
            .from(LightState::Red)
            .to(LightState::Green)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(LightState::Green)
            .to(LightState::Yellow)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(LightState::Yellow)
            .to(LightState::Red)
            .succeeds()
            .build()?
    )
    .build()?;
```

## TransitionBuilder

The `TransitionBuilder` constructs individual state transitions with guards, actions, and enforcement rules.

### Type Signature

```rust
pub struct TransitionBuilder<S: State, Env>
```

### Required Fields

- **from**: Source state (required)
- **to**: Target state (required)
- **action**: Transition action/effect (required)

### Methods

#### `new() -> Self`

Creates a new transition builder.

```rust
let builder = TransitionBuilder::<MyState, ()>::new();
```

#### `from(state: S) -> Self`

Sets the source state (required).

```rust
builder.from(MyState::Draft)
```

#### `to(state: S) -> Self`

Sets the target state (required).

```rust
builder.to(MyState::Submitted)
```

#### `guard(guard: Guard<S>) -> Self`

Adds a guard object (optional).

```rust
builder.guard(my_guard)
```

#### `when<F>(predicate: F) -> Self`

Adds a guard using a closure (optional). More ergonomic than `guard()`.

```rust
builder.when(|state| state.is_ready())
```

#### `action<E>(effect: E) -> Self`

Sets a custom action effect (required, or use `succeeds()`).

```rust
builder.action(|| pure(TransitionResult::Success(MyState::Done)).boxed())
```

#### `succeeds() -> Self`

Sets a simple success action that transitions to the target state. Requires `.to()` to be called first.

```rust
builder
    .from(State::A)
    .to(State::B)
    .succeeds()  // Automatically creates success action to State::B
```

#### `enforce(rules: EnforcementRules<S>) -> Self`

Adds enforcement rules (optional).

```rust
use mindset::enforcement::EnforcementBuilder;
use std::time::Duration;

builder.enforce(
    EnforcementBuilder::new()
        .max_attempts(3)
        .timeout(Duration::from_secs(30))
        .build()
)
```

#### `build() -> Result<Transition<S, Env>, BuildError>`

Validates and builds the transition. Returns errors if:
- Source state is not set
- Target state is not set
- Action is not set

### Complete Example

```rust
use mindset::builder::TransitionBuilder;
use mindset::enforcement::EnforcementBuilder;
use std::time::Duration;

let transition = TransitionBuilder::new()
    .from(OrderState::Draft)
    .to(OrderState::Submitted)
    .when(|order| order.is_valid())
    .enforce(
        EnforcementBuilder::new()
            .max_attempts(3)
            .timeout(Duration::from_secs(10))
            .build()
    )
    .succeeds()
    .build()?;
```

## Common Patterns

### Pattern 1: Simple Linear Workflow

Build a straightforward sequence of states.

```rust
state_enum! {
    enum TaskState {
        Todo,
        InProgress,
        Review,
        Done,
    }
    final: [Done]
}

let machine = StateMachineBuilder::new()
    .initial(TaskState::Todo)
    .add_transition(
        TransitionBuilder::new()
            .from(TaskState::Todo)
            .to(TaskState::InProgress)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(TaskState::InProgress)
            .to(TaskState::Review)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(TaskState::Review)
            .to(TaskState::Done)
            .succeeds()
            .build()?
    )
    .build()?;
```

### Pattern 2: Branching with Guards

Add conditional transitions based on state properties.

```rust
state_enum! {
    enum PaymentState {
        Pending,
        Processing,
        Completed,
        Failed,
    }
    final: [Completed, Failed]
    error: [Failed]
}

struct Payment {
    amount: f64,
    state: PaymentState,
}

let machine = StateMachineBuilder::new()
    .initial(PaymentState::Pending)
    .add_transition(
        TransitionBuilder::new()
            .from(PaymentState::Pending)
            .to(PaymentState::Processing)
            .when(|_| true)  // Always allow this transition
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(PaymentState::Processing)
            .to(PaymentState::Completed)
            .when(|state| !state.is_error())  // Only if not in error state
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(PaymentState::Processing)
            .to(PaymentState::Failed)
            .when(|state| state.is_error())  // Only on error
            .succeeds()
            .build()?
    )
    .build()?;
```

### Pattern 3: Enforced Transitions

Add retry limits and timeouts to prevent infinite loops.

```rust
use mindset::enforcement::EnforcementBuilder;
use std::time::Duration;

state_enum! {
    enum ApiState {
        Idle,
        Requesting,
        Success,
        Error,
    }
    final: [Success, Error]
    error: [Error]
}

let machine = StateMachineBuilder::new()
    .initial(ApiState::Idle)
    .add_transition(
        TransitionBuilder::new()
            .from(ApiState::Idle)
            .to(ApiState::Requesting)
            .enforce(
                EnforcementBuilder::new()
                    .max_attempts(3)
                    .timeout(Duration::from_secs(30))
                    .build()
            )
            .succeeds()
            .build()?
    )
    .build()?;
```

### Pattern 4: Bulk Transition Creation

Create multiple similar transitions efficiently.

```rust
use mindset::builder::simple_transition;

state_enum! {
    enum ProcessState {
        Step1,
        Step2,
        Step3,
        Step4,
        Done,
    }
    final: [Done]
}

let transitions = vec![
    simple_transition(ProcessState::Step1, ProcessState::Step2),
    simple_transition(ProcessState::Step2, ProcessState::Step3),
    simple_transition(ProcessState::Step3, ProcessState::Step4),
    simple_transition(ProcessState::Step4, ProcessState::Done),
];

let machine = StateMachineBuilder::new()
    .initial(ProcessState::Step1)
    .transitions(transitions)
    .build()?;
```

### Pattern 5: Error Recovery Paths

Create multiple paths including error recovery.

```rust
state_enum! {
    enum DeployState {
        Ready,
        Deploying,
        Verifying,
        Complete,
        Failed,
        RollingBack,
        RolledBack,
    }
    final: [Complete, RolledBack]
    error: [Failed]
}

let machine = StateMachineBuilder::new()
    .initial(DeployState::Ready)
    // Happy path
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::Ready)
            .to(DeployState::Deploying)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::Deploying)
            .to(DeployState::Verifying)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::Verifying)
            .to(DeployState::Complete)
            .succeeds()
            .build()?
    )
    // Error path
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::Deploying)
            .to(DeployState::Failed)
            .when(|state| state.is_error())
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::Failed)
            .to(DeployState::RollingBack)
            .succeeds()
            .build()?
    )
    .add_transition(
        TransitionBuilder::new()
            .from(DeployState::RollingBack)
            .to(DeployState::RolledBack)
            .succeeds()
            .build()?
    )
    .build()?;
```

## Macro Reference

### `state_enum!`

Generates a State trait implementation for simple enums, reducing boilerplate.

#### Syntax

```rust
state_enum! {
    $(#[$meta:meta])*              // Optional attributes (e.g., #[derive(...)])
    $vis:vis enum $name:ident {    // Visibility and enum name
        $(
            $(#[$variant_meta:meta])*  // Optional variant attributes
            $variant:ident
        ),*
    }

    final: [$($final:ident),*]     // Optional: List of final states
    error: [$($error:ident),*]     // Optional: List of error states
}
```

#### Features

1. **Automatic State Implementation**: Generates `State` trait implementation
2. **Serialization Support**: Automatically derives `Serialize` and `Deserialize`
3. **Debug Support**: Derives `Clone`, `PartialEq`, and `Debug`
4. **Final States**: Mark terminal states with `final: [...]`
5. **Error States**: Mark error states with `error: [...]`

#### Generated Methods

The macro generates implementations for:

- `fn name(&self) -> &str`: Returns the variant name as a string
- `fn is_final(&self) -> bool`: Returns true if state is in the `final` list
- `fn is_error(&self) -> bool`: Returns true if state is in the `error` list

#### Examples

##### Basic Usage

```rust
use mindset::state_enum;

state_enum! {
    enum SimpleState {
        Start,
        End,
    }
}
```

##### With Final States

```rust
state_enum! {
    enum WorkflowState {
        Initial,
        Processing,
        Done,
        Failed,
    }
    final: [Done, Failed]
}
```

##### With Error States

```rust
state_enum! {
    enum TaskState {
        Ready,
        Running,
        Success,
        Error,
        Timeout,
    }
    final: [Success, Error, Timeout]
    error: [Error, Timeout]
}
```

##### Public Enum

```rust
state_enum! {
    pub enum PublicState {
        Open,
        Closed,
    }
    final: [Closed]
}
```

##### With Documentation

```rust
state_enum! {
    /// Represents the state of a database connection
    pub enum ConnectionState {
        /// Not yet connected
        Disconnected,
        /// Currently establishing connection
        Connecting,
        /// Successfully connected
        Connected,
        /// Connection failed
        Failed,
    }
    final: [Connected, Failed]
    error: [Failed]
}
```

## Advanced Usage

### Custom Actions with Effects

For complex transitions that need to perform I/O or side effects:

```rust
use stillwater::prelude::*;
use mindset::effects::{TransitionResult, TransitionError};

trait Logger {
    fn log(&mut self, msg: &str);
}

let transition = TransitionBuilder::new()
    .from(State::A)
    .to(State::B)
    .action(|| {
        // Create an effect that logs and transitions
        effect(|env: &mut dyn Logger| {
            env.log("Transitioning from A to B");
            TransitionResult::Success(State::B)
        }).boxed()
    })
    .build()?;
```

### Combining Guards and Enforcement

Use both guards (pre-conditions) and enforcement (runtime limits):

```rust
let transition = TransitionBuilder::new()
    .from(State::Idle)
    .to(State::Processing)
    .when(|state| state.is_ready())  // Pre-condition check
    .enforce(
        EnforcementBuilder::new()
            .max_attempts(3)
            .timeout(Duration::from_secs(30))
            .require_pred(
                |ctx| !ctx.from.is_locked(),
                "State is locked".to_string()
            )
            .build()
    )
    .succeeds()
    .build()?;
```

### Helper Functions

Mindset provides helper functions for common transition patterns:

#### `simple_transition`

Creates an unconditional transition with no guard.

```rust
use mindset::builder::simple_transition;

let transition = simple_transition::<MyState, ()>(
    MyState::Start,
    MyState::End
);
```

#### `guarded_transition`

Creates a transition with a guard predicate.

```rust
use mindset::builder::guarded_transition;

let transition = guarded_transition::<MyState, (), _>(
    MyState::Start,
    MyState::Middle,
    |state| !state.is_final()
);
```

## Error Handling

### BuildError Types

All builders return `Result<T, BuildError>` from their `.build()` methods.

#### StateMachineBuilder Errors

- `BuildError::MissingInitialState`: Initial state not set
- `BuildError::NoTransitions`: No transitions added

```rust
let result = StateMachineBuilder::<MyState, ()>::new().build();

match result {
    Ok(machine) => { /* use machine */ },
    Err(BuildError::MissingInitialState) => {
        eprintln!("Must call .initial(state) before .build()");
    },
    Err(BuildError::NoTransitions) => {
        eprintln!("Must add at least one transition");
    },
    _ => {}
}
```

#### TransitionBuilder Errors

- `BuildError::MissingFromState`: Source state not set
- `BuildError::MissingToState`: Target state not set
- `BuildError::MissingAction`: Action not set

```rust
let result = TransitionBuilder::<MyState, ()>::new()
    .from(MyState::A)
    .build();

match result {
    Ok(transition) => { /* use transition */ },
    Err(BuildError::MissingToState) => {
        eprintln!("Must call .to(state) before .build()");
    },
    Err(BuildError::MissingAction) => {
        eprintln!("Must call .action() or .succeeds() before .build()");
    },
    _ => {}
}
```

### Error Recovery

Use the `?` operator for clean error propagation:

```rust
fn build_machine() -> Result<StateMachine<MyState, ()>, BuildError> {
    Ok(StateMachineBuilder::new()
        .initial(MyState::Start)
        .transition(
            TransitionBuilder::new()
                .from(MyState::Start)
                .to(MyState::End)
                .succeeds()
        )?  // Propagates TransitionBuilder errors
        .build()?)  // Propagates StateMachineBuilder errors
}
```

## Best Practices

1. **Use the `state_enum!` macro**: Reduces boilerplate and ensures consistency
2. **Prefer `.succeeds()` over custom actions**: Use the simpler API unless you need effects
3. **Chain builders with `?`**: Use the `?` operator for clean error handling
4. **Mark final states**: Always specify final states for proper termination
5. **Mark error states**: Distinguish error states from successful terminal states
6. **Use guards for pre-conditions**: Validate state before transitions
7. **Use enforcement for runtime limits**: Prevent infinite loops and timeouts
8. **Collect transitions in vectors**: For bulk operations and cleaner code
9. **Document complex guards**: Add comments explaining guard logic
10. **Test each transition**: Verify guards and actions work as expected

## Next Steps

- Review the [Effects Guide]effects-guide.md for effectful transitions
- See the [Enforcement Guide]enforcement.md for validation and policies
- Check the [API Documentation]https://docs.rs/mindset for complete reference
- Explore the [Checkpointing Guide]checkpointing.md for long-running workflows