autumn-web 0.6.0

An opinionated, convention-over-configuration web framework for Rust
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
//! Deterministic fake-data generation.
//!
//! This module backs the factory `.fake()` feature (issue #1343): it produces
//! realistic-looking values — names, emails, sentences, timestamps, and so on —
//! drawn from a per-thread pseudo-random generator.
//!
//! # Determinism
//!
//! The generator can run in two modes:
//!
//! - **Deterministic**: when the `AUTUMN_FAKE_SEED` environment variable is set
//!   to a `u64`, or when [`reseed`](crate::fake::reseed) is called explicitly, the process-global RNG
//!   is seeded from that value. The exact same sequence of calls then produces
//!   the exact same values on every run — ideal for golden tests and
//!   reproducible fixtures. In this mode time-based helpers such as
//!   [`recent_datetime`](crate::fake::recent_datetime) anchor to a fixed base instant rather than the wall
//!   clock, so even timestamps are reproducible.
//! - **Random**: with no seed configured, the RNG is seeded from OS entropy and
//!   output varies per run.
//!
//! The RNG is [`rand_chacha::ChaCha8Rng`], chosen because ChaCha is portable and
//! reproducible across platforms given the same seed.
//!
//! # Why process-global (not thread-local)
//!
//! The generator is a single process-global RNG behind a [`Mutex`](std::sync::Mutex), **not** a
//! per-thread one. `#[autumn_web::main]` runs on a multi-thread tokio runtime,
//! and a faked `create_many` awaits a pooled connection between rows, so the
//! driving task freely migrates between worker threads mid-run. A thread-local
//! RNG would let a freshly-touched thread lazily re-seed itself from the *same*
//! `AUTUMN_FAKE_SEED` and restart the identical sequence — producing duplicate
//! rows and destroying reproducibility under a seed. Drawing every value from
//! one shared sequence keeps a seeded run deterministic and distinct no matter
//! how tasks are scheduled across threads.

use std::sync::{Mutex, OnceLock, PoisonError};

use chrono::{DateTime, Utc};
use rand::{Rng, RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use rust_decimal::Decimal;
use uuid::Uuid;

/// Process-global generator state.
struct FakeState {
    rng: ChaCha8Rng,
    /// True when the RNG was seeded deterministically (via `AUTUMN_FAKE_SEED` or
    /// [`reseed`]). Drives whether time helpers anchor to a fixed instant.
    deterministic: bool,
}

/// The one shared generator, initialized on first use. All threads and tasks
/// draw from this single sequence so a seeded run is reproducible regardless of
/// how work is scheduled across the multi-thread runtime.
static RNG: OnceLock<Mutex<FakeState>> = OnceLock::new();

/// The fixed base instant used by time helpers in deterministic mode.
/// Chosen as `2024-01-01T00:00:00Z`.
const DETERMINISTIC_BASE_EPOCH_SECS: i64 = 1_704_067_200;

/// Get the process-global generator, initializing it on first use.
///
/// Seeds deterministically from `AUTUMN_FAKE_SEED` when that env var parses as a
/// `u64`; otherwise seeds from OS entropy.
fn global() -> &'static Mutex<FakeState> {
    RNG.get_or_init(|| {
        let seed = std::env::var("AUTUMN_FAKE_SEED")
            .ok()
            .and_then(|s| s.trim().parse::<u64>().ok());
        Mutex::new(seed.map_or_else(
            || FakeState {
                rng: ChaCha8Rng::from_os_rng(),
                deterministic: false,
            },
            |seed| FakeState {
                rng: ChaCha8Rng::seed_from_u64(seed),
                deterministic: true,
            },
        ))
    })
}

/// Lock the global state, recovering the inner value if a previous holder
/// panicked (a poisoned RNG is not a correctness hazard — the bytes are still
/// usable — so we never want to cascade a panic across unrelated draws).
fn lock_state() -> std::sync::MutexGuard<'static, FakeState> {
    global().lock().unwrap_or_else(PoisonError::into_inner)
}

/// Run `f` with a mutable reference to the process-global RNG.
fn with_rng<R>(f: impl FnOnce(&mut ChaCha8Rng) -> R) -> R {
    let mut state = lock_state();
    f(&mut state.rng)
}

/// Whether the generator is currently in deterministic mode.
fn is_deterministic() -> bool {
    lock_state().deterministic
}

/// Force the process-global generator into deterministic mode seeded from
/// `seed`.
///
/// After this call, a fixed sequence of generator calls yields a fixed sequence
/// of values, across every thread and task in the process. Primarily intended
/// for tests and reproducible fixtures.
pub fn reseed(seed: u64) {
    let mut state = lock_state();
    state.rng = ChaCha8Rng::seed_from_u64(seed);
    state.deterministic = true;
}

/// Serializes `reseed`-based tests so the process-global RNG is not reseeded by
/// a concurrently-running test in the middle of another test's draw sequence.
///
/// Hold the returned guard for the duration of any test that reseeds and then
/// asserts on the exact values drawn. Test-only; not part of the stable API.
#[doc(hidden)]
pub fn test_serial_guard() -> std::sync::MutexGuard<'static, ()> {
    static LOCK: Mutex<()> = Mutex::new(());
    LOCK.lock().unwrap_or_else(PoisonError::into_inner)
}

/// Pick a random element from a non-empty static list.
fn pick(list: &[&'static str]) -> &'static str {
    debug_assert!(!list.is_empty(), "fake: word list must be non-empty");
    let idx = with_rng(|r| r.random_range(0..list.len()));
    list[idx]
}

// ── People ──────────────────────────────────────────────────────────────────

/// A random first name, e.g. `"Olivia"`.
#[must_use]
pub fn first_name() -> String {
    pick(FIRST_NAMES).to_string()
}

/// A random last name, e.g. `"Nguyen"`.
#[must_use]
pub fn last_name() -> String {
    pick(LAST_NAMES).to_string()
}

/// A random full name (`"First Last"`).
#[must_use]
pub fn name() -> String {
    format!("{} {}", first_name(), last_name())
}

/// A lowercase, space-free username derived from a name plus digits,
/// e.g. `"olivianguyen473"`.
#[must_use]
pub fn username() -> String {
    // Draw both names and the digits under a single lock acquisition.
    with_rng(|r| {
        let first = FIRST_NAMES[r.random_range(0..FIRST_NAMES.len())].to_ascii_lowercase();
        let last = LAST_NAMES[r.random_range(0..LAST_NAMES.len())].to_ascii_lowercase();
        let n: u32 = r.random_range(0..1000);
        format!("{first}{last}{n}")
    })
}

/// A random email address containing exactly one `@`, e.g.
/// `"olivia473@example.com"`.
#[must_use]
pub fn email() -> String {
    // Local part (name + digits) and domain are drawn under one lock; the single
    // `@` separator keeps exactly one `@` in the result.
    with_rng(|r| {
        let first = FIRST_NAMES[r.random_range(0..FIRST_NAMES.len())].to_ascii_lowercase();
        let n: u32 = r.random_range(0..1000);
        let domain = DOMAINS[r.random_range(0..DOMAINS.len())];
        format!("{first}{n}@{domain}")
    })
}

// ── Text ────────────────────────────────────────────────────────────────────

/// A single lorem-style word.
#[must_use]
pub fn word() -> String {
    pick(LOREM).to_string()
}

/// `n` space-joined lorem words. Returns an empty string when `n == 0`.
#[must_use]
pub fn words(n: usize) -> String {
    if n == 0 {
        return String::new();
    }
    // Rough capacity: average lorem word (~6 chars) plus a joining space.
    let mut out = String::with_capacity(n * 8);
    // Build the whole string under a single lock acquisition.
    with_rng(|r| {
        for i in 0..n {
            if i > 0 {
                out.push(' ');
            }
            out.push_str(LOREM[r.random_range(0..LOREM.len())]);
        }
    });
    out
}

/// A capitalized sentence of several words ending in `'.'`.
#[must_use]
pub fn sentence() -> String {
    let n = with_rng(|r| r.random_range(4..12));
    let mut s = words(n);
    if let Some(head) = s.get_mut(0..1) {
        head.make_ascii_uppercase();
    }
    s.push('.');
    s
}

/// A paragraph of several sentences joined by spaces.
#[must_use]
pub fn paragraph() -> String {
    let n = with_rng(|r| r.random_range(3..7));
    let mut out = String::new();
    for i in 0..n {
        if i > 0 {
            out.push(' ');
        }
        out.push_str(&sentence());
    }
    out
}

// ── Scalars ─────────────────────────────────────────────────────────────────

/// A random URL beginning with `"https://"`.
#[must_use]
pub fn url() -> String {
    // Pick the domain and path segment under one lock acquisition.
    with_rng(|r| {
        let domain = DOMAINS[r.random_range(0..DOMAINS.len())];
        let path = LOREM[r.random_range(0..LOREM.len())];
        format!("https://{domain}/{path}")
    })
}

/// A random boolean.
#[must_use]
pub fn boolean() -> bool {
    with_rng(|r| r.random_bool(0.5))
}

/// A random integer within the inclusive range `[lo, hi]`.
///
/// If `lo >= hi`, returns `lo` (so the result is always within `[lo, hi]`).
#[must_use]
pub fn int_range(lo: i64, hi: i64) -> i64 {
    if lo >= hi {
        return lo;
    }
    with_rng(|r| r.random_range(lo..=hi))
}

/// A random non-negative [`Decimal`] with two fractional digits (0.00–9999.99).
#[must_use]
pub fn decimal() -> Decimal {
    let cents = with_rng(|r| r.random_range(0..1_000_000_i64));
    Decimal::new(cents, 2)
}

/// A random `f64` in `[0, 10000)`, for `f32`/`f64` fields.
#[must_use]
pub fn decimal_f64() -> f64 {
    with_rng(|r| r.random_range(0.0..10_000.0))
}

/// A timestamp within roughly the last 30 days.
///
/// In deterministic mode the offset is subtracted from a fixed base instant
/// (`2024-01-01T00:00:00Z`) so golden data is reproducible; otherwise it is
/// subtracted from [`Utc::now`].
#[must_use]
pub fn recent_datetime() -> DateTime<Utc> {
    const THIRTY_DAYS_SECS: i64 = 30 * 24 * 60 * 60;
    let offset = with_rng(|r| r.random_range(0..THIRTY_DAYS_SECS));
    // In deterministic mode, anchor to a fixed instant so timestamps reproduce.
    // `UNIX_EPOCH + N seconds` is infallible (no panic path).
    let base = if is_deterministic() {
        DateTime::<Utc>::UNIX_EPOCH + chrono::Duration::seconds(DETERMINISTIC_BASE_EPOCH_SECS)
    } else {
        Utc::now()
    };
    base - chrono::Duration::seconds(offset)
}

/// A random v4 UUID drawn from the seeded RNG (so it is reproducible under a
/// fixed seed, unlike [`Uuid::new_v4`] which uses OS entropy directly).
#[must_use]
pub fn uuid() -> Uuid {
    let mut bytes = [0u8; 16];
    with_rng(|r| r.fill_bytes(&mut bytes));
    // Set the version (4) and variant (RFC 4122) bits.
    bytes[6] = (bytes[6] & 0x0F) | 0x40;
    bytes[8] = (bytes[8] & 0x3F) | 0x80;
    Uuid::from_bytes(bytes)
}

// ── Word lists ──────────────────────────────────────────────────────────────
// Bundled static lists, sized so name()/email()/sentence() have high
// cardinality (AC3). ~120 first names, ~120 last names, ~150 lorem words.

/// First names (~120).
static FIRST_NAMES: &[&str] = &[
    "Olivia",
    "Liam",
    "Emma",
    "Noah",
    "Ava",
    "Oliver",
    "Sophia",
    "Elijah",
    "Isabella",
    "James",
    "Mia",
    "William",
    "Amelia",
    "Benjamin",
    "Harper",
    "Lucas",
    "Evelyn",
    "Henry",
    "Abigail",
    "Alexander",
    "Emily",
    "Mason",
    "Elizabeth",
    "Michael",
    "Sofia",
    "Ethan",
    "Avery",
    "Daniel",
    "Ella",
    "Jacob",
    "Scarlett",
    "Logan",
    "Grace",
    "Jackson",
    "Chloe",
    "Levi",
    "Victoria",
    "Sebastian",
    "Riley",
    "Mateo",
    "Aria",
    "Jack",
    "Lily",
    "Owen",
    "Aubrey",
    "Theodore",
    "Zoey",
    "Aiden",
    "Penelope",
    "Samuel",
    "Lillian",
    "Joseph",
    "Addison",
    "John",
    "Layla",
    "David",
    "Natalie",
    "Wyatt",
    "Camila",
    "Matthew",
    "Hannah",
    "Luke",
    "Brooklyn",
    "Asher",
    "Zoe",
    "Carter",
    "Nora",
    "Julian",
    "Leah",
    "Grayson",
    "Savannah",
    "Leo",
    "Audrey",
    "Jayden",
    "Claire",
    "Gabriel",
    "Eleanor",
    "Isaac",
    "Skylar",
    "Lincoln",
    "Ellie",
    "Anthony",
    "Samantha",
    "Hudson",
    "Stella",
    "Dylan",
    "Paisley",
    "Ezra",
    "Violet",
    "Thomas",
    "Mila",
    "Charles",
    "Allison",
    "Christopher",
    "Alexa",
    "Jaxon",
    "Anna",
    "Maverick",
    "Hazel",
    "Josiah",
    "Aaliyah",
    "Isaiah",
    "Ariana",
    "Andrew",
    "Gabriella",
    "Elias",
    "Alice",
    "Joshua",
    "Sarah",
    "Nathan",
    "Ruby",
    "Caleb",
    "Eva",
    "Ryan",
    "Serenity",
    "Adrian",
    "Autumn",
    "Miles",
    "Quinn",
    "Eli",
    "Nova",
];

/// Last names (~120).
static LAST_NAMES: &[&str] = &[
    "Smith",
    "Johnson",
    "Williams",
    "Brown",
    "Jones",
    "Garcia",
    "Miller",
    "Davis",
    "Rodriguez",
    "Martinez",
    "Hernandez",
    "Lopez",
    "Gonzalez",
    "Wilson",
    "Anderson",
    "Thomas",
    "Taylor",
    "Moore",
    "Jackson",
    "Martin",
    "Lee",
    "Perez",
    "Thompson",
    "White",
    "Harris",
    "Sanchez",
    "Clark",
    "Ramirez",
    "Lewis",
    "Robinson",
    "Walker",
    "Young",
    "Allen",
    "King",
    "Wright",
    "Scott",
    "Torres",
    "Nguyen",
    "Hill",
    "Flores",
    "Green",
    "Adams",
    "Nelson",
    "Baker",
    "Hall",
    "Rivera",
    "Campbell",
    "Mitchell",
    "Carter",
    "Roberts",
    "Gomez",
    "Phillips",
    "Evans",
    "Turner",
    "Diaz",
    "Parker",
    "Cruz",
    "Edwards",
    "Collins",
    "Reyes",
    "Stewart",
    "Morris",
    "Morales",
    "Murphy",
    "Cook",
    "Rogers",
    "Gutierrez",
    "Ortiz",
    "Morgan",
    "Cooper",
    "Peterson",
    "Bailey",
    "Reed",
    "Kelly",
    "Howard",
    "Ramos",
    "Kim",
    "Cox",
    "Ward",
    "Richardson",
    "Watson",
    "Brooks",
    "Chavez",
    "Wood",
    "James",
    "Bennett",
    "Gray",
    "Mendoza",
    "Ruiz",
    "Hughes",
    "Price",
    "Alvarez",
    "Castillo",
    "Sanders",
    "Patel",
    "Myers",
    "Long",
    "Ross",
    "Foster",
    "Jimenez",
    "Powell",
    "Jenkins",
    "Perry",
    "Russell",
    "Sullivan",
    "Bell",
    "Coleman",
    "Butler",
    "Henderson",
    "Barnes",
    "Gonzales",
    "Fisher",
    "Vasquez",
    "Simmons",
    "Romero",
    "Jordan",
    "Patterson",
    "Alexander",
    "Hamilton",
    "Graham",
    "Reynolds",
];

/// Lorem-ipsum vocabulary (~150).
static LOREM: &[&str] = &[
    "lorem",
    "ipsum",
    "dolor",
    "sit",
    "amet",
    "consectetur",
    "adipiscing",
    "elit",
    "sed",
    "do",
    "eiusmod",
    "tempor",
    "incididunt",
    "ut",
    "labore",
    "et",
    "dolore",
    "magna",
    "aliqua",
    "enim",
    "ad",
    "minim",
    "veniam",
    "quis",
    "nostrud",
    "exercitation",
    "ullamco",
    "laboris",
    "nisi",
    "aliquip",
    "ex",
    "ea",
    "commodo",
    "consequat",
    "duis",
    "aute",
    "irure",
    "in",
    "reprehenderit",
    "voluptate",
    "velit",
    "esse",
    "cillum",
    "eu",
    "fugiat",
    "nulla",
    "pariatur",
    "excepteur",
    "sint",
    "occaecat",
    "cupidatat",
    "non",
    "proident",
    "sunt",
    "culpa",
    "qui",
    "officia",
    "deserunt",
    "mollit",
    "anim",
    "id",
    "est",
    "laborum",
    "perspiciatis",
    "unde",
    "omnis",
    "iste",
    "natus",
    "error",
    "voluptatem",
    "accusantium",
    "doloremque",
    "laudantium",
    "totam",
    "rem",
    "aperiam",
    "eaque",
    "ipsa",
    "quae",
    "ab",
    "illo",
    "inventore",
    "veritatis",
    "quasi",
    "architecto",
    "beatae",
    "vitae",
    "dicta",
    "explicabo",
    "nemo",
    "ipsam",
    "quia",
    "voluptas",
    "aspernatur",
    "aut",
    "odit",
    "fugit",
    "consequuntur",
    "magni",
    "dolores",
    "eos",
    "ratione",
    "sequi",
    "nesciunt",
    "neque",
    "porro",
    "quisquam",
    "dolorem",
    "adipisci",
    "numquam",
    "eius",
    "modi",
    "tempora",
    "incidunt",
    "magnam",
    "quaerat",
    "voluptatem",
    "minus",
    "quod",
    "maxime",
    "placeat",
    "facere",
    "possimus",
    "assumenda",
    "repellendus",
    "temporibus",
    "quibusdam",
    "officiis",
    "debitis",
    "rerum",
    "necessitatibus",
    "saepe",
    "eveniet",
    "voluptates",
    "repudiandae",
    "recusandae",
    "itaque",
    "earum",
    "hic",
    "tenetur",
    "sapiente",
    "delectus",
    "reiciendis",
    "voluptatibus",
    "maiores",
    "alias",
    "perferendis",
    "doloribus",
    "asperiores",
    "repellat",
];

/// Email/URL domains (mixes several second-level names and TLDs).
static DOMAINS: &[&str] = &[
    "example.com",
    "example.org",
    "example.net",
    "test.com",
    "mail.com",
    "acme.io",
    "globex.dev",
    "initech.co",
    "umbrella.app",
    "hooli.tech",
    "stark.io",
    "wayne.net",
];