Repository navigation
Typestate Protocol
Ferrous.States pairs with Ferrous.Ownership to enforce linear state machines at compile time using [Typestate] (IsInitial, IsTerminal) and [ConsumesSelf].
In vanilla .NET, stateful workflows (HttpRequestBuilder, DbTransaction, PaymentIntent) usually enforce call ordering with a runtime enum State field and InvalidOperationException—meaning out-of-order calls, double transitions, or forgotten finalization all compile cleanly and only fail at runtime:
// Vanilla C#: illegal call sequences compile and fail at runtime
public sealed class PaymentIntent
{
private string _state = "Created";
public void Authorize() { if (_state != "Created") throw new InvalidOperationException(); _state = "Authorized"; }
public void Capture() { if (_state != "Authorized") throw new InvalidOperationException(); _state = "Captured"; }
}
var intent = new PaymentIntent();
intent.Capture(); // Compiles, throws InvalidOperationException at runtime
intent.Authorize();
intent.Authorize(); // Compiles, double-authorization bugWith Ferrous.States + Ferrous.Ownership, each state is its own type, each transition consumes the previous state ([ConsumesSelf]), and valid end states are marked [Typestate(IsTerminal = true)]:
using Ferrous;
[Typestate(IsInitial = true)]
public sealed class DraftPayment
{
[ConsumesSelf]
public AuthorizedPayment Authorize() => new AuthorizedPayment();
}
[Typestate]
public sealed class AuthorizedPayment
{
[ConsumesSelf]
public CapturedPayment Capture() => new CapturedPayment();
[ConsumesSelf]
public VoidedPayment Void() => new VoidedPayment();
}
[Typestate(IsTerminal = true)]
public sealed class CapturedPayment { }
[Typestate(IsTerminal = true)]
public sealed class VoidedPayment { }Now every protocol bug becomes a compile-time error:
-
Skipping states (
new AuthorizedPayment()): blocked byFE0300. -
Double-capturing or using stale state (
auth.Capture(); auth.Void();): blocked byFE0200(use of moved valueauth). -
Forgetting to finish an authorized payment (falling out of scope on a branch without calling
Capture()orVoid()): caught byFE0303(leaked non-terminal typestate)! -
Visualizing & auditing the state machine:
dotnet fe graph src/renders a MermaidstateDiagram-v2with[*] --> DraftPayment,CapturedPayment --> [*], and flags unreachable or non-terminal dead-end states.
Running dotnet fe graph src/ on the payment state machine above emits (and GitHub/static docs render):
stateDiagram-v2
[*] --> DraftPayment
DraftPayment --> AuthorizedPayment : Authorize()
AuthorizedPayment --> CapturedPayment : Capture()
AuthorizedPayment --> VoidedPayment : Void()
CapturedPayment --> [*]
VoidedPayment --> [*]
You can also keep a diagram in any Markdown file automatically up to date in CI with dotnet fe graph src/ --write docs/Architecture.md, or get it embedded automatically in your static API docs via dotnet fe doc.
| ID | Severity | Description |
|---|---|---|
FE0300 |
Error | Direct construction of a non-initial [Typestate] outside a [ConsumesSelf] transition. |
FE0301 |
Warning | Instance transition method between [Typestate] types missing [ConsumesSelf] (IDE quick-fix available via Ferrous.CodeFixes). |
FE0302 |
Warning | Resulting typestate from a [ConsumesSelf] transition was discarded. |
FE0303 |
Warning | Non-terminal [Typestate] local falls out of scope without transitioning ([ConsumesSelf]), being consumed ([Consuming]), or being returned. |
Ferrous — Rust quality-of-life primitives, source generators, and analyzers for .NET (.NET 10 / .NET 11, NativeAOT-ready, MIT License).