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
//! Form Validation Example
//!
//! Demonstrates practical form validation with error accumulation.
//! Shows how Validation excels at collecting all validation errors
//! instead of failing on the first one.
//!
//! Includes:
//! - Contact form validation
//! - User profile validation
//! - Payment form validation
//! - Multi-field cross-validation

use stillwater::Validation;

// ==================== Contact Form ====================

/// Example 1: Simple contact form validation
///
/// Demonstrates validating a contact form and collecting all errors.
fn example_contact_form() {
    println!("\n=== Example 1: Contact Form ===");

    #[derive(Debug)]
    struct ContactForm {
        name: String,
        email: String,
        message: String,
    }

    fn validate_name(name: &str) -> Validation<(), Vec<String>> {
        let mut errors = Vec::new();

        if name.is_empty() {
            errors.push("Name is required".to_string());
        } else if name.len() < 2 {
            errors.push("Name must be at least 2 characters".to_string());
        }

        if errors.is_empty() {
            Validation::success(())
        } else {
            Validation::failure(errors)
        }
    }

    fn validate_email(email: &str) -> Validation<(), Vec<String>> {
        let mut errors = Vec::new();

        if email.is_empty() {
            errors.push("Email is required".to_string());
        } else if !email.contains('@') {
            errors.push("Email must contain @".to_string());
        } else if !email.contains('.') {
            errors.push("Email must contain domain".to_string());
        }

        if errors.is_empty() {
            Validation::success(())
        } else {
            Validation::failure(errors)
        }
    }

    fn validate_message(message: &str) -> Validation<(), Vec<String>> {
        let mut errors = Vec::new();

        if message.is_empty() {
            errors.push("Message is required".to_string());
        } else if message.len() < 10 {
            errors.push("Message must be at least 10 characters".to_string());
        } else if message.len() > 1000 {
            errors.push("Message must not exceed 1000 characters".to_string());
        }

        if errors.is_empty() {
            Validation::success(())
        } else {
            Validation::failure(errors)
        }
    }

    fn validate_contact_form(form: ContactForm) -> Validation<ContactForm, Vec<String>> {
        let v1 = validate_name(&form.name);
        let v2 = validate_email(&form.email);
        let v3 = validate_message(&form.message);
        Validation::<((), (), ()), Vec<String>>::all((v1, v2, v3)).map(|_| form)
    }

    // Valid form
    let valid_form = ContactForm {
        name: "Alice Smith".to_string(),
        email: "alice@example.com".to_string(),
        message: "Hello, I would like more information about your services.".to_string(),
    };

    match validate_contact_form(valid_form) {
        Validation::Success(_) => println!("✓ Valid contact form"),
        Validation::Failure(errors) => {
            println!("✗ Invalid contact form:");
            for error in errors {
                println!("  - {}", error);
            }
        }
    }

    // Invalid form - collects all errors
    let invalid_form = ContactForm {
        name: "A".to_string(),
        email: "invalid-email".to_string(),
        message: "Hi".to_string(),
    };

    println!();
    match validate_contact_form(invalid_form) {
        Validation::Success(_) => println!("✓ Valid contact form"),
        Validation::Failure(errors) => {
            println!("✗ Invalid contact form ({} errors):", errors.len());
            for error in errors {
                println!("  - {}", error);
            }
        }
    }
}

// ==================== User Profile ====================

/// Example 2: User profile validation
///
/// Demonstrates validating a user profile with multiple fields.
fn example_user_profile() {
    println!("\n=== Example 2: User Profile ===");

    #[derive(Debug)]
    struct UserProfile {
        username: String,
        age: u32,
        bio: String,
        website: String,
    }

    fn validate_username(username: &str) -> Validation<(), Vec<String>> {
        if username.len() >= 3
            && username.len() <= 20
            && username.chars().all(|c| c.is_alphanumeric() || c == '_')
        {
            Validation::success(())
        } else {
            Validation::failure(vec![
                "Username must be 3-20 characters and contain only letters, numbers, and underscores".to_string()
            ])
        }
    }

    fn validate_age(age: u32) -> Validation<(), Vec<String>> {
        if (13..=120).contains(&age) {
            Validation::success(())
        } else {
            Validation::failure(vec![format!(
                "Age must be between 13 and 120 (got {})",
                age
            )])
        }
    }

    fn validate_bio(bio: &str) -> Validation<(), Vec<String>> {
        if bio.len() <= 500 {
            Validation::success(())
        } else {
            Validation::failure(vec![format!(
                "Bio must not exceed 500 characters (got {})",
                bio.len()
            )])
        }
    }

    fn validate_website(website: &str) -> Validation<(), Vec<String>> {
        if website.is_empty() || website.starts_with("http://") || website.starts_with("https://") {
            Validation::success(())
        } else {
            Validation::failure(vec![
                "Website must start with http:// or https://".to_string()
            ])
        }
    }

    fn validate_user_profile(profile: UserProfile) -> Validation<UserProfile, Vec<String>> {
        let v1 = validate_username(&profile.username);
        let v2 = validate_age(profile.age);
        let v3 = validate_bio(&profile.bio);
        let v4 = validate_website(&profile.website);
        Validation::<((), (), (), ()), Vec<String>>::all((v1, v2, v3, v4)).map(|_| profile)
    }

    // Valid profile
    let valid = UserProfile {
        username: "alice_123".to_string(),
        age: 25,
        bio: "Software engineer interested in functional programming.".to_string(),
        website: "https://alice.dev".to_string(),
    };

    match validate_user_profile(valid) {
        Validation::Success(profile) => println!("✓ Valid profile: {}", profile.username),
        Validation::Failure(errors) => {
            println!("✗ Invalid profile:");
            for error in errors {
                println!("  - {}", error);
            }
        }
    }

    // Invalid profile - multiple errors
    let invalid = UserProfile {
        username: "ab".to_string(),
        age: 10,
        bio: "x".repeat(600),
        website: "alice.dev".to_string(),
    };

    println!();
    match validate_user_profile(invalid) {
        Validation::Success(_) => println!("✓ Valid profile"),
        Validation::Failure(errors) => {
            println!("✗ Invalid profile ({} errors):", errors.len());
            for error in errors {
                println!("  - {}", error);
            }
        }
    }
}

// ==================== Payment Form ====================

/// Example 3: Payment form validation
///
/// Demonstrates validating payment information with complex rules.
fn example_payment_form() {
    println!("\n=== Example 3: Payment Form ===");

    #[derive(Debug)]
    struct PaymentForm {
        card_number: String,
        expiry_month: u32,
        expiry_year: u32,
        cvv: String,
        amount: f64,
    }

    fn validate_card_number(card: &str) -> Validation<(), Vec<String>> {
        let digits_only: String = card.chars().filter(|c| c.is_numeric()).collect();

        if digits_only.len() == 16 {
            Validation::success(())
        } else {
            Validation::failure(vec![format!(
                "Card number must be 16 digits (got {})",
                digits_only.len()
            )])
        }
    }

    fn validate_expiry(month: u32, year: u32) -> Validation<(), Vec<String>> {
        let mut errors = Vec::new();

        if !(1..=12).contains(&month) {
            errors.push(format!("Invalid month: {}", month));
        }

        if !(2025..=2035).contains(&year) {
            errors.push(format!("Invalid year: {}", year));
        }

        if errors.is_empty() {
            Validation::success(())
        } else {
            Validation::failure(errors)
        }
    }

    fn validate_cvv(cvv: &str) -> Validation<(), Vec<String>> {
        if cvv.len() == 3 && cvv.chars().all(|c| c.is_numeric()) {
            Validation::success(())
        } else {
            Validation::failure(vec!["CVV must be 3 digits".to_string()])
        }
    }

    fn validate_amount(amount: f64) -> Validation<(), Vec<String>> {
        if amount > 0.0 && amount <= 10000.0 {
            Validation::success(())
        } else {
            Validation::failure(vec![format!(
                "Amount must be between $0.01 and $10,000 (got ${:.2})",
                amount
            )])
        }
    }

    fn validate_payment_form(form: PaymentForm) -> Validation<PaymentForm, Vec<String>> {
        let v1 = validate_card_number(&form.card_number);
        let v2 = validate_expiry(form.expiry_month, form.expiry_year);
        let v3 = validate_cvv(&form.cvv);
        let v4 = validate_amount(form.amount);
        Validation::<((), (), (), ()), Vec<String>>::all((v1, v2, v3, v4)).map(|_| form)
    }

    // Valid payment
    let valid = PaymentForm {
        card_number: "4532-1234-5678-9010".to_string(),
        expiry_month: 12,
        expiry_year: 2027,
        cvv: "123".to_string(),
        amount: 99.99,
    };

    match validate_payment_form(valid) {
        Validation::Success(_) => println!("✓ Valid payment form"),
        Validation::Failure(errors) => {
            println!("✗ Invalid payment:");
            for error in errors {
                println!("  - {}", error);
            }
        }
    }

    // Invalid payment - multiple errors
    let invalid = PaymentForm {
        card_number: "1234".to_string(),
        expiry_month: 13,
        expiry_year: 2040,
        cvv: "12".to_string(),
        amount: -50.0,
    };

    println!();
    match validate_payment_form(invalid) {
        Validation::Success(_) => println!("✓ Valid payment form"),
        Validation::Failure(errors) => {
            println!("✗ Invalid payment ({} errors):", errors.len());
            for error in errors {
                println!("  - {}", error);
            }
        }
    }
}

// ==================== Cross-Field Validation ====================

/// Example 4: Cross-field validation
///
/// Demonstrates validating fields that depend on each other.
fn example_cross_field_validation() {
    println!("\n=== Example 4: Cross-Field Validation ===");

    #[derive(Debug)]
    struct RegistrationForm {
        password: String,
        confirm_password: String,
        email: String,
        alternate_email: String,
    }

    fn validate_password_strength(password: &str) -> Validation<(), Vec<String>> {
        let mut errors = Vec::new();

        if password.len() < 8 {
            errors.push("Password must be at least 8 characters".to_string());
        }
        if !password.chars().any(|c| c.is_uppercase()) {
            errors.push("Password must contain an uppercase letter".to_string());
        }
        if !password.chars().any(|c| c.is_lowercase()) {
            errors.push("Password must contain a lowercase letter".to_string());
        }
        if !password.chars().any(|c| c.is_numeric()) {
            errors.push("Password must contain a number".to_string());
        }

        if errors.is_empty() {
            Validation::success(())
        } else {
            Validation::failure(errors)
        }
    }

    fn validate_passwords_match(password: &str, confirm: &str) -> Validation<(), Vec<String>> {
        if password == confirm {
            Validation::success(())
        } else {
            Validation::failure(vec!["Passwords do not match".to_string()])
        }
    }

    fn validate_emails_different(email: &str, alternate: &str) -> Validation<(), Vec<String>> {
        if email.is_empty() || alternate.is_empty() || email != alternate {
            Validation::success(())
        } else {
            Validation::failure(vec![
                "Alternate email must be different from primary email".to_string()
            ])
        }
    }

    fn validate_registration(form: RegistrationForm) -> Validation<RegistrationForm, Vec<String>> {
        let v1 = validate_password_strength(&form.password);
        let v2 = validate_passwords_match(&form.password, &form.confirm_password);
        let v3 = validate_emails_different(&form.email, &form.alternate_email);
        Validation::<((), (), ()), Vec<String>>::all((v1, v2, v3)).map(|_| form)
    }

    // Valid registration
    let valid = RegistrationForm {
        password: "Secret123".to_string(),
        confirm_password: "Secret123".to_string(),
        email: "user@example.com".to_string(),
        alternate_email: "user.backup@example.com".to_string(),
    };

    match validate_registration(valid) {
        Validation::Success(_) => println!("✓ Valid registration"),
        Validation::Failure(errors) => {
            println!("✗ Invalid registration:");
            for error in errors {
                println!("  - {}", error);
            }
        }
    }

    // Invalid - password issues and mismatch
    let invalid1 = RegistrationForm {
        password: "weak".to_string(),
        confirm_password: "different".to_string(),
        email: "user@example.com".to_string(),
        alternate_email: "backup@example.com".to_string(),
    };

    println!();
    match validate_registration(invalid1) {
        Validation::Success(_) => println!("✓ Valid registration"),
        Validation::Failure(errors) => {
            println!("✗ Invalid registration ({} errors):", errors.len());
            for error in errors {
                println!("  - {}", error);
            }
        }
    }

    // Invalid - duplicate emails
    let invalid2 = RegistrationForm {
        password: "Secret123".to_string(),
        confirm_password: "Secret123".to_string(),
        email: "user@example.com".to_string(),
        alternate_email: "user@example.com".to_string(),
    };

    println!();
    match validate_registration(invalid2) {
        Validation::Success(_) => println!("✓ Valid registration"),
        Validation::Failure(errors) => {
            println!("✗ Invalid registration ({} errors):", errors.len());
            for error in errors {
                println!("  - {}", error);
            }
        }
    }
}

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

fn main() {
    println!("Form Validation Examples");
    println!("========================");

    example_contact_form();
    example_user_profile();
    example_payment_form();
    example_cross_field_validation();

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