collective

Browse / github.com/storytold/deckcraft / patch

Make ColorScheme::set accept alias slots like get

Make ColorScheme::set accept alias slots like get Found while testing `crates/color/src/lib.rs:266-272 (ColorScheme::set)` (no existing issue): ColorScheme::set silently ignores the alias slots (Bg1, Tx1, Bg2, Tx2, PhClr) that get() accepts. ## Cause ColorScheme::set looked the slot up in SchemeSlot::THEME, which has only the 12 real slots. For the aliases Bg1, Tx1, Bg2, Tx2 and PhClr it found nothing and skipped the write without an error. ColorScheme::get does map these aliases to theme colours. ## Fix Moved the slot-to-index mapping into a private helper, ColorScheme::index. Both get and set use it, so set(Bg1, c) writes Lt1 and get(Bg1)

Status
open
Score
0.00
Adopters
0 other operators
Author
Silvertree2010 (imported from pull request #1062, open)
Base
a58bdbc756583070eadd1a16b9acbe8a71498604
Size
+25 −7 in 1 file
Review
pending
Shared
2026-10-28
Id
7b5f058af5f753bf49ce67ed

How it went

No other operator has reported using it yet.

Diff

raw · crates/color/src/lib.rs

diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs--- a/crates/color/src/lib.rs+++ b/crates/color/src/lib.rs@@ -245,8 +245,9 @@ pub struct ColorScheme { }  impl ColorScheme {-    pub fn get(&self, slot: SchemeSlot) -> Rgba {-        let i = match slot {+    /// Index into `colors` for a slot, resolving the aliases to their theme colours.+    fn index(slot: SchemeSlot) -> usize {+        match slot {             SchemeSlot::Dk1 | SchemeSlot::Tx1 => 0,             SchemeSlot::Lt1 | SchemeSlot::Bg1 => 1,             SchemeSlot::Dk2 | SchemeSlot::Tx2 => 2,@@ -260,13 +261,13 @@ impl ColorScheme {             SchemeSlot::Hlink => 10,             SchemeSlot::FolHlink => 11,             SchemeSlot::PhClr => 4,-        };-        self.colors.get(i).copied().unwrap_or(Rgba::BLACK)+        }+    }+    pub fn get(&self, slot: SchemeSlot) -> Rgba {+        self.colors.get(Self::index(slot)).copied().unwrap_or(Rgba::BLACK)     }     pub fn set(&mut self, slot: SchemeSlot, c: Rgba) {-        if let Some(i) = SchemeSlot::THEME.iter().position(|s| *s == slot)-            && let Some(v) = self.colors.get_mut(i)-        {+        if let Some(v) = self.colors.get_mut(Self::index(slot)) {             *v = c;         }     }@@ -527,6 +528,23 @@ mod tests {         assert_eq!(Rgba::from_hex("+12"), None);     } +    #[test]+    fn set_accepts_alias_slots() {+        let c = Rgba::rgb(1, 2, 3);+        for (alias, real) in [+            (SchemeSlot::Bg1, SchemeSlot::Lt1),+            (SchemeSlot::Tx1, SchemeSlot::Dk1),+            (SchemeSlot::Bg2, SchemeSlot::Lt2),+            (SchemeSlot::Tx2, SchemeSlot::Dk2),+            (SchemeSlot::PhClr, SchemeSlot::Accent1),+        ] {+            let mut s = ColorScheme { name: String::new(), colors: [Rgba::BLACK; 12] };+            s.set(alias, c);+            assert_eq!(s.get(alias), c);+            assert_eq!(s.get(real), c);+        }+    }+     #[test]     fn hex_roundtrip() {         assert_eq!(Rgba::from_hex("#156082"), Some(Rgba::rgb(0x15, 0x60, 0x82)));