Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Resolve the "ft" unit suffix to Feet in Unit::named

NetterSilverf2601137ab0d
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index 3eeb13f7a..1e50fabe7 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -274,7 +274,12 @@ impl Unit {
pub fn named(name: &str) -> Option<Unit> {
let letters = |s: &'static str| s.chars().filter(char::is_ascii_alphanumeric).map(|c| c.to_ascii_lowercase());
let given = || name.chars().filter(char::is_ascii_alphanumeric).map(|c| c.to_ascii_lowercase());
- Unit::ALL.into_iter().find(|u| letters(u.label()).eq(given()) || u.suffix().chars().eq(given()))
+ // Feet and Feet & Inches share the suffix `ft`: it names the plain unit, so Feet & Inches
+ // is left to its label and key.
+ Unit::ALL
+ .into_iter()
+ .find(|u| letters(u.label()).eq(given()))
+ .or_else(|| Unit::ALL.into_iter().filter(|u| *u != Unit::FeetInches).find(|u| u.suffix().chars().eq(given())))
}
/// A point value as the canvas measurement labels show it: two decimals and the suffix
/// (`12.50 mm`).
diff --git a/crates/doc/src/tests_units.rs b/crates/doc/src/tests_units.rs
index b09ed5a50..a5dbe9fb3 100644
--- a/crates/doc/src/tests_units.rs
+++ b/crates/doc/src/tests_units.rs
@@ -98,6 +98,13 @@ fn units_are_named_by_label_key_or_suffix() {
assert_eq!(Unit::named(""), None);
}
+#[test]
+fn a_shared_suffix_names_the_plain_unit() {
+ assert_eq!(Unit::named(Unit::Feet.suffix()), Some(Unit::Feet));
+ assert_eq!(Unit::named("FT"), Some(Unit::Feet));
+ assert_eq!(Unit::named(Unit::FeetInches.label()), Some(Unit::FeetInches));
+}
+
#[test]
fn readouts_show_two_decimals() {
assert_eq!(Unit::Points.readout(15.0), "15.00 pt");