Stillwater
"Still waters run pure"
A Rust library for pragmatic effect composition and validation, emphasizing the pure core, imperative shell pattern.
Philosophy
Stillwater embodies a simple idea:
- Still = Pure functions (unchanging, referentially transparent)
- Water = Effects (flowing, performing I/O)
Keep your business logic pure and calm like still water. Let effects flow at the boundaries.
What Problems Does It Solve?
1. "I want ALL validation errors, not just the first one"
use Validation;
// Standard Result: stops at first error ❌
let email = validate_email?; // Stops here
let age = validate_age?; // Never reached if email fails
// Stillwater: accumulates all errors ✓
let user = all?;
// Returns: Err(vec![EmailError, AgeError, NameError])
2. "How do I test code with database calls?"
use Effect;
// Pure business logic (no DB, easy to test)
// Effects at boundaries (mockable)
// Test with mock environment
3. "My errors lose context as they bubble up"
use Effect;
fetch_user
.context
.and_then
.context
.run?;
// Error output:
// Error: UserNotFound(12345)
// -> Loading user profile
// -> Processing user data
4. "I need clean dependency injection without passing parameters everywhere"
use Effect;
// Functions don't need explicit config parameters
# block_on;
Core Features
Validation<T, E>- Accumulate all errors instead of short-circuitingNonEmptyVec<T>- Type-safe non-empty collections with guaranteed head elementEffect<T, E, Env>- Separate pure logic from I/O effects- Parallel effect execution - Run independent effects concurrently with
par_all(),race(), andpar_all_limit() - Traverse and sequence - Transform collections with
traverse()andsequence()for both validations and effects- Validate entire collections with error accumulation
- Process collections with effects using fail-fast semantics
- Reader pattern helpers - Clean dependency injection with
ask(),asks(), andlocal() Semigrouptrait - Associative combination of values- Extended implementations for
HashMap,HashSet,BTreeMap,BTreeSet,Option - Wrapper types:
First,Last,Intersectionfor alternative semantics
- Extended implementations for
Monoidtrait - Identity elements for powerful composition patterns- Testing utilities - Ergonomic test helpers
MockEnvbuilder for composing test environments- Assertion macros:
assert_success!,assert_failure!,assert_validation_errors! TestEffectwrapper for deterministic effect testing- Optional
proptestfeature for property-based testing
- Context chaining - Never lose error context
- Zero-cost abstractions - Compiles to same code as hand-written
- Works with
?operator - Integrates with Rust idioms - No heavy macros - Clear types, obvious behavior
Quick Start
use ;
// 1. Validation with error accumulation
// 2. Effect composition
// 3. Run at application boundary
let env = AppEnv ;
let result = create_user.run?;
Why Stillwater?
Compared to existing solutions:
vs. frunk:
- ✓ Focused on practical use cases, not type-level programming
- ✓ Better documentation and examples
- ✓ Effect composition, not just validation
vs. monadic:
- ✓ No awkward macro syntax (
rdrdo! { ... }) - ✓ Zero-cost (no boxing by default)
- ✓ Idiomatic Rust, not Haskell port
vs. hand-rolling:
- ✓ Validation accumulation built-in
- ✓ Error context handling
- ✓ Testability patterns established
- ✓ Composable, reusable
What makes it "Rust-first":
- ❌ No attempt at full monad abstraction (impossible without HKTs)
- ✓ Works with
?operator viaTrytrait - ✓ Zero-cost via generics and monomorphization
- ✓ Integrates with async/await
- ✓ Borrows checker friendly
- ✓ Clear error messages
Installation
Add to your Cargo.toml:
[]
= "0.7"
# Optional: async support
= { = "0.7", = ["async"] }
# Optional: property-based testing
= { = "0.7", = ["proptest"] }
# Optional: multiple features
= { = "0.7", = ["async", "proptest"] }
Examples
Run any example with cargo run --example <name>:
| Example | Demonstrates |
|---|---|
| form_validation | Validation error accumulation |
| nonempty | NonEmptyVec type for guaranteed non-empty collections |
| user_registration | Effect composition and I/O separation |
| error_context | Error trails for debugging |
| data_pipeline | Real-world ETL pipeline |
| testing_patterns | Testing pure vs effectful code |
| reader_pattern | Reader pattern with ask(), asks(), and local() |
| validation | Validation type and error accumulation patterns |
| effects | Effect type and composition patterns |
| parallel_effects | Parallel execution with par_all, race, and par_all_limit |
| io_patterns | IO module helpers for reading/writing |
| pipeline | Data transformation pipelines |
| traverse | Traverse and sequence for collections of validations and effects |
| monoid | Monoid and Semigroup traits for composition |
| extended_semigroup | Semigroup for HashMap, HashSet, Option, and wrapper types |
See examples/ directory for full code.
Production Readiness
Status: 0.7 - Production Ready for Early Adopters
- ✅ 248 unit tests passing (includes property-based tests)
- ✅ 122 documentation tests passing
- ✅ Zero clippy warnings
- ✅ Comprehensive examples
- ✅ Full async support
- ✅ Testing utilities with MockEnv and assertion macros
- ✅ CI/CD pipeline with security audits
This library is stable and ready for use. The 0.x version indicates the API may evolve based on community feedback.
Documentation
- 📚 User Guide - Comprehensive tutorials
- 📖 API Docs - Full API reference
- 🤔 FAQ - Common questions
- 🏛️ Design - Architecture and decisions
- 💭 Philosophy - Core principles
- 🎯 Patterns - Common patterns and recipes
- 🔄 Comparison - vs other libraries
Migrating from Result
Already using Result everywhere? No problem! Stillwater integrates seamlessly:
// Your existing code works as-is
// Upgrade to accumulation when you need it
// Convert back to Result when needed
let result: = validation.into_result;
Start small, adopt progressively. Use Validation only where you need error accumulation.
Contributing
Contributions welcome! This is a young library with room to grow:
- 🐛 Bug reports and feature requests via issues
- 📖 Documentation improvements
- 🧪 More examples and use cases
- 💡 API feedback and design discussions
Before submitting PRs, please open an issue to discuss the change.
License
MIT © Glen Baker iepathos@gmail.com
"Like a still pond with water flowing through it, stillwater keeps your pure business logic calm and testable while effects flow at the boundaries."