1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-25 21:07:39 +03:00

config: fix specifying numeric font weights

add helpers to Value coerce to different numeric types, and use
one of those in the FontWeight conversion.

refs: #2012
This commit is contained in:
Wez Furlong 2022-05-20 06:32:45 -07:00
parent 55767c69b1
commit d6063e180a
2 changed files with 46 additions and 8 deletions

View File

@ -152,17 +152,20 @@ impl FromDynamic for FontWeight {
Value::String(s) => { Value::String(s) => {
Ok(Self::from_str(s).ok_or_else(|| format!("invalid font weight {}", s))?) Ok(Self::from_str(s).ok_or_else(|| format!("invalid font weight {}", s))?)
} }
Value::U64(value) => { other => {
if *value > 0 && *value <= (u16::MAX as u64) { if let Some(value) = value.coerce_unsigned() {
Ok(FontWeight(*value as u16)) if value > 0 && value <= (u16::MAX as u64) {
Ok(FontWeight(value as u16))
} else {
Err(format!("invalid font weight {}", value).into())
}
} else { } 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",
}),
} }
} }
} }

View File

@ -52,4 +52,39 @@ impl Value {
Self::F64(_) => "F64", Self::F64(_) => "F64",
} }
} }
pub fn coerce_unsigned(&self) -> Option<u64> {
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<i64> {
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<f64> {
match self {
Self::I64(u) => Some(*u as f64),
Self::U64(i) => Some(*i as f64),
Self::F64(OrderedFloat(f)) => Some(*f),
_ => None,
}
}
} }