stillwater 1.0.1

Pragmatic effect composition and validation for Rust - pure core, imperative shell
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# Effect Composition: Pure Core, Imperative Shell

## The Philosophy

Effect helps you structure applications with:
- **Pure core**: Business logic with no side effects (easy to test)
- **Imperative shell**: I/O operations at the boundaries (controlled)

This separation makes code more testable, maintainable, and composable.

## Zero-Cost by Default

Stillwater's Effect system follows the `futures` crate pattern: **zero-cost by default, explicit boxing when needed**.

```rust
use stillwater::prelude::*;

// Zero heap allocations - compiler can inline everything
let effect = pure::<_, String, ()>(42)
    .map(|x| x + 1)           // Returns Map<Pure<...>, ...>
    .and_then(|x| pure(x * 2)) // Returns AndThen<Map<...>, ...>
    .map(|x| x.to_string());   // Returns Map<AndThen<...>, ...>

// Type: Map<AndThen<Map<Pure<i32, String, ()>, ...>, ...>, ...>
// NO heap allocation!
```

Each combinator returns a concrete type. The compiler knows the exact type at compile time and can fully optimize the effect chain.

## The Problem

How do you test this code?

```rust
async fn create_user(email: String, age: u8) -> Result<User, Error> {
    // Validation mixed with I/O
    if !email.contains('@') {
        return Err(Error::InvalidEmail);
    }

    // Database call (requires real/mock DB)
    let existing = database.find_by_email(&email).await?;
    if existing.is_some() {
        return Err(Error::EmailExists);
    }

    // More I/O
    let user = User { email, age };
    database.save(&user).await?;
    Ok(user)
}
```

Problems:
- Can't test without database
- Business logic mixed with I/O
- Hard to reason about what's pure vs effectful

## The Solution: Effect

Effect separates pure logic from I/O:

```rust
use stillwater::prelude::*;

#[derive(Clone)]
struct AppEnv {
    db: Database,
}

fn create_user(email: String, age: u8) -> impl Effect<Output = User, Error = AppError, Env = AppEnv> {
    // Pure validation first
    from_validation(validate_user(&email, age).map_err(AppError::Validation))
        // Then I/O
        .and_then(move |_| {
            from_fn(move |env: &AppEnv| env.db.find_by_email(&email))
        })
        // Pure logic
        .and_then(move |existing| {
            if existing.is_some() {
                fail(AppError::EmailExists)
            } else {
                pure(User { email, age })
            }
        })
        // More I/O
        .and_then(|user| {
            from_fn(move |env: &AppEnv| env.db.save(&user))
                .map(move |_| user)
        })
}

// Run at application boundary
let env = AppEnv { db };
let user = create_user(email, age).run(&env).await?;
```

Benefits:
- Pure functions need no mocks
- I/O is explicit via `from_fn`, `from_async`
- Easy to test with mock environments
- Zero heap allocations in the effect chain

## Core API

### Creating Effects

```rust
use stillwater::prelude::*;

// Pure value (no I/O)
let effect = pure::<_, String, ()>(42);

// Failed effect
let effect = fail::<i32, _, ()>("error".to_string());

// From Result
let effect = from_result::<_, String, ()>(Ok(42));

// From Validation
let validation = Validation::success(42);
let effect = from_validation(validation);

// From sync function
let effect = from_fn(|env: &Env| {
    Ok::<_, String>(env.config.value)
});

// From async function
let effect = from_async(|env: &Env| async {
    env.db.fetch_user(123).await
});

// From Option
let effect = from_option::<_, _, ()>(Some(42), || "value missing");
```

### Transforming Effects

```rust
use stillwater::prelude::*;

// Map success value
let effect = pure::<_, String, ()>(21).map(|x| x * 2);
let result = effect.run(&()).await; // Ok(42)

// Map error value
let effect = fail::<i32, _, ()>("oops").map_err(|e| format!("Error: {}", e));

// Chain dependent effects
let effect = pure::<_, String, ()>(5)
    .and_then(|x| pure(x * 2))
    .and_then(|x| pure(x + 10));
let result = effect.run(&()).await; // Ok(20)
```

### Validation Combinators

Stillwater provides declarative validation combinators that eliminate verbose `and_then` boilerplate when validating effect outputs:

#### Using `ensure()` with Closures

The `ensure()` method validates an effect's success value and fails if the predicate returns false:

```rust
use stillwater::prelude::*;

#[derive(Debug, PartialEq)]
enum Error {
    Negative,
    TooLarge,
}

// Before: verbose and_then pattern
let effect = pure::<_, Error, ()>(5)
    .and_then(|x| {
        if x > 0 {
            pure(x)
        } else {
            fail(Error::Negative)
        }
    });

// After: declarative ensure
let effect = pure::<_, Error, ()>(5)
    .ensure(|x| *x > 0, Error::Negative);

let result = effect.run(&()).await;
assert_eq!(result, Ok(5));
```

#### Using `ensure_with()` for Lazy Errors

When you need the value to construct the error:

```rust
use stillwater::prelude::*;

#[derive(Debug, PartialEq)]
struct RangeError {
    value: i32,
    min: i32,
}

let effect = pure::<_, RangeError, ()>(-5)
    .ensure_with(
        |x| *x >= 0,
        |x| RangeError { value: *x, min: 0 }
    );

let result = effect.run(&()).await;
assert_eq!(result, Err(RangeError { value: -5, min: 0 }));
```

#### Using `ensure_pred()` with Composable Predicates

For reusable validation logic, use predicates from the `predicate` module:

```rust
use stillwater::prelude::*;
use stillwater::predicate::*;

#[derive(Debug, PartialEq)]
enum Error {
    InvalidAge,
}

let valid_age = between(18, 120);

let effect = pure::<_, Error, ()>(25)
    .ensure_pred(valid_age, Error::InvalidAge);

let result = effect.run(&()).await;
assert_eq!(result, Ok(25));

// Fails for invalid ages
let effect = pure::<_, Error, ()>(15)
    .ensure_pred(valid_age, Error::InvalidAge);

let result = effect.run(&()).await;
assert_eq!(result, Err(Error::InvalidAge));
```

#### Using `unless()` for Inverse Validation

The `unless()` method fails when the predicate is TRUE (inverse of `ensure`):

```rust
use stillwater::prelude::*;

#[derive(Debug, PartialEq)]
enum Error {
    UserBanned,
}

struct User {
    id: u32,
    is_banned: bool,
}

let effect = from_fn(|_: &()| User { id: 1, is_banned: false })
    .unless(|u| u.is_banned, Error::UserBanned);

let result = effect.run(&()).await;
assert!(result.is_ok());

// Fails when user is banned
let effect = from_fn(|_: &()| User { id: 2, is_banned: true })
    .unless(|u| u.is_banned, Error::UserBanned);

let result = effect.run(&()).await;
assert_eq!(result, Err(Error::UserBanned));
```

#### Using `filter_or()` Alias

`filter_or()` is an alias for `ensure()` following functional programming conventions:

```rust
use stillwater::prelude::*;

let effect = pure::<_, &str, ()>(5)
    .filter_or(|x| *x > 0, "must be positive");

let result = effect.run(&()).await;
assert_eq!(result, Ok(5));
```

#### Chaining Multiple Validations

Combine multiple validation checks for comprehensive validation:

```rust
use stillwater::prelude::*;
use stillwater::predicate::*;

#[derive(Debug, PartialEq)]
enum Error {
    TooShort,
    TooLong,
    NotAlpha,
}

let effect = pure::<_, Error, ()>(String::from("hello"))
    .ensure_pred(len_min(3), Error::TooShort)
    .ensure_pred(len_max(10), Error::TooLong)
    .ensure_pred(is_alphabetic(), Error::NotAlpha);

let result = effect.run(&()).await;
assert_eq!(result, Ok(String::from("hello")));

// Fails at first violation (fail-fast)
let effect = pure::<_, Error, ()>(String::from("hi"))
    .ensure_pred(len_min(3), Error::TooShort)  // fails here
    .ensure_pred(len_max(10), Error::TooLong)
    .ensure_pred(is_alphabetic(), Error::NotAlpha);

let result = effect.run(&()).await;
assert_eq!(result, Err(Error::TooShort));
```

#### Real-World Example

```rust
use stillwater::prelude::*;

#[derive(Clone)]
struct Database;

impl Database {
    async fn fetch_user(&self, id: u32) -> Result<User, DbError> {
        // ... database logic
    }
}

#[derive(Clone)]
struct AppEnv {
    db: Database,
}

#[derive(Debug)]
enum AppError {
    Db(DbError),
    UserBanned,
    Underage,
}

struct User {
    id: u32,
    age: u8,
    is_banned: bool,
}

fn fetch_valid_user(id: u32) -> impl Effect<Output = User, Error = AppError, Env = AppEnv> {
    from_fn(move |env: &AppEnv| env.db.fetch_user(id))
        .map_err(AppError::Db)
        .unless(|u| u.is_banned, AppError::UserBanned)
        .ensure(|u| u.age >= 18, AppError::Underage)
}

// Usage
let env = AppEnv { db: Database };
let user = fetch_valid_user(123).run(&env).await?;
```

#### Why Use Effect Validation Combinators?

**Before** (12 lines):
```rust
from_fn(|env: &AppEnv| fetch_data(env))
    .and_then(|data| {
        if data.value > 0 {
            pure(data)
        } else {
            fail(Error::InvalidValue)
        }
    })
    .and_then(|data| {
        if data.count < 100 {
            pure(data)
        } else {
            fail(Error::TooMany)
        }
    })
```

**After** (3 lines):
```rust
from_fn(|env: &AppEnv| fetch_data(env))
    .ensure(|data| data.value > 0, Error::InvalidValue)
    .ensure(|data| data.count < 100, Error::TooMany)
```

### Running Effects

```rust
use stillwater::prelude::*;

// With environment
let env = AppEnv { /* ... */ };
let result = effect.run(&env).await;

// With unit environment (when Env = ())
use stillwater::RunStandalone;
let result = effect.run_standalone().await;
```

## When to Use `.boxed()`

Boxing is needed in exactly three situations:

### 1. Storing in Collections

```rust
use stillwater::prelude::*;

// Different effect types can't be stored in the same Vec
// Boxing gives them a uniform type
let effects: Vec<BoxedEffect<i32, String, ()>> = vec![
    pure(1).boxed(),
    pure(2).map(|x| x * 2).boxed(),
    pure(3).and_then(|x| pure(x * 3)).boxed(),
];

// Process them
for effect in effects {
    let result = effect.run(&()).await?;
    println!("Result: {}", result);
}
```

### 2. Recursive Effects

```rust
use stillwater::prelude::*;

// Recursive function needs concrete return type
fn countdown(n: i32) -> BoxedEffect<i32, String, ()> {
    if n <= 0 {
        pure(0).boxed()
    } else {
        pure(n)
            .and_then(move |x| countdown(x - 1).map(move |sum| x + sum))
            .boxed()
    }
}

let sum = countdown(5).run(&()).await?; // 15
```

### 3. Match Arms with Different Effect Types

```rust
use stillwater::prelude::*;

enum DataSource {
    Cache,
    Database,
    Remote,
}

fn fetch_data(source: DataSource) -> BoxedEffect<String, String, ()> {
    match source {
        DataSource::Cache => {
            // Just pure value
            pure("cached data".to_string()).boxed()
        }
        DataSource::Database => {
            // Effect with map
            pure("db")
                .map(|s| format!("{} data", s))
                .boxed()
        }
        DataSource::Remote => {
            // Effect with and_then
            pure("remote")
                .and_then(|s| pure(format!("{} data", s)))
                .boxed()
        }
    }
}
```

## Reader Pattern

The Reader pattern provides functional dependency injection. Stillwater includes three helpers:

### `ask()` - Access the Environment

Returns the entire environment:

```rust
use stillwater::prelude::*;

#[derive(Clone)]
struct Config {
    api_key: String,
    timeout: u64,
}

// Get the whole environment
let effect = ask::<String, Config>();

let config = Config {
    api_key: "secret".into(),
    timeout: 30,
};

let result = effect.run(&config).await.unwrap();
assert_eq!(result.api_key, "secret");
```

### `asks()` - Query Environment

Extract a specific value:

```rust
use stillwater::prelude::*;

#[derive(Clone)]
struct AppEnv {
    database: String,
    cache: String,
}

// Query just the database field
let effect = asks(|env: &AppEnv| env.database.clone());

let env = AppEnv {
    database: "postgres".into(),
    cache: "redis".into(),
};

let result = effect.run(&env).await.unwrap();
assert_eq!(result, "postgres");
```

### `local()` - Modify Environment

Run an effect with a temporarily modified environment:

```rust
use stillwater::prelude::*;

#[derive(Clone)]
struct Config {
    debug: bool,
    timeout: u64,
}

fn fetch_data() -> impl Effect<Output = String, Error = String, Env = Config> {
    asks(|cfg: &Config| format!("fetched with timeout {}", cfg.timeout))
}

let config = Config {
    debug: false,
    timeout: 30,
};

// Run with modified timeout
let effect = local(
    |cfg: &Config| Config { timeout: 60, ..*cfg },
    fetch_data()
);

let result = effect.run(&config).await.unwrap();
assert_eq!(result, "fetched with timeout 60");
// Original config still has timeout=30
```

## Parallel Effects

### Heterogeneous Parallel (Zero-Cost)

For 2-4 effects of different types, use `par2`, `par3`, `par4`:

```rust
use stillwater::prelude::*;

let (num, text) = par2(
    pure::<_, String, ()>(42),
    pure::<_, String, ()>("hello".to_string()),
).run(&()).await?;
```

### Homogeneous Parallel (Requires Boxing)

For collections of effects, use `par_all`, `race`, `par_all_limit`:

```rust
use stillwater::prelude::*;

// par_all - run all, collect all results
let effects: Vec<BoxedEffect<i32, String, ()>> = vec![
    pure(1).boxed(),
    pure(2).boxed(),
    pure(3).boxed(),
];
let results = par_all(effects).run(&()).await?; // [1, 2, 3]

// race - return first success
let effects: Vec<BoxedEffect<String, String, ()>> = vec![
    fail("first failed".to_string()).boxed(),
    pure("second succeeded".to_string()).boxed(),
];
let result = race(effects).run(&()).await?; // "second succeeded"

// par_all_limit - run with concurrency limit
let effects: Vec<BoxedEffect<i32, String, ()>> = /* many effects */;
let results = par_all_limit(effects, 10).run(&()).await?; // max 10 concurrent
```

## Real-World Example: User Registration

```rust
use stillwater::prelude::*;

// Environment with dependencies
#[derive(Clone)]
struct AppEnv {
    db: Database,
    email_service: EmailService,
}

// Error type
#[derive(Debug)]
enum AppError {
    ValidationError(Vec<String>),
    EmailExists,
    DatabaseError(String),
    EmailError(String),
}

// Pure validation (no I/O, easy to test)
fn validate_user(email: &str, age: u8) -> Validation<(), Vec<String>> {
    Validation::all((
        validate_email(email),
        validate_age(age),
    ))
    .map(|_| ())
}

// Effect composition (I/O at boundaries)
fn register_user(
    email: String,
    age: u8,
) -> impl Effect<Output = User, Error = AppError, Env = AppEnv> {
    // 1. Validate input (pure)
    from_validation(
        validate_user(&email, age)
            .map_err(AppError::ValidationError)
    )
    // 2. Check if email exists (I/O)
    .and_then(move |_| {
        from_fn(move |env: &AppEnv| {
            env.db.find_by_email(&email)
                .map_err(|e| AppError::DatabaseError(e.to_string()))
        })
    })
    // 3. Check uniqueness (pure logic)
    .and_then(move |existing| {
        if existing.is_some() {
            fail(AppError::EmailExists)
        } else {
            pure(())
        }
    })
    // 4. Create user (pure)
    .map(move |_| User { email: email.clone(), age })
    // 5. Save to database (I/O)
    .and_then(|user| {
        from_fn(move |env: &AppEnv| {
            env.db.save_user(&user)
                .map_err(|e| AppError::DatabaseError(e.to_string()))
        })
        .map(move |_| user)
    })
    // 6. Send welcome email (I/O)
    .and_then(|user| {
        from_fn(move |env: &AppEnv| {
            env.email_service.send_welcome(&user.email)
                .map_err(|e| AppError::EmailError(e.to_string()))
        })
        .map(move |_| user)
    })
}

// Usage at application boundary
#[tokio::main]
async fn main() -> Result<(), AppError> {
    let env = AppEnv {
        db: Database::connect("postgres://...").await?,
        email_service: EmailService::new(),
    };

    let user = register_user(
        "user@example.com".to_string(),
        25
    ).run(&env).await?;

    println!("Registered: {:?}", user);
    Ok(())
}
```

## Testing Effects

The key benefit: pure functions need no mocks!

```rust
#[cfg(test)]
mod tests {
    use super::*;

    // Test pure validation (no mocks needed!)
    #[test]
    fn test_validate_user() {
        let result = validate_user("user@example.com", 25);
        assert!(result.is_success());

        let result = validate_user("invalid", 15);
        assert!(result.is_failure());
    }

    // Test effectful code with mock environment
    #[derive(Clone)]
    struct MockEnv {
        users: Vec<User>,
    }

    impl MockEnv {
        fn find_by_email(&self, email: &str) -> Result<Option<User>, String> {
            Ok(self.users.iter().find(|u| u.email == email).cloned())
        }
    }

    #[tokio::test]
    async fn test_effect_with_mock_env() {
        let env = MockEnv { users: vec![] };

        let effect = from_fn(|env: &MockEnv| env.find_by_email("test@example.com"))
            .and_then(|existing| {
                if existing.is_some() {
                    fail("Email exists")
                } else {
                    pure(User {
                        email: "test@example.com".to_string(),
                        age: 25,
                    })
                }
            });

        let result = effect.run(&env).await;
        assert!(result.is_ok());
    }
}
```

## Performance Considerations

The Effect trait is zero-cost by default:
- No heap allocations for effect chains
- Compiler can fully inline combinators
- Same performance as hand-written async code

Boxing happens only when you call `.boxed()`:
- Collections of effects
- Recursive effects
- Match arms with different types

For I/O-bound work (API calls, database queries), boxing overhead is negligible compared to actual work.

## Common Patterns

### Pattern 1: Validate Then Execute

```rust
from_validation(validate_input(input))
    .and_then(|valid| execute_with_db(valid))
```

### Pattern 2: Read, Decide, Write

```rust
from_fn(|env: &Env| env.db.fetch(id))
    .and_then(|data| {
        let result = pure_business_logic(data);
        from_fn(move |env: &Env| env.db.save(result))
    })
```

### Pattern 3: Error Context

```rust
create_user(email, age)
    .context("Creating user account")
    .and_then(|user| {
        send_welcome_email(&user)
            .context("Sending welcome email")
    })
```

### Pattern 4: Conditional Effect

```rust
fn conditional_fetch(use_cache: bool) -> BoxedEffect<String, String, AppEnv> {
    if use_cache {
        from_fn(|env: &AppEnv| Ok(env.cache.get("data"))).boxed()
    } else {
        from_async(|env: &AppEnv| async { env.db.fetch().await }).boxed()
    }
}
```

## When to Use Effect

**Use Effect when**:
- Separating I/O from business logic
- Testing effectful code
- Composing async operations
- Dependency injection needed

**Use plain async fn when**:
- Simple CRUD operations
- No complex composition
- Testing not critical
- Maximum simplicity needed

## Summary

- **Effect trait**: Zero-cost effect composition following `futures` pattern
- **Pure core**: Business logic is easy to test (no mocks)
- **Imperative shell**: I/O at boundaries via `from_fn`, `from_async`
- **Environment**: Provides dependency injection
- **Boxing**: Use `.boxed()` only when type erasure is needed
- **Composition**: Via `map`, `and_then`, `or_else`, etc.
- **Reader pattern**: `ask()`, `asks()`, `local()` for environment access

## Next Steps

- Learn about [Error Context]04-error-context.md
- Explore the [Reader Pattern]09-reader-pattern.md in depth
- See the [Migration Guide]../MIGRATION.md if upgrading from 0.10.x
- Check out [testing_patterns example]../../examples/testing_patterns.rs
- Read the [API docs]https://docs.rs/stillwater