EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Settings: handle invalid Unicode label colours gracefully
storybored379202417dab3fa
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex e56867d66..a0d305e90 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -262,7 +262,7 @@ fn parse_hex(s: &str) -> Option<[u8; 3]> {if h.len() != 6 {return None;}- let p = |i: usize| u8::from_str_radix(&h[i..i + 2], 16).ok();+ let p = |i: usize| u8::from_str_radix(h.get(i..i + 2)?, 16).ok();Some([p(0)?, p(2)?, p(4)?])}diff --git a/crates/engine/src/tests_settings.rs b/crates/engine/src/tests_settings.rsindex 8c2fcac52..4afa08fce 100644--- a/crates/engine/src/tests_settings.rs+++ b/crates/engine/src/tests_settings.rs@@ -508,3 +508,16 @@ fn interface_language_is_validated_persisted_and_backward_compatible() {assert_eq!(Prefs::from_json(r#"{"general":{"undoLevels":17}}"#).general.language, "en");assert_eq!(Prefs::from_json(r#"{"general":{"language":"unknown"}}"#).general.language, "en");}++#[test]+fn unicode_label_colors_fall_back_without_panicking() {+ use effectcraft_color::Label;+ let mut s = Session::default();+ let default = s.prefs.label_rgb(Label::Red);+ for color in ["#€abc", "#a€bc", "#abc€"] {+ s.execute("prefs.set", json!({"key": "labels.0.color", "value": color})).unwrap();+ assert_eq!(s.prefs.label_rgb(Label::Red), default);+ }+ s.execute("prefs.set", json!({"key": "labels.0.color", "value": "#aBcD09"})).unwrap();+ assert_eq!(s.prefs.label_rgb(Label::Red), [0xab, 0xcd, 9]);+}