1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

impl FromStrProp for Color

This commit is contained in:
Quaternions 2025-02-13 16:01:41 -08:00
commit a32b9f060e

View file

@ -133,6 +133,7 @@ impl FromStrProp for u16 {}
impl FromStrProp for f32 {}
impl FromStrProp for u32 {}
impl FromStrProp for i32 {}
impl FromStrProp for Color {}
impl FromStrProp for Vector {}
impl<T: FromStrProp> EntityProp<'_> for T
@ -184,6 +185,21 @@ pub struct Color {
pub b: u8,
}
impl FromStr for Color {
type Err = EntityParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut floats = s.split(' ').map(u8::from_str);
let r = floats.next().ok_or(EntityParseError::ElementCount)??;
let g = floats.next().ok_or(EntityParseError::ElementCount)??;
let b = floats.next().ok_or(EntityParseError::ElementCount)??;
if floats.next().is_some() {
return Err(EntityParseError::ElementCount);
}
Ok(Self { r, g, b })
}
}
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where