Documentation
¶
Overview ¶
Package rule provides composable business-rule predicates and a typed guard-driven state machine following the Specification pattern.
Class: primitive UseWhen: Validation logic, guard conditions, eligibility checks. Composable rules with structured failure reporting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AllOfRule ¶
type AllOfRule[T any] struct { // contains filtered or unexported fields }
func AllOf ¶
AllOf returns a Rule that is satisfied only when every inner rule passes. All rules are evaluated regardless of failures; violations are collected from every failing rule.
func (AllOfRule[T]) Evaluate ¶
func (a AllOfRule[T]) Evaluate(candidate T) Violations
Evaluate runs all rules and collects violations from every failing rule.
type AnyOfRule ¶
type AnyOfRule[T any] struct { // contains filtered or unexported fields }
func AnyOf ¶
AnyOf returns a Rule that is satisfied when at least one inner rule passes. Short-circuits on the first success.
func (AnyOfRule[T]) Evaluate ¶
func (a AnyOfRule[T]) Evaluate(candidate T) Violations
Evaluate returns no violations if any rule passes; otherwise collects all.
type HistoryEntry ¶
HistoryEntry records a single state transition with its timestamp.
type Instance ¶
type Instance[T any] struct { // contains filtered or unexported fields }
Instance[T] is a running instance of a Machine[T]. It tracks the current state and transition history. Instance is not safe for concurrent use; it follows a single-owner pattern.
func NewInstance ¶
NewInstance creates an Instance starting at the machine's initial state.
func (*Instance[T]) Advance ¶
Advance evaluates candidate against all available transitions from the current state and applies the first passing one. Returns an error if no transition is available.
func (*Instance[T]) AdvanceTo ¶
AdvanceTo finds the transition from the current state to target, checks its guard, and applies it. Returns an error if the transition is not found or the guard fails.
func (*Instance[T]) History ¶
func (i *Instance[T]) History() []HistoryEntry
History returns a copy of the transition history.
func (*Instance[T]) IsTerminal ¶
IsTerminal reports whether the current state is a terminal state.
type Machine ¶
type Machine[T any] struct { // contains filtered or unexported fields }
Machine[T] is an immutable state machine definition. It stores the initial state and the full set of transitions.
func NewMachine ¶
func NewMachine[T any](initial State, transitions []Transition[T]) *Machine[T]
NewMachine creates a Machine with the given initial state and transitions. The transitions slice is copied so the machine is immutable after construction.
func (*Machine[T]) AvailableTransitions ¶
func (m *Machine[T]) AvailableTransitions(current State, candidate T) []Transition[T]
AvailableTransitions returns all transitions from current whose guard passes for candidate.
func (*Machine[T]) TerminalStates ¶
TerminalStates returns all states that have no outgoing transitions. A state is terminal if it appears in any transition (as From or To) but never as From.
type Negated ¶
type Negated struct{}
Negated is the violation context produced when a Not rule fails.
type NotRule ¶
type NotRule[T any] struct { // contains filtered or unexported fields }
func Not ¶
Not returns a Rule that inverts another rule. It is satisfied when the inner rule fails. Produces a Negated violation when the inner rule passes.
func (NotRule[T]) Evaluate ¶
func (n NotRule[T]) Evaluate(candidate T) Violations
Evaluate produces a violation when the inner rule passes (i.e. Not fails).
type Rule ¶
Rule defines a single business rule for a candidate of type T.
func AlwaysAllow ¶
AlwaysAllow returns a Rule whose Check always returns a passing Verdict.
type State ¶
State represents a named state in a state machine. Equality is based on Name only; Metadata is informational.
type Transition ¶
Transition defines a guarded edge between two states. Guard is evaluated against the subject to determine if the transition is allowed.
type Verdict ¶
Verdict is the return value of a predicate function.
type Violation ¶
Violation records a rule that was not satisfied. Code is derived from the type name of Context.
type Violations ¶
type Violations struct {
Items []Violation
}
Violations holds the outcome of evaluating rules against a candidate.
func (Violations) Codes ¶
func (v Violations) Codes() []string
Codes returns the codes of all recorded violations.
func (Violations) IsValid ¶
func (v Violations) IsValid() bool
IsValid returns true when no violations were recorded.