diff --git a/config/src/font.rs b/config/src/font.rs index c4ded0649..f7134c721 100644 --- a/config/src/font.rs +++ b/config/src/font.rs @@ -152,17 +152,20 @@ impl FromDynamic for FontWeight { Value::String(s) => { Ok(Self::from_str(s).ok_or_else(|| format!("invalid font weight {}", s))?) } - Value::U64(value) => { - if *value > 0 && *value <= (u16::MAX as u64) { - Ok(FontWeight(*value as u16)) + other => { + if let Some(value) = value.coerce_unsigned() { + if value > 0 && value <= (u16::MAX as u64) { + Ok(FontWeight(value as u16)) + } else { + Err(format!("invalid font weight {}", value).into()) + } } else { - Err(format!("invalid font weight {}", value).into()) + Err(wezterm_dynamic::Error::NoConversion { + source_type: other.variant_name().to_string(), + dest_type: "FontWeight", + }) } } - other => Err(wezterm_dynamic::Error::NoConversion { - source_type: other.variant_name().to_string(), - dest_type: "FontWeight", - }), } } } diff --git a/wezterm-dynamic/src/value.rs b/wezterm-dynamic/src/value.rs index 509e22d7c..a675dd63d 100644 --- a/wezterm-dynamic/src/value.rs +++ b/wezterm-dynamic/src/value.rs @@ -52,4 +52,39 @@ impl Value { Self::F64(_) => "F64", } } + + pub fn coerce_unsigned(&self) -> Option { + match self { + Self::U64(u) => Some(*u), + Self::I64(i) => (*i).try_into().ok(), + Self::F64(OrderedFloat(f)) + if f.fract() == 0.0 && *f >= u64::MIN as f64 && *f <= u64::MAX as f64 => + { + Some(*f as u64) + } + _ => None, + } + } + + pub fn coerce_signed(&self) -> Option { + match self { + Self::I64(u) => Some(*u), + Self::U64(i) => (*i).try_into().ok(), + Self::F64(OrderedFloat(f)) + if f.fract() == 0.0 && *f >= i64::MIN as f64 && *f <= i64::MAX as f64 => + { + Some(*f as i64) + } + _ => None, + } + } + + pub fn coerce_float(&self) -> Option { + match self { + Self::I64(u) => Some(*u as f64), + Self::U64(i) => Some(*i as f64), + Self::F64(OrderedFloat(f)) => Some(*f), + _ => None, + } + } }