1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 10:44:07 +02:00

Merge pull request #17 from krakow10/parse_prop

Misc Parsing Changes
This commit is contained in:
Robin Appelman 2025-02-22 19:10:53 +01:00 committed by GitHub
commit 07a9fe5f71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 12 deletions

View file

@ -123,7 +123,7 @@ impl<'a> RawEntity<'a> {
&self, &self,
key: &'static str, key: &'static str,
) -> Option<Result<T, EntityParseError>> { ) -> Option<Result<T, EntityParseError>> {
Some(T::parse(self.prop(key)?)) self.prop(key).map(T::parse)
} }
pub fn parse<E: Deserialize<'a>>(&self) -> Result<E, VdfError> { pub fn parse<E: Deserialize<'a>>(&self) -> Result<E, VdfError> {
@ -162,7 +162,7 @@ where
[T; N]: Default, [T; N]: Default,
{ {
fn parse(raw: &'_ str) -> Result<Self, EntityParseError> { fn parse(raw: &'_ str) -> Result<Self, EntityParseError> {
let mut values = raw.split(' ').map(T::from_str); let mut values = raw.split_whitespace().map(T::from_str);
let mut result = <[T; N]>::default(); let mut result = <[T; N]>::default();
for item in result.iter_mut() { for item in result.iter_mut() {
*item = values.next().ok_or(EntityParseError::ElementCount)??; *item = values.next().ok_or(EntityParseError::ElementCount)??;
@ -200,7 +200,7 @@ impl FromStr for Color {
type Err = EntityParseError; type Err = EntityParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut floats = s.split(' ').map(u8::from_str); let mut floats = s.split_whitespace().map(u8::from_str);
let r = floats.next().ok_or(EntityParseError::ElementCount)??; let r = floats.next().ok_or(EntityParseError::ElementCount)??;
let g = floats.next().ok_or(EntityParseError::ElementCount)??; let g = floats.next().ok_or(EntityParseError::ElementCount)??;
let b = floats.next().ok_or(EntityParseError::ElementCount)??; let b = floats.next().ok_or(EntityParseError::ElementCount)??;
@ -217,13 +217,9 @@ impl<'de> Deserialize<'de> for Color {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let str = <&str>::deserialize(deserializer)?; let str = <&str>::deserialize(deserializer)?;
let colors = <[u8; 3]>::parse(str) let [r, g, b] = <[u8; 3]>::parse(str)
.map_err(|_| D::Error::invalid_value(Unexpected::Other(str), &"a list of 3 numbers"))?; .map_err(|_| D::Error::invalid_value(Unexpected::Other(str), &"a list of 3 numbers"))?;
Ok(Color { Ok(Color { r, g, b })
r: colors[0],
g: colors[1],
b: colors[2],
})
} }
} }
@ -239,7 +235,7 @@ impl FromStr for LightColor {
type Err = EntityParseError; type Err = EntityParseError;
fn from_str(str: &str) -> Result<Self, Self::Err> { fn from_str(str: &str) -> Result<Self, Self::Err> {
let mut values = str.split(' '); let mut values = str.split_whitespace();
let r = values let r = values
.next() .next()
.ok_or(EntityParseError::ElementCount)? .ok_or(EntityParseError::ElementCount)?

View file

@ -558,7 +558,7 @@ impl FromStr for Angles {
type Err = EntityParseError; type Err = EntityParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut floats = s.split(' ').map(f32::from_str); let mut floats = s.split_whitespace().map(f32::from_str);
let pitch = floats.next().ok_or(EntityParseError::ElementCount)??; let pitch = floats.next().ok_or(EntityParseError::ElementCount)??;
let yaw = floats.next().ok_or(EntityParseError::ElementCount)??; let yaw = floats.next().ok_or(EntityParseError::ElementCount)??;
let roll = floats.next().ok_or(EntityParseError::ElementCount)??; let roll = floats.next().ok_or(EntityParseError::ElementCount)??;

View file

@ -99,7 +99,7 @@ impl FromStr for Vector {
type Err = EntityParseError; type Err = EntityParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut floats = s.split(' ').map(f32::from_str); let mut floats = s.split_whitespace().map(f32::from_str);
let x = floats.next().ok_or(EntityParseError::ElementCount)??; let x = floats.next().ok_or(EntityParseError::ElementCount)??;
let y = floats.next().ok_or(EntityParseError::ElementCount)??; let y = floats.next().ok_or(EntityParseError::ElementCount)??;
let z = floats.next().ok_or(EntityParseError::ElementCount)??; let z = floats.next().ok_or(EntityParseError::ElementCount)??;