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
//! Error Context Example
//!
//! Demonstrates ContextError and error trails for better debugging.
//! Shows practical patterns including:
//! - Creating context errors
//! - Adding context breadcrumbs
//! - Using .context() with Effects
//! - Building error trails through call stacks
//! - Formatted error display

use stillwater::{from_fn, ContextError, Effect, EffectContext, EffectExt};

// ==================== Basic Context Errors ====================

/// Example 1: Creating and displaying context errors
///
/// Demonstrates basic ContextError construction and display.
fn example_basic_context() {
    println!("\n=== Example 1: Basic Context Errors ===");

    // Simple error with no context
    let err1: ContextError<&str> = ContextError::new("file not found");
    println!("Basic error:\n{}\n", err1);

    // Error with single context
    let err2 = ContextError::new("file not found").context("reading config file");
    println!("With context:\n{}\n", err2);

    // Error with multiple context layers
    let err3 = ContextError::new("connection refused")
        .context("connecting to database")
        .context("initializing application")
        .context("startup sequence");
    println!("Multiple contexts:\n{}\n", err3);
}

// ==================== Building Error Trails ====================

/// Example 2: Building error trails through function calls
///
/// Demonstrates how context accumulates as errors bubble up.
fn example_error_trails() {
    println!("\n=== Example 2: Error Trails ===");

    // Simulated function call stack
    fn read_file(path: &str) -> Result<String, ContextError<String>> {
        Err(ContextError::new(format!("No such file: {}", path)))
    }

    fn load_config() -> Result<String, ContextError<String>> {
        read_file("/etc/app/config.toml")
            .map_err(|e| e.context("loading configuration file".to_string()))
    }

    fn initialize_database() -> Result<(), ContextError<String>> {
        load_config().map_err(|e| e.context("reading database connection string".to_string()))?;
        Ok(())
    }

    fn start_application() -> Result<(), ContextError<String>> {
        initialize_database()
            .map_err(|e| e.context("initializing database connection".to_string()))?;
        Ok(())
    }

    // Run and see the full error trail
    match start_application() {
        Ok(_) => println!("Application started successfully"),
        Err(e) => println!("Application startup failed:\n{}", e),
    }
}

// ==================== Context with Effects ====================

/// Example 3: Using context() with Effect
///
/// Demonstrates how to add context to Effect errors.
async fn example_effect_context() {
    println!("\n=== Example 3: Context with Effects ===");

    #[derive(Clone)]
    struct Database {
        connected: bool,
    }

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

    impl AsRef<Database> for Env {
        fn as_ref(&self) -> &Database {
            &self.db
        }
    }

    // Effect that fails
    fn connect_to_db() -> impl Effect<Output = (), Error = String, Env = Env> {
        from_fn(|env: &Env| {
            if env.db.connected {
                Ok(())
            } else {
                Err("connection refused".to_string())
            }
        })
    }

    // Add context to the effect
    let effect = connect_to_db().context("connecting to primary database");

    let env = Env {
        db: Database { connected: false },
    };

    match effect.run(&env).await {
        Ok(_) => println!("Connected successfully"),
        Err(e) => println!("Connection failed:\n{}", e),
    }
}

// ==================== Layered Context in Workflows ====================

/// Example 4: Building context through effect composition
///
/// Demonstrates context accumulation in complex effect workflows.
async fn example_layered_context() {
    println!("\n=== Example 4: Layered Context in Workflows ===");

    #[derive(Clone)]
    struct User {
        id: u64,
        name: String,
    }

    #[derive(Clone)]
    struct Database {
        users: Vec<User>,
    }

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

    impl AsRef<Database> for Env {
        fn as_ref(&self) -> &Database {
            &self.db
        }
    }

    // Low-level: fetch user from database
    fn fetch_user(
        user_id: u64,
    ) -> impl Effect<Output = User, Error = ContextError<String>, Env = Env> {
        from_fn(move |env: &Env| {
            env.db
                .users
                .iter()
                .find(|u| u.id == user_id)
                .cloned()
                .ok_or_else(|| format!("user id {} not found", user_id))
        })
        .context(format!("fetching user {}", user_id))
    }

    // Mid-level: load user profile
    fn load_user_profile(
        user_id: u64,
    ) -> impl Effect<Output = User, Error = ContextError<String>, Env = Env> {
        fetch_user(user_id)
            .map_err(move |e| e.context(format!("loading profile for user {}", user_id)))
    }

    // High-level: display user dashboard
    fn display_dashboard(
        user_id: u64,
    ) -> impl Effect<Output = String, Error = ContextError<String>, Env = Env> {
        load_user_profile(user_id)
            .map(|user| format!("Dashboard for {}", user.name))
            .map_err(move |e| e.context(format!("rendering dashboard for user {}", user_id)))
    }

    let env = Env {
        db: Database { users: vec![] }, // Empty database - will fail
    };

    match display_dashboard(42).run(&env).await {
        Ok(dashboard) => println!("{}", dashboard),
        Err(e) => println!("Failed to display dashboard:\n{}", e),
    }
}

// ==================== Practical Error Handling ====================

/// Example 5: Real-world error handling with context
///
/// Demonstrates using context for a realistic file processing scenario.
async fn example_realistic_error_handling() {
    println!("\n=== Example 5: Realistic Error Handling ===");

    #[derive(Debug, Clone)]
    struct Config {
        timeout: u32,
    }

    #[derive(Clone)]
    struct FileSystem {
        files: Vec<String>,
    }

    #[derive(Clone)]
    struct Env {
        fs: FileSystem,
    }

    impl AsRef<FileSystem> for Env {
        fn as_ref(&self) -> &FileSystem {
            &self.fs
        }
    }

    // Read file
    fn read_file(
        path: String,
    ) -> impl Effect<Output = String, Error = ContextError<String>, Env = Env> {
        let path_for_context = path.clone();
        from_fn(move |env: &Env| {
            env.fs
                .files
                .iter()
                .find(|f| f.starts_with(&path))
                .cloned()
                .ok_or_else(|| format!("file not found: {}", path))
        })
        .context(format!("reading file '{}'", path_for_context))
    }

    // Parse config
    fn parse_config(
        content: String,
    ) -> impl Effect<Output = Config, Error = ContextError<String>, Env = Env> {
        from_fn(move |_env: &Env| {
            if content.contains("timeout") {
                Ok(Config { timeout: 30 })
            } else {
                Err("missing required field 'timeout'".to_string())
            }
        })
        .context("parsing configuration".to_string())
    }

    // Load and parse config
    fn load_config(
        path: String,
    ) -> impl Effect<Output = Config, Error = ContextError<String>, Env = Env> {
        read_file(path.clone())
            .and_then(parse_config)
            .map_err(move |e| e.context(format!("loading config from '{}'", path)))
    }

    let env1 = Env {
        fs: FileSystem {
            files: vec!["config.toml: timeout=30".to_string()],
        },
    };

    // Success case
    match load_config("config.toml".to_string()).run(&env1).await {
        Ok(config) => println!("Config loaded: timeout={}", config.timeout),
        Err(e) => println!("Failed:\n{}", e),
    }

    // Failure case: file not found
    let env2 = Env {
        fs: FileSystem { files: vec![] },
    };

    match load_config("missing.toml".to_string()).run(&env2).await {
        Ok(_) => println!("Config loaded"),
        Err(e) => println!("\nFailed:\n{}", e),
    }

    // Failure case: parse error
    let env3 = Env {
        fs: FileSystem {
            files: vec!["bad.toml: invalid content".to_string()],
        },
    };

    match load_config("bad.toml".to_string()).run(&env3).await {
        Ok(_) => println!("Config loaded"),
        Err(e) => println!("\nFailed:\n{}", e),
    }
}

// ==================== Custom Error Types ====================

/// Example 6: Using ContextError with custom error types
///
/// Demonstrates wrapping domain-specific errors.
async fn example_custom_errors() {
    println!("\n=== Example 6: Custom Error Types ===");

    #[derive(Debug, Clone)]
    enum AppError {
        NotFound,
        PermissionDenied,
        InvalidInput(String),
    }

    impl std::fmt::Display for AppError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                AppError::NotFound => write!(f, "resource not found"),
                AppError::PermissionDenied => write!(f, "permission denied"),
                AppError::InvalidInput(msg) => write!(f, "invalid input: {}", msg),
            }
        }
    }

    #[derive(Clone)]
    struct Env;

    fn validate_user_input(
        input: String,
    ) -> impl Effect<Output = String, Error = AppError, Env = Env> {
        from_fn(move |_env: &Env| {
            if input.is_empty() {
                Err(AppError::InvalidInput("input cannot be empty".to_string()))
            } else if input.len() > 100 {
                Err(AppError::InvalidInput(
                    "input exceeds maximum length".to_string(),
                ))
            } else {
                Ok(input.clone())
            }
        })
    }

    fn check_permissions(user_id: u64) -> impl Effect<Output = (), Error = AppError, Env = Env> {
        from_fn(move |_env: &Env| {
            if user_id == 0 {
                Err(AppError::PermissionDenied)
            } else {
                Ok(())
            }
        })
    }

    fn find_resource(id: u64) -> impl Effect<Output = String, Error = AppError, Env = Env> {
        from_fn(move |_env: &Env| {
            if id == 99 {
                Ok("resource data".to_string())
            } else {
                Err(AppError::NotFound)
            }
        })
    }

    let env = Env;

    // Valid input
    match validate_user_input("hello".to_string()).run(&env).await {
        Ok(s) => println!("Valid input: {}", s),
        Err(e) => println!("Error:\n{}", e),
    }

    // Invalid input
    println!();
    match validate_user_input("".to_string()).run(&env).await {
        Ok(s) => println!("Valid input: {}", s),
        Err(e) => println!("Error:\n{}", e),
    }

    // Permission denied
    println!();
    match check_permissions(0).run(&env).await {
        Ok(_) => println!("Permission granted"),
        Err(e) => println!("Error:\n{}", e),
    }

    // Not found
    println!();
    match find_resource(42).run(&env).await {
        Ok(data) => println!("Found: {}", data),
        Err(e) => println!("Error:\n{}", e),
    }
}

// ==================== Main ====================

#[tokio::main]
async fn main() {
    println!("Error Context Examples");
    println!("======================");

    example_basic_context();
    example_error_trails();
    example_effect_context().await;
    example_layered_context().await;
    example_realistic_error_handling().await;
    example_custom_errors().await;

    println!("\n=== All examples completed successfully! ===");
}