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
# Common Patterns and Recipes

This document collects common patterns and recipes for using Stillwater effectively.

## Validation Patterns

### Pattern 1: Independent Field Validation

When validating multiple independent fields:

```rust
use stillwater::Validation;

fn validate_user_registration(input: UserInput) -> Validation<User, Vec<Error>> {
    Validation::all((
        validate_email(&input.email),
        validate_password(&input.password),
        validate_age(input.age),
        validate_username(&input.username),
    ))
    .map(|(email, password, age, username)| {
        User { email, password, age, username }
    })
}
```

### Pattern 2: Dependent Validation

When one validation depends on another's result:

```rust
use stillwater::Validation;

fn validate_and_check_unique(email: &str) -> Validation<Email, Vec<Error>> {
    validate_email_format(email)
        .and_then(|email| check_email_not_taken(email))
}
```

### Pattern 3: Validating Collections

Validate all items in a collection:

```rust
use stillwater::Validation;

fn validate_all(items: Vec<Item>) -> Validation<Vec<ValidItem>, Vec<Error>> {
    let validations: Vec<_> = items
        .into_iter()
        .map(|item| validate_item(item))
        .collect();

    Validation::all_vec(validations)
}
```

### Pattern 4: Conditional Validation

Validate different fields based on conditions:

```rust
use stillwater::Validation;

fn validate_payment(method: PaymentMethod, data: PaymentData) -> Validation<Payment, Vec<Error>> {
    match method {
        PaymentMethod::CreditCard => {
            Validation::all((
                validate_card_number(&data.card_number),
                validate_cvv(&data.cvv),
                validate_expiry(&data.expiry),
            ))
            .map(|(card, cvv, expiry)| Payment::CreditCard { card, cvv, expiry })
        }
        PaymentMethod::BankTransfer => {
            Validation::all((
                validate_account_number(&data.account),
                validate_routing_number(&data.routing),
            ))
            .map(|(account, routing)| Payment::BankTransfer { account, routing })
        }
    }
}
```

## Effect Patterns

All Effect patterns use the free function style with the prelude import:

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

### Pattern 1: Read, Transform, Write

Classic pattern for processing data:

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

fn process_user_data(id: u64) -> impl Effect<Output = (), Error = Error, Env = Env> {
    from_fn(|env: &Env| env.db.fetch_user(id))
        .map(|user| transform_user_data(user))  // Pure transformation
        .and_then(|transformed| {
            from_fn(|env: &Env| env.db.save_user(&transformed))
        })
}
```

### Pattern 2: Validate Then Execute

Validate input, then perform I/O if valid:

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

fn create_user(input: UserInput) -> impl Effect<Output = User, Error = Error, Env = Env> {
    from_validation(validate_user(input))
        .and_then(|valid| {
            from_fn(|env: &Env| env.db.insert_user(&valid))
        })
}
```

### Pattern 3: Try Cache, Fall Back to DB

Common caching pattern:

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

fn get_user(id: u64) -> impl Effect<Output = User, Error = Error, Env = Env> {
    from_fn(|env: &Env| env.cache.get_user(id))
        .and_then(move |cached| {
            match cached {
                Some(user) => pure(user).boxed(),
                None => {
                    from_fn(move |env: &Env| env.db.fetch_user(id))
                        .and_then(move |user| {
                            from_fn(move |env: &Env| env.cache.set_user(id, user.clone()))
                                .map(|_| user)
                        })
                        .boxed()
                }
            }
        })
}
```

### Pattern 4: Sequential Operations with Context

Add context at each step:

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

fn process_order(id: u64) -> impl Effect<Output = Receipt, Error = Error, Env = Env> {
    fetch_order(id)
        .context("fetching order")
        .and_then(|order| {
            validate_order(&order)
                .context("validating order")
        })
        .and_then(|order| {
            charge_payment(&order)
                .context("processing payment")
        })
        .and_then(|charge| {
            generate_receipt(charge)
                .context("generating receipt")
        })
}
```

### Pattern 5: Parallel Operations (using tokio)

When effects are independent:

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

async fn load_dashboard(user_id: u64, env: &Env) -> Result<Dashboard, Error> {
    let (user, projects, notifications) = tokio::try_join!(
        fetch_user(user_id).execute(env),
        fetch_projects(user_id).execute(env),
        fetch_notifications(user_id).execute(env),
    )?;

    Ok(Dashboard { user, projects, notifications })
}
```

### Pattern 6: Combining Independent Effects with Zip

Use `zip` when you need both results from independent effects:

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

// Basic zip: combine two independent effects into a tuple
fn load_user_with_settings(id: UserId) -> impl Effect<Output = (User, Settings), Error = AppError, Env = AppEnv> {
    fetch_user(id).zip(fetch_settings(id))
}

// zip_with: combine with a function directly (more efficient than zip + map)
fn calculate_total(order_id: OrderId) -> impl Effect<Output = Money, Error = AppError, Env = AppEnv> {
    fetch_price(order_id)
        .zip_with(fetch_quantity(order_id), |price, qty| price * qty)
}

// zip3 through zip8: flat tuple results for multiple effects
fn load_profile(id: UserId) -> impl Effect<Output = Profile, Error = AppError, Env = AppEnv> {
    zip3(
        fetch_user(id),
        fetch_settings(id),
        fetch_preferences(id),
    )
    .map(|(user, settings, prefs)| Profile { user, settings, prefs })
}

// Chained zips create nested tuples
fn chained_example() -> impl Effect<Output = i32, Error = String, Env = ()> {
    pure(1)
        .zip(pure(2))
        .zip(pure(3))
        .map(|((a, b), c)| a + b + c)  // Note the nested tuple
}
```

**Key points:**
- `zip` expresses independence - neither effect depends on the other's output
- Uses fail-fast semantics (first error wins), same as `and_then`
- For error accumulation with independent operations, use `Validation::all()` instead
- `zip3` through `zip8` return flat tuples for cleaner pattern matching

## Testing Patterns

### Pattern 1: Testing Pure Functions

Pure functions need no mocking:

```rust
#[test]
fn test_pure_validation() {
    let result = validate_email("user@example.com");
    assert!(result.is_success());

    let result = validate_email("invalid");
    assert!(result.is_failure());
}
```

### Pattern 2: Testing Effects with Mock Environment

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

struct MockEnv {
    users: HashMap<u64, User>,
}

impl MockEnv {
    fn fetch_user(&self, id: u64) -> Result<User, Error> {
        self.users.get(&id).cloned().ok_or(Error::NotFound)
    }
}

#[tokio::test]
async fn test_user_workflow() {
    let mut env = MockEnv {
        users: HashMap::new(),
    };
    env.users.insert(1, User { name: "Alice".into() });

    let effect = from_fn(|env: &MockEnv| env.fetch_user(1));
    let result = effect.execute(&env).await;

    assert!(result.is_ok());
}
```

### Pattern 3: Testing Error Cases

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

#[tokio::test]
async fn test_user_not_found() {
    let env = MockEnv {
        users: HashMap::new(),  // Empty
    };

    let effect = from_fn(|env: &MockEnv| env.fetch_user(999));
    let result = effect.execute(&env).await;

    assert_eq!(result, Err(Error::NotFound));
}
```

## Error Handling Patterns

### Pattern 1: Domain-Specific Errors

```rust
#[derive(Debug)]
enum UserError {
    NotFound(u64),
    InvalidEmail(String),
    PermissionDenied,
}

impl std::fmt::Display for UserError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UserError::NotFound(id) => write!(f, "User {} not found", id),
            UserError::InvalidEmail(email) => write!(f, "Invalid email: {}", email),
            UserError::PermissionDenied => write!(f, "Permission denied"),
        }
    }
}

impl std::error::Error for UserError {}
```

### Pattern 2: Error Conversion

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

fn fetch_user(id: u64) -> impl Effect<Output = User, Error = AppError, Env = Env> {
    from_fn(|env: &Env| {
        env.db.fetch_user(id)
            .map_err(|e| AppError::Database(e.to_string()))
    })
}
```

### Pattern 3: Error Context Trails

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

fn complex_operation() -> impl Effect<Output = Result, Error = ContextError<Error>, Env = Env> {
    step1()
        .context("performing step 1")
        .and_then(|r1| {
            step2(r1).context("performing step 2")
        })
        .and_then(|r2| {
            step3(r2).context("performing step 3")
        })
        .context("complex operation")
}
```

## Composition Patterns

### Pattern 1: Building Complex Validations

```rust
fn validate_address(addr: &Address) -> Validation<ValidAddress, Vec<Error>> {
    Validation::all((
        validate_street(&addr.street),
        validate_city(&addr.city),
        validate_zip(&addr.zip),
        validate_country(&addr.country),
    ))
    .map(|(street, city, zip, country)| {
        ValidAddress { street, city, zip, country }
    })
}

fn validate_contact(contact: &Contact) -> Validation<ValidContact, Vec<Error>> {
    Validation::all((
        validate_email(&contact.email),
        validate_phone(&contact.phone),
        validate_address(&contact.address),
    ))
    .map(|(email, phone, address)| {
        ValidContact { email, phone, address }
    })
}
```

### Pattern 2: Effect Pipelines

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

fn user_registration_pipeline(input: UserInput) -> impl Effect<Output = User, Error = Error, Env = Env> {
    validate_input(input)
        .and_then(|valid| check_uniqueness(valid))
        .and_then(|valid| create_user(valid))
        .and_then(|user| send_welcome_email(user))
        .and_then(|user| create_default_settings(user))
        .context("user registration")
}
```

## Resource Management Patterns

The bracket pattern ensures resources are properly released even when errors occur.

### Pattern 1: Single Resource with Guaranteed Cleanup

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

fn with_database_connection<T>(
    f: impl FnOnce(&Connection) -> impl Effect<Output = T, Error = AppError, Env = AppEnv>
) -> impl Effect<Output = T, Error = AppError, Env = AppEnv> {
    bracket(
        from_fn(|env: &AppEnv| env.pool.get_connection()),  // Acquire
        |conn| async move { conn.release().await },          // Release (always runs)
        f,                                                   // Use
    )
}

// Usage
let result = with_database_connection(|conn| {
    from_fn(move |_| conn.query("SELECT * FROM users"))
}).execute(&env).await;
```

### Pattern 2: Multiple Resources with LIFO Cleanup

Resources are released in reverse order of acquisition (Last In, First Out):

```rust
use stillwater::effect::bracket::bracket2;

fn with_db_and_file(
    path: &Path,
) -> impl Effect<Output = Data, Error = AppError, Env = AppEnv> {
    bracket2(
        open_database(),                                    // Acquired first
        open_file(path),                                    // Acquired second
        |db| async move { db.close().await },               // Released second
        |file| async move { file.close().await },           // Released first (LIFO)
        |db, file| process_data(db, file),
    )
}
```

### Pattern 3: Fluent Builder for Multiple Resources

The `acquiring` builder provides ergonomic multi-resource management:

```rust
use stillwater::effect::bracket::acquiring;

fn complex_operation() -> impl Effect<Output = Result, Error = AppError, Env = AppEnv> {
    acquiring(
        open_connection(),
        |conn| async move { conn.close().await },
    )
    .and(acquire_lock(), |lock| async move { lock.release().await })
    .and(open_file(), |file| async move { file.close().await })
    .with_flat3(|conn, lock, file| {
        // All three resources available here
        // Cleanup happens in reverse order: file, lock, conn
        do_work(conn, lock, file)
    })
}
```

### Pattern 4: Explicit Error Handling with BracketError

When you need to distinguish between use errors and cleanup errors:

```rust
use stillwater::effect::bracket::{bracket_full, BracketError};

fn with_explicit_errors() -> impl Effect<Output = Data, Error = BracketError<AppError>, Env = AppEnv> {
    bracket_full(
        acquire_resource(),
        |r| async move { r.cleanup().await },
        |r| use_resource(r),
    )
}

// Handle all error cases explicitly
let result = with_explicit_errors().execute(&env).await;
match result {
    Ok(data) => println!("Success: {:?}", data),
    Err(BracketError::AcquireError(e)) => {
        // Resource was never acquired, no cleanup needed
        log::error!("Failed to acquire: {:?}", e);
    }
    Err(BracketError::UseError(e)) => {
        // Use failed, but cleanup succeeded
        log::error!("Operation failed: {:?}", e);
    }
    Err(BracketError::CleanupError(e)) => {
        // Use succeeded, but cleanup failed - may need manual intervention
        log::warn!("Cleanup failed: {:?}", e);
    }
    Err(BracketError::Both { use_error, cleanup_error }) => {
        // Both failed - log both for debugging
        log::error!("Use failed: {:?}, cleanup also failed: {:?}", use_error, cleanup_error);
    }
}
```

### Pattern 5: Partial Acquisition Rollback

When acquiring multiple resources, earlier acquisitions are rolled back if later ones fail:

```rust
use stillwater::effect::bracket::acquiring;

// If file acquisition fails, connection is automatically released
let effect = acquiring(
    open_connection(),  // Succeeds
    |c| async move { c.close().await },
)
.and(
    open_file(path),    // Fails!
    |f| async move { f.close().await },
)
.with(|(conn, file)| use_both(conn, file));

// Connection is properly cleaned up even though file failed
let result = effect.execute(&env).await;  // Returns file acquisition error
```

### Pattern 6: Connection Pool Pattern

Encapsulate resource management in reusable abstractions:

```rust
use stillwater::effect::bracket::Resource;

struct ConnectionPool {
    // ... pool internals
}

impl ConnectionPool {
    fn connection(&self) -> Resource<Connection, PoolError, AppEnv> {
        Resource::new(
            from_fn(|env: &AppEnv| env.pool.checkout()),
            |conn| async move { conn.checkin().await },
        )
    }
}

// Usage - cleanup is automatic
let pool = ConnectionPool::new();
let result = pool.connection()
    .with(|conn| from_fn(move |_| conn.query("SELECT 1")))
    .execute(&env)
    .await;
```

## Performance Patterns

### Pattern 1: Avoid Excessive Boxing

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

// Instead of creating many small effects:
let effect = pure::<_, String, ()>(1)
    .map(|x| x + 1)
    .map(|x| x * 2)
    .map(|x| x - 3);

// Combine transformations:
let effect = pure::<_, String, ()>(1)
    .map(|x| (x + 1) * 2 - 3);
```

### Pattern 2: Batch Operations

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

// Instead of many individual queries:
for id in ids {
    fetch_user(id).execute(&env).await?;
}

// Batch fetch:
let users = fetch_users_batch(ids).execute(&env).await?;
```

## Summary

These patterns cover common use cases. Mix and match them based on your needs!

## Next Steps

- See [FAQ]FAQ.md for common questions
- Read [Comparison]COMPARISON.md vs other libraries
- Check [examples/]../examples/ for working code