renox-core 1.2.0

Runtime of the Renox web framework: routing, views, database and models, validation, auth, queue, scheduler, mail, storage. Use it through the `renox` crate.
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! Brand themes: colour math and the contrast core.
//!
//! This part holds the building blocks the theme derivation uses: sRGB and
//! OKLCH conversion, hex parsing, the WCAG 2.x contrast ratio, mixing, and a
//! lightness search that finds the nearest colour passing a check. On top of
//! it, [`Brand`] and [`Theme`] derive the kit's accent tokens (light and dark)
//! from one or two brand colours and refuse colours that cannot reach readable
//! contrast. No I/O.

use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::{Arc, PoisonError, RwLock};

use sha2::{Digest, Sha256};

use self::color::{Oklch, Rgb, nearest_passing};

/// The kit's light page background (`--rx-bg`), one of the colours text is checked on.
const LIGHT_BG: Rgb = Rgb::new(0xf6, 0xf1, 0xe8);
/// The kit's light card surface (`--rx-surface`).
const LIGHT_SURFACE: Rgb = Rgb::new(0xff, 0xfd, 0xf9);
/// The kit's dark page background.
const DARK_BG: Rgb = Rgb::new(0x15, 0x14, 0x12);
/// The kit's dark card surface.
const DARK_SURFACE: Rgb = Rgb::new(0x1d, 0x1b, 0x18);
const WHITE: Rgb = Rgb::new(255, 255, 255);
const NEAR_BLACK: Rgb = Rgb::new(0x1f, 0x24, 0x21);
/// Contrast for text (WCAG AA).
const TEXT_RATIO: f64 = 4.5;
/// Contrast for button labels and chart marks (WCAG AA for large text and graphics).
const LARGE_RATIO: f64 = 3.0;
/// How far (OKLCH lightness) a brand colour may move to become a readable
/// text colour before the brand is refused. Dark mode may lighten further:
/// a very dark brand colour (black, navy) is a normal choice there.
const MAX_SHIFT: f64 = 0.3;
const MAX_SHIFT_DARK: f64 = 0.7;

/// A brand: one colour, an optional second one and an optional display font.
///
/// ```
/// use renox::theme::{Brand, Theme};
///
/// let brand = Brand::new("#0F766E").second("#B8532E").display_font("Poppins");
/// let theme = Theme::from_brand(&brand).unwrap();
/// assert!(theme.css().contains("--rx-accent"));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Brand {
    color: String,
    second: Option<String>,
    display_font: Option<String>,
}

impl Brand {
    /// A brand with this colour, `#rgb` or `#rrggbb`.
    pub fn new(color: impl Into<String>) -> Self {
        Self {
            color: color.into(),
            second: None,
            display_font: None,
        }
    }

    /// Sets the second brand colour (`--rx-accent-2`).
    #[must_use]
    pub fn second(mut self, color: impl Into<String>) -> Self {
        self.second = Some(color.into());
        self
    }

    /// Sets the display font family (`--rx-font-display`). The name is reduced
    /// to letters, digits, spaces, `-` and `_`; the font itself is not loaded.
    #[must_use]
    pub fn display_font(mut self, family: impl Into<String>) -> Self {
        self.display_font = Some(family.into());
        self
    }
}

/// What went wrong in [`Theme::from_brand`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContrastErrorKind {
    /// The text is not a `#rgb` / `#rrggbb` colour.
    InvalidColor,
    /// The colour cannot reach readable contrast.
    LowContrast,
}

/// A brand colour that is malformed or cannot reach readable contrast.
///
/// For [`ContrastErrorKind::LowContrast`] the `suggestion` is the nearest
/// colour that [`Theme::from_brand`] accepts, so a settings form can show it.
///
/// ```
/// use renox::theme::{Brand, Theme};
///
/// let err = Theme::from_brand(&Brand::new("#FFF59D")).unwrap_err();
/// assert!(Theme::from_brand(&Brand::new(err.suggestion.as_str())).is_ok());
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContrastError {
    /// What went wrong.
    pub kind: ContrastErrorKind,
    /// The colour as given.
    pub color: String,
    /// The nearest passing colour as `#rrggbb`; empty for an invalid colour.
    pub suggestion: String,
}

impl fmt::Display for ContrastError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            ContrastErrorKind::InvalidColor => {
                write!(f, "`{}` is not a colour like #0f766e", self.color)
            }
            ContrastErrorKind::LowContrast => write!(
                f,
                "`{}` cannot reach readable contrast; try {}",
                self.color, self.suggestion
            ),
        }
    }
}

impl std::error::Error for ContrastError {}

/// The accent tokens of a brand for one colour scheme.
#[derive(Debug, Clone)]
struct Scheme {
    accent: Rgb,
    hover: Rgb,
    pressed: Rgb,
    text: Rgb,
    on_accent: Rgb,
    soft: Rgb,
    focus: String,
    chart: Rgb,
    second: Option<(Rgb, Rgb)>,
}

impl Scheme {
    /// Derives the tokens for `brand` on `bg`/`surface`; `None` when no text
    /// colour within [`MAX_SHIFT`] (or [`MAX_SHIFT_DARK`]) reaches [`TEXT_RATIO`].
    fn derive(brand: Rgb, second: Option<Rgb>, bg: Rgb, surface: Rgb, dark: bool) -> Option<Self> {
        let shift = if dark { MAX_SHIFT_DARK } else { MAX_SHIFT };
        let readable = |c: Rgb| c.contrast(bg) >= TEXT_RATIO && c.contrast(surface) >= TEXT_RATIO;
        let text = nearest_passing(brand, shift, readable)?;
        let second = match second {
            Some(c) => Some((c, nearest_passing(c, shift, readable)?)),
            None => None,
        };
        let (white, black) = (brand.contrast(WHITE), brand.contrast(NEAR_BLACK));
        let on_accent = if white >= TEXT_RATIO || white >= black {
            WHITE
        } else {
            NEAR_BLACK
        };
        if brand.contrast(on_accent) < LARGE_RATIO {
            return None;
        }
        // Hover and pressed move so the label keeps (or gains) contrast.
        let step = |by: f64| {
            let o = brand.to_oklch();
            let l = if on_accent == WHITE {
                o.l - by
            } else {
                o.l + by
            };
            Oklch { l, ..o }.to_rgb()
        };
        let chart =
            nearest_passing(brand, shift, |c| c.contrast(surface) >= LARGE_RATIO).unwrap_or(brand);
        let soft = surface.mix(brand, if dark { 0.22 } else { 0.14 });
        let (focus_base, alpha) = if dark { (text, 0.45) } else { (brand, 0.35) };
        Some(Self {
            accent: brand,
            hover: step(0.03),
            pressed: step(0.06),
            text,
            on_accent,
            soft,
            focus: format!(
                "rgba({}, {}, {}, {alpha})",
                focus_base.r, focus_base.g, focus_base.b
            ),
            chart,
            second,
        })
    }

    fn tokens(&self) -> BTreeMap<&'static str, String> {
        let mut t = BTreeMap::new();
        t.insert("--rx-accent", self.accent.to_hex());
        t.insert("--rx-accent-hover", self.hover.to_hex());
        t.insert("--rx-accent-pressed", self.pressed.to_hex());
        t.insert("--rx-accent-text", self.text.to_hex());
        t.insert("--rx-on-accent", self.on_accent.to_hex());
        t.insert("--rx-accent-soft", self.soft.to_hex());
        t.insert("--rx-focus", self.focus.clone());
        t.insert("--rx-chart-1", self.chart.to_hex());
        if let Some((c, text)) = self.second {
            t.insert("--rx-accent-2", c.to_hex());
            t.insert("--rx-accent-2-text", text.to_hex());
        }
        t
    }
}

/// The accent tokens derived from a [`Brand`], for light and dark mode.
///
/// Build it with [`Theme::from_brand`]; [`Theme::css`] is the stylesheet,
/// [`Theme::tokens`] and [`Theme::dark_tokens`] the values as data.
#[derive(Debug, Clone)]
pub struct Theme {
    light: BTreeMap<&'static str, String>,
    dark: BTreeMap<&'static str, String>,
    css: String,
    hash: String,
}

impl Theme {
    /// Derives the kit's accent tokens from `brand`.
    ///
    /// Text colours must reach 4.5:1 on the kit's background and surface in
    /// both colour schemes, the label on the accent 3:1. The brand colour
    /// itself is kept; only the text variants move in lightness. When that is
    /// not enough, the error carries the nearest colour that works.
    ///
    /// ```
    /// use renox::theme::{Brand, Theme};
    ///
    /// assert!(Theme::from_brand(&Brand::new("#1E3A5F")).is_ok());
    /// assert!(Theme::from_brand(&Brand::new("#FFF59D")).is_err());
    /// ```
    ///
    /// # Errors
    ///
    /// [`ContrastError`] for a malformed colour or one that cannot reach
    /// readable contrast.
    pub fn from_brand(brand: &Brand) -> Result<Self, ContrastError> {
        let invalid = |c: &str| ContrastError {
            kind: ContrastErrorKind::InvalidColor,
            color: c.to_string(),
            suggestion: String::new(),
        };
        let low = |color: &str, suggestion: String| ContrastError {
            kind: ContrastErrorKind::LowContrast,
            color: color.to_string(),
            suggestion,
        };
        let main = Rgb::parse_hex(&brand.color).ok_or_else(|| invalid(&brand.color))?;
        let second = match &brand.second {
            Some(c) => Some(Rgb::parse_hex(c).ok_or_else(|| invalid(c))?),
            None => None,
        };
        let derive = |main: Rgb, second: Option<Rgb>| {
            let light = Scheme::derive(main, second, LIGHT_BG, LIGHT_SURFACE, false)?;
            let dark = Scheme::derive(main, second, DARK_BG, DARK_SURFACE, true)?;
            Some((light, dark))
        };
        // The suggestion is searched over the whole lightness range, and is
        // checked with the same derivation, so it always passes.
        if derive(main, None).is_none() {
            let nearest = nearest_passing(main, 1.0, |c| derive(c, None).is_some());
            return Err(low(&brand.color, nearest.unwrap_or(main).to_hex()));
        }
        if let (Some(c), Some(raw)) = (second, &brand.second)
            && derive(main, Some(c)).is_none()
        {
            let nearest = nearest_passing(c, 1.0, |x| derive(main, Some(x)).is_some());
            return Err(low(raw, nearest.unwrap_or(c).to_hex()));
        }
        let (light, dark) = derive(main, second).expect("checked above");
        let mut light_tokens = light.tokens();
        if let Some(font) = brand.display_font.as_deref().and_then(sanitize_family) {
            light_tokens.insert("--rx-font-display", format!("\"{font}\", var(--rx-font)"));
        }
        let dark_tokens = dark.tokens();
        let css = render_css(&light_tokens, &dark_tokens);
        let hash = hash_css(&css);
        Ok(Self {
            light: light_tokens,
            dark: dark_tokens,
            css,
            hash,
        })
    }

    /// The stylesheet: a `:root` block for light mode and the kit's two dark
    /// selectors (`prefers-color-scheme: dark` and `[data-theme="dark"]`).
    /// Load it after the kit's CSS.
    pub fn css(&self) -> &str {
        &self.css
    }

    /// A short content hash of [`Theme::css`] (16 hex digits), the key of the
    /// theme's cacheable stylesheet URL.
    pub fn hash(&self) -> &str {
        &self.hash
    }

    /// The derived tokens for light mode, such as `--rx-accent` => `#0f766e`.
    pub fn tokens(&self) -> BTreeMap<&'static str, String> {
        self.light.clone()
    }

    /// The derived tokens for dark mode.
    pub fn dark_tokens(&self) -> BTreeMap<&'static str, String> {
        self.dark.clone()
    }
}

/// What a [`App::theme_for`](crate::App::theme_for) chooser gets: the request
/// a page is rendered for, to pick the brand by host, tenant or setting.
#[derive(Clone)]
#[non_exhaustive]
pub struct ThemeRequest {
    /// The request's host without the port, e.g. `shop.example.com`
    /// (empty when the request has none).
    pub host: String,
    /// The request's path, e.g. `/products`.
    pub path: String,
    /// The request's language, e.g. `en`.
    pub locale: String,
    /// The logged-in user, if any.
    pub user: Option<Arc<crate::auth::User>>,
    /// The application state, e.g. to read a tenant's brand from the database.
    pub state: crate::AppState,
}

/// How a per-request theme chooser is kept.
pub(crate) type ThemeFn = Arc<
    dyn Fn(
            ThemeRequest,
        ) -> std::pin::Pin<
            Box<dyn std::future::Future<Output = crate::Result<Option<Theme>>> + Send>,
        > + Send
        + Sync,
>;

pub(crate) fn theme_fn<F, Fut>(choose: F) -> ThemeFn
where
    F: Fn(ThemeRequest) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = crate::Result<Option<Theme>>> + Send + 'static,
{
    Arc::new(move |req| Box::pin(choose(req)))
}

/// Every theme the app can serve, by content hash, and which one a request
/// gets.
///
/// Filled when the app sets a theme and when a per-request chooser returns
/// one, so `/_renox/theme/{hash}.css` can answer any hash a page was given.
/// Cloning shares the map.
#[derive(Clone, Default)]
pub(crate) struct ThemeRegistry {
    themes: Arc<RwLock<HashMap<String, Arc<str>>>>,
    /// The URL of the app's own theme (`App::theme`).
    app_url: Option<String>,
    /// The per-request chooser (`App::theme_for`).
    chooser: Option<ThemeFn>,
}

impl ThemeRegistry {
    /// A registry for an app with `app` as its theme and `chooser` picking
    /// per request.
    pub(crate) fn new(app: Option<&Theme>, chooser: Option<ThemeFn>) -> Self {
        let mut registry = Self {
            chooser,
            ..Self::default()
        };
        registry.app_url = app.map(|theme| registry.register(theme));
        registry
    }

    /// Remembers `theme`'s stylesheet under its hash and returns its URL.
    /// A theme already known is not copied again.
    pub(crate) fn register(&self, theme: &Theme) -> String {
        let known = self
            .themes
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .contains_key(theme.hash());
        if !known {
            self.themes
                .write()
                .unwrap_or_else(PoisonError::into_inner)
                .entry(theme.hash().to_owned())
                .or_insert_with(|| theme.css().into());
        }
        theme_url(theme.hash())
    }

    /// The stylesheet registered under `hash`.
    pub(crate) fn css(&self, hash: &str) -> Option<Arc<str>> {
        self.themes
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .get(hash)
            .cloned()
    }

    /// The stylesheet URL for a request: the chooser's theme, else the
    /// app's, else none (the kit's own look). A chooser that fails is logged
    /// and falls back like one that answers `None`.
    pub(crate) async fn resolve(&self, req: ThemeRequest) -> Option<String> {
        if let Some(choose) = &self.chooser {
            match choose(req).await {
                Ok(Some(theme)) => return Some(self.register(&theme)),
                Ok(None) => {}
                Err(err) => tracing::warn!(error = ?err, "the theme chooser failed"),
            }
        }
        self.app_url.clone()
    }

    /// Whether a theme can be chosen at all, so a request without one skips
    /// the work.
    pub(crate) fn is_set(&self) -> bool {
        self.app_url.is_some() || self.chooser.is_some()
    }
}

/// The URL of the stylesheet for a theme hash.
pub(crate) fn theme_url(hash: &str) -> String {
    format!("/_renox/theme/{hash}.css")
}

fn sanitize_family(name: &str) -> Option<String> {
    let clean: String = name
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, ' ' | '-' | '_'))
        .collect();
    let clean = clean.trim().to_string();
    (!clean.is_empty()).then_some(clean)
}

fn declarations(tokens: &BTreeMap<&'static str, String>) -> String {
    tokens.iter().map(|(k, v)| format!("{k}:{v};")).collect()
}

fn render_css(
    light: &BTreeMap<&'static str, String>,
    dark: &BTreeMap<&'static str, String>,
) -> String {
    let dark = declarations(dark);
    format!(
        ":root{{{}}}\n@media (prefers-color-scheme:dark){{:root:not([data-theme=\"light\"]){{{dark}}}}}\n:root[data-theme=\"dark\"]{{{dark}}}\n",
        declarations(light)
    )
}

fn hash_css(css: &str) -> String {
    Sha256::digest(css.as_bytes())
        .iter()
        .take(8)
        .map(|b| format!("{b:02x}"))
        .collect()
}

#[cfg(test)]
mod theme_tests {
    use super::*;

    #[test]
    fn registry_serves_what_it_handed_out() {
        let registry = ThemeRegistry::default();
        let theme = Theme::from_brand(&Brand::new("#1E3A5F")).unwrap();
        let url = registry.register(&theme);
        assert_eq!(url, format!("/_renox/theme/{}.css", theme.hash()));
        assert_eq!(registry.register(&theme), url);
        assert_eq!(&*registry.css(theme.hash()).unwrap(), theme.css());
        assert!(registry.css("0000000000000000").is_none());
        // A clone shares the map.
        assert!(registry.clone().css(theme.hash()).is_some());
    }

    fn rgb(t: &BTreeMap<&'static str, String>, k: &str) -> Rgb {
        Rgb::parse_hex(&t[k]).unwrap_or_else(|| panic!("{k} is not a hex colour: {}", t[k]))
    }

    fn assert_ratios(brand: &str) {
        let theme = Theme::from_brand(&Brand::new(brand).second("#b8532e")).expect(brand);
        for (t, bg, surface) in [
            (theme.tokens(), LIGHT_BG, LIGHT_SURFACE),
            (theme.dark_tokens(), DARK_BG, DARK_SURFACE),
        ] {
            for key in ["--rx-accent-text", "--rx-accent-2-text"] {
                let c = rgb(&t, key);
                assert!(c.contrast(bg) >= 4.5, "{brand} {key} on bg");
                assert!(c.contrast(surface) >= 4.5, "{brand} {key} on surface");
            }
            let accent = rgb(&t, "--rx-accent");
            assert!(
                rgb(&t, "--rx-on-accent").contrast(accent) >= 3.0,
                "{brand} label"
            );
            for key in [
                "--rx-accent-hover",
                "--rx-accent-pressed",
                "--rx-accent-soft",
                "--rx-chart-1",
            ] {
                assert!(t.contains_key(key), "{key}");
            }
            assert!(t["--rx-focus"].starts_with("rgba("));
        }
    }

    #[test]
    fn accepted_colours_meet_the_ratios_in_both_modes() {
        for c in [
            "#0b6e66", "#1E3A5F", "#0F766E", "#b8532e", "#7c3aed", "#000", "#333",
        ] {
            assert_ratios(c);
        }
    }

    #[test]
    fn the_kit_default_accent_is_kept() {
        let t = Theme::from_brand(&Brand::new("#0b6e66")).unwrap().tokens();
        assert_eq!(t["--rx-accent"], "#0b6e66");
        assert_eq!(t["--rx-on-accent"], "#ffffff");
        assert_eq!(t["--rx-accent-text"], "#0b6e66");
    }

    #[test]
    fn pale_yellow_is_refused_with_a_passing_suggestion() {
        let err = Theme::from_brand(&Brand::new("#FFF59D")).unwrap_err();
        assert_eq!(err.kind, ContrastErrorKind::LowContrast);
        assert!(err.to_string().contains(&err.suggestion));
        assert_ne!(err.suggestion, "#fff59d");
        assert_ratios(&err.suggestion);
    }

    #[test]
    fn a_failing_second_colour_is_refused_and_named() {
        let err = Theme::from_brand(&Brand::new("#0f766e").second("#fff59d")).unwrap_err();
        assert_eq!(err.color, "#fff59d");
        Theme::from_brand(&Brand::new("#0f766e").second(err.suggestion.as_str())).unwrap();
    }

    #[test]
    fn malformed_hex_is_a_distinct_error() {
        for bad in ["", "red", "#12", "#ggg"] {
            let err = Theme::from_brand(&Brand::new(bad)).unwrap_err();
            assert_eq!(err.kind, ContrastErrorKind::InvalidColor, "{bad}");
            assert!(err.suggestion.is_empty());
        }
        let err = Theme::from_brand(&Brand::new("#0f766e").second("nope")).unwrap_err();
        assert_eq!(err.kind, ContrastErrorKind::InvalidColor);
    }

    #[test]
    fn second_colour_and_display_font_are_optional_tokens() {
        let plain = Theme::from_brand(&Brand::new("#0f766e")).unwrap().tokens();
        assert!(!plain.contains_key("--rx-accent-2"));
        assert!(!plain.contains_key("--rx-font-display"));
        let theme = Theme::from_brand(
            &Brand::new("#0f766e")
                .second("#b8532e")
                .display_font("Poppins\";}body{x:y"),
        )
        .unwrap();
        let t = theme.tokens();
        assert_eq!(t["--rx-accent-2"], "#b8532e");
        assert_eq!(t["--rx-font-display"], "\"Poppinsbodyxy\", var(--rx-font)");
        // The font only goes into light mode (dark inherits it).
        assert!(!theme.dark_tokens().contains_key("--rx-font-display"));
    }

    #[test]
    fn css_has_the_kits_three_selector_blocks() {
        let css = Theme::from_brand(&Brand::new("#1e3a5f"))
            .unwrap()
            .css()
            .to_string();
        assert!(css.starts_with(":root{--rx-accent:"));
        assert!(
            css.contains("@media (prefers-color-scheme:dark){:root:not([data-theme=\"light\"]){")
        );
        assert!(css.contains(":root[data-theme=\"dark\"]{"));
        // No `:where` on the generated selectors, so the brand is at least as
        // specific as the kit's `data-rx-theme` blocks and, loaded later, wins.
        assert!(!css.contains(":where"));
    }

    #[test]
    fn hash_follows_the_content() {
        let a = Theme::from_brand(&Brand::new("#1e3a5f")).unwrap();
        let b = Theme::from_brand(&Brand::new("#1E3A5F")).unwrap();
        let c = Theme::from_brand(&Brand::new("#0f766e")).unwrap();
        assert_eq!(a.hash(), b.hash());
        assert_ne!(a.hash(), c.hash());
        assert_eq!(a.hash().len(), 16);
    }
}

pub(crate) mod color {
    //! sRGB, OKLCH and WCAG contrast math.

    /// An 8-bit sRGB colour.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(crate) struct Rgb {
        pub(crate) r: u8,
        pub(crate) g: u8,
        pub(crate) b: u8,
    }

    /// A colour in OKLCH: lightness `l` in 0..=1, chroma `c`, hue `h` in degrees.
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub(crate) struct Oklch {
        pub(crate) l: f64,
        pub(crate) c: f64,
        pub(crate) h: f64,
    }

    impl Rgb {
        /// Builds a colour from its three channels.
        pub(crate) const fn new(r: u8, g: u8, b: u8) -> Self {
            Self { r, g, b }
        }

        /// Parses `#rgb` or `#rrggbb` (the `#` is optional, case is ignored).
        pub(crate) fn parse_hex(s: &str) -> Option<Self> {
            let s = s.trim();
            let s = s.strip_prefix('#').unwrap_or(s);
            if !s.chars().all(|c| c.is_ascii_hexdigit()) {
                return None;
            }
            let digit = |i: usize| u8::from_str_radix(&s[i..=i], 16).ok();
            let pair = |i: usize| u8::from_str_radix(&s[i..i + 2], 16).ok();
            match s.len() {
                3 => Some(Self::new(digit(0)? * 17, digit(1)? * 17, digit(2)? * 17)),
                6 => Some(Self::new(pair(0)?, pair(2)?, pair(4)?)),
                _ => None,
            }
        }

        /// The colour as lowercase `#rrggbb`.
        pub(crate) fn to_hex(self) -> String {
            format!("#{:02x}{:02x}{:02x}", self.r, self.g, self.b)
        }

        /// WCAG relative luminance, 0 (black) to 1 (white).
        pub(crate) fn luminance(self) -> f64 {
            let lin = |v: u8| srgb_to_linear(f64::from(v) / 255.0);
            0.2126 * lin(self.r) + 0.7152 * lin(self.g) + 0.0722 * lin(self.b)
        }

        /// The WCAG contrast ratio against `other`, 1.0 to 21.0.
        pub(crate) fn contrast(self, other: Rgb) -> f64 {
            let (a, b) = (self.luminance(), other.luminance());
            let (hi, lo) = if a >= b { (a, b) } else { (b, a) };
            (hi + 0.05) / (lo + 0.05)
        }

        /// This colour in OKLCH.
        pub(crate) fn to_oklch(self) -> Oklch {
            let (l, a, b) = self.to_oklab();
            let c = a.hypot(b);
            let h = if c < 1e-9 {
                0.0
            } else {
                b.atan2(a).to_degrees().rem_euclid(360.0)
            };
            Oklch { l, c, h }
        }

        fn to_oklab(self) -> (f64, f64, f64) {
            let r = srgb_to_linear(f64::from(self.r) / 255.0);
            let g = srgb_to_linear(f64::from(self.g) / 255.0);
            let b = srgb_to_linear(f64::from(self.b) / 255.0);
            let l = (0.412_221_470_8 * r + 0.536_332_536_3 * g + 0.051_445_992_9 * b).cbrt();
            let m = (0.211_903_498_2 * r + 0.680_699_545_1 * g + 0.107_396_956_6 * b).cbrt();
            let s = (0.088_302_461_9 * r + 0.281_718_837_6 * g + 0.629_978_700_5 * b).cbrt();
            (
                0.210_454_255_3 * l + 0.793_617_785 * m - 0.004_072_046_8 * s,
                1.977_998_495_1 * l - 2.428_592_205 * m + 0.450_593_709_9 * s,
                0.025_904_037_1 * l + 0.782_771_766_2 * m - 0.808_675_766 * s,
            )
        }

        fn from_oklab(l: f64, a: f64, b: f64) -> (f64, f64, f64) {
            let l_ = l + 0.396_337_777_4 * a + 0.215_803_757_3 * b;
            let m_ = l - 0.105_561_345_8 * a - 0.063_854_172_8 * b;
            let s_ = l - 0.089_484_177_5 * a - 1.291_485_548 * b;
            let (l, m, s) = (l_ * l_ * l_, m_ * m_ * m_, s_ * s_ * s_);
            (
                4.076_741_662_1 * l - 3.307_711_591_3 * m + 0.230_969_929_2 * s,
                -1.268_438_004_6 * l + 2.609_757_401_1 * m - 0.341_319_396_5 * s,
                -0.004_196_086_3 * l - 0.703_418_614_7 * m + 1.707_614_701 * s,
            )
        }

        /// Mixes `self` with `other` in OKLab; `t` is the share of `other`, 0..=1.
        pub(crate) fn mix(self, other: Rgb, t: f64) -> Rgb {
            let t = t.clamp(0.0, 1.0);
            let (a, b) = (self.to_oklab(), other.to_oklab());
            let lerp = |x: f64, y: f64| x + (y - x) * t;
            let (r, g, bl) = Self::from_oklab(lerp(a.0, b.0), lerp(a.1, b.1), lerp(a.2, b.2));
            from_linear(r, g, bl)
        }
    }

    impl Oklch {
        /// The colour as sRGB. When it is outside the sRGB gamut the chroma is
        /// reduced (hue and lightness kept) until it fits.
        pub(crate) fn to_rgb(self) -> Rgb {
            let l = self.l.clamp(0.0, 1.0);
            let lin = |c: f64| {
                let (a, b) = (c * self.h.to_radians().cos(), c * self.h.to_radians().sin());
                Rgb::from_oklab(l, a, b)
            };
            let in_gamut = |(r, g, b): (f64, f64, f64)| {
                let ok = |v: f64| (-1e-4..=1.0 + 1e-4).contains(&v);
                ok(r) && ok(g) && ok(b)
            };
            let mut c = self.c.max(0.0);
            if !in_gamut(lin(c)) {
                let (mut lo, mut hi) = (0.0, c);
                for _ in 0..24 {
                    let mid = (lo + hi) / 2.0;
                    if in_gamut(lin(mid)) {
                        lo = mid;
                    } else {
                        hi = mid;
                    }
                }
                c = lo;
            }
            let (r, g, b) = lin(c);
            from_linear(r, g, b)
        }
    }

    fn srgb_to_linear(v: f64) -> f64 {
        if v <= 0.040_45 {
            v / 12.92
        } else {
            ((v + 0.055) / 1.055).powf(2.4)
        }
    }

    fn linear_to_srgb(v: f64) -> f64 {
        let v = v.clamp(0.0, 1.0);
        if v <= 0.003_130_8 {
            v * 12.92
        } else {
            1.055 * v.powf(1.0 / 2.4) - 0.055
        }
    }

    fn from_linear(r: f64, g: f64, b: f64) -> Rgb {
        let ch = |v: f64| (linear_to_srgb(v) * 255.0).round() as u8;
        Rgb::new(ch(r), ch(g), ch(b))
    }

    /// The colour nearest to `from` in lightness (hue and chroma kept) for which
    /// `passes` holds, searching at most `max_shift` of OKLCH lightness up or down.
    /// `from` itself is returned when it already passes. `None` when nothing in
    /// range passes.
    pub(crate) fn nearest_passing(
        from: Rgb,
        max_shift: f64,
        passes: impl Fn(Rgb) -> bool,
    ) -> Option<Rgb> {
        if passes(from) {
            return Some(from);
        }
        let base = from.to_oklch();
        let step = 0.002;
        let mut i = 1.0;
        while i * step <= max_shift + 1e-12 {
            let shift = i * step;
            let at = |l: f64| {
                (0.0..=1.0)
                    .contains(&l)
                    .then(|| Oklch { l, ..base }.to_rgb())
                    .filter(|c| passes(*c))
            };
            // When both directions pass, keep the one closer to the original.
            match (at(base.l + shift), at(base.l - shift)) {
                (Some(u), Some(d)) => {
                    return Some(if dist(from, u) <= dist(from, d) { u } else { d });
                }
                (Some(c), None) | (None, Some(c)) => return Some(c),
                (None, None) => {}
            }
            i += 1.0;
        }
        None
    }

    fn dist(a: Rgb, b: Rgb) -> f64 {
        let d = |x: u8, y: u8| (f64::from(x) - f64::from(y)).powi(2);
        d(a.r, b.r) + d(a.g, b.g) + d(a.b, b.b)
    }

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

        #[test]
        fn hex_parses_short_long_and_rejects_junk() {
            assert_eq!(Rgb::parse_hex("#0F766E"), Some(Rgb::new(15, 118, 110)));
            assert_eq!(Rgb::parse_hex("fa0"), Some(Rgb::new(255, 170, 0)));
            assert_eq!(Rgb::parse_hex(" #abc "), Some(Rgb::new(170, 187, 204)));
            for bad in ["", "#", "#12", "#12345", "#1234567", "#ggg", "red", "#é12"] {
                assert_eq!(Rgb::parse_hex(bad), None, "{bad}");
            }
            assert_eq!(Rgb::new(11, 110, 102).to_hex(), "#0b6e66");
        }

        #[test]
        fn contrast_matches_wcag_reference_values() {
            let (black, white) = (Rgb::new(0, 0, 0), Rgb::new(255, 255, 255));
            assert!((black.contrast(white) - 21.0).abs() < 1e-9);
            assert!((white.contrast(black) - 21.0).abs() < 1e-9);
            assert!((white.contrast(white) - 1.0).abs() < 1e-9);
            // #777 on white is the well-known 4.48:1.
            let grey = Rgb::parse_hex("#777777").unwrap();
            assert!((grey.contrast(white) - 4.48).abs() < 0.01);
        }

        #[test]
        fn oklch_round_trips_and_matches_reference() {
            let white = Rgb::new(255, 255, 255).to_oklch();
            assert!((white.l - 1.0).abs() < 1e-3 && white.c < 1e-3);
            let red = Rgb::new(255, 0, 0).to_oklch();
            assert!((red.l - 0.628).abs() < 0.002 && (red.c - 0.258).abs() < 0.002);
            assert!((red.h - 29.2).abs() < 0.5);
            for hex in [
                "#0f766e", "#b8532e", "#1e3a5f", "#fff59d", "#808080", "#000000",
            ] {
                let c = Rgb::parse_hex(hex).unwrap();
                assert_eq!(c.to_oklch().to_rgb(), c, "{hex}");
            }
        }

        #[test]
        fn out_of_gamut_chroma_is_reduced_keeping_lightness() {
            let wild = Oklch {
                l: 0.7,
                c: 0.5,
                h: 150.0,
            }
            .to_rgb();
            let back = wild.to_oklch();
            assert!((back.l - 0.7).abs() < 0.02);
            assert!((back.h - 150.0).abs() < 3.0);
        }

        #[test]
        fn mix_endpoints_and_midpoint() {
            let (a, b) = (Rgb::new(0, 0, 0), Rgb::new(255, 255, 255));
            assert_eq!(a.mix(b, 0.0), a);
            assert_eq!(a.mix(b, 1.0), b);
            let mid = a.mix(b, 0.5);
            assert!(mid.r > 90 && mid.r < 130 && mid.r == mid.g && mid.g == mid.b);
        }

        #[test]
        fn nearest_passing_keeps_a_passing_colour_and_moves_a_failing_one() {
            let white = Rgb::new(255, 255, 255);
            let teal = Rgb::parse_hex("#0b6e66").unwrap();
            assert_eq!(
                nearest_passing(teal, 0.3, |c| c.contrast(white) >= 4.5),
                Some(teal)
            );

            let yellow = Rgb::parse_hex("#fff59d").unwrap();
            let fixed = nearest_passing(yellow, 0.7, |c| c.contrast(white) >= 4.5).unwrap();
            assert!(fixed.contrast(white) >= 4.5);
            // Hue is kept.
            assert!((fixed.to_oklch().h - yellow.to_oklch().h).abs() < 8.0);
            // Too small a range finds nothing.
            assert_eq!(
                nearest_passing(yellow, 0.1, |c| c.contrast(white) >= 4.5),
                None
            );
        }

        #[test]
        fn nearest_passing_is_minimal_in_lightness() {
            let white = Rgb::new(255, 255, 255);
            let mid = Rgb::parse_hex("#8a8a8a").unwrap();
            let fixed = nearest_passing(mid, 0.5, |c| c.contrast(white) >= 4.5).unwrap();
            assert!(fixed.contrast(white) >= 4.5);
            // A hair closer to the original lightness no longer passes.
            let (from_l, o) = (mid.to_oklch().l, fixed.to_oklch());
            let closer = if o.l < from_l { o.l + 0.01 } else { o.l - 0.01 };
            assert!(Oklch { l: closer, ..o }.to_rgb().contrast(white) < 4.5);
        }
    }
}