Expand description
§typed_use_cases
Formalize use cases at the type level. Zero runtime overhead.
A Rust library that brings UML use cases into your type system, providing compile-time awareness when your code structure changes in ways that affect declared use cases.
⚠️ Experimental: This is a proof-of-concept library. It has not been validated in production environments and should be considered an exploratory tool for representing use cases in type systems.
§Quick Example
use typed_use_cases::{Actor, Entity, UseCase};
// Define actors
#[derive(Actor)]
struct Authenticated { user_id: u64 }
// Define entities
#[derive(Entity)]
struct Cart { owner: Authenticated, items: Vec<u64> }
// Declare a use case
trait AddItemToCart: UseCase<
Authenticated,
Cart,
Input = u64,
Output = Cart,
Dependencies = (),
> {}
// Define your System type (application-specific, not part of this library)
struct System;
// Implement the use case
impl UseCase<Authenticated, Cart> for System {
const NAME: &'static str = "Add item to cart";
const DESCRIPTION: &'static str = "User adds product to their cart";
type Input = u64;
type Output = Cart;
type Dependencies = ();
fn satisfy(
actor: Authenticated,
mut cart: Cart,
product_id: u64,
_deps: (),
) -> Cart {
cart.items.push(product_id);
cart
}
}
impl AddItemToCart for System {}
// Verify at compile time (in tests only)
typed_use_cases::implement_all_use_cases!(System: [AddItemToCart]);§What This Library Does
- Formalizes use cases as named traits in Rust
- Compile-time awareness: The compiler knows which use cases exist
- Breaking changes are visible: If you change a method signature, the compiler breaks at the use case level
- Zero runtime overhead: Uses a zero-sized System type (0 bytes)
- Verification in tests only: Uses
#[cfg(test)]to verify implementations
§What This Library Is NOT
- ❌ NOT a verification tool - Does not prove your program satisfies a use case
- ❌ NOT a testing framework - Does not test that your system fulfills requirements
- ❌ NOT a formal methods tool - Does not prove program properties
- ❌ NOT for runtime enforcement - All verification happens at compile time
§Installation
Using cargo:
cargo add typed_use_casesOr in Cargo.toml:
[dependencies]
typed_use_cases = "0.1"§Core Concepts
Actor- The initiator of a use case (e.g.,Authenticated,Anonymous)Entity- What the use case operates on (e.g.,Cart,Order)DependentEntity- An entity owned by a specific actorUseCase- The central trait representing an actionimplement_all_use_cases!- Macro to verify all use cases are implemented
Re-exports§
pub use traits::Actor;pub use traits::Entity;pub use traits::DependentEntity;pub use traits::UseCase;
Modules§
Macros§
- implement_
all_ use_ cases - Verifies at compile time (within test builds) that the given system type implements all declared use case traits.