Skip to content

Commit

Permalink
Implement FromStr for TerrainMaterials (#354)
Browse files Browse the repository at this point in the history
Closes #353.

This also modifies `MaterialColorsError` to be a generalized error for
both `MaterialColors` and `TerrainColors`. This is technically a
breaking change because the enum is publicly exposed, but I don't think
it will seriously impact anyone beyond adding a new variant to the enum.
  • Loading branch information
Dekkonot authored Sep 11, 2023
1 parent 37e3a83 commit b9d6a42
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions rbx_types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased Changes

* Implement `FromStr` for `TerrainMaterials`. ([#354])

[#354]: https://github.com/rojo-rbx/rbx-dom/pull/354

## 1.6.0 (2023-08-09)
* Added support for `UniqueId` values. ([#271])
* Changed `BinaryString`'s non-human readable serde implementation to be identical to `Vec<u8>`. ([#276])
Expand Down
34 changes: 32 additions & 2 deletions rbx_types/src/material_colors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, str::FromStr};

use thiserror::Error;

Expand Down Expand Up @@ -88,14 +88,18 @@ where
}
}

/// An error that can occur when deserializing MaterialColors.
/// An error that can occur when deserializing or working with MaterialColors and TerrainMaterials.
#[derive(Debug, Error)]
pub enum MaterialColorsError {
/// The `MaterialColors` blob was the wrong number of bytes.
#[error(
"MaterialColors blob was the wrong length (expected it to be 69 bytes, it was {0} bytes)"
)]
WrongLength(usize),
/// The argument provided to `from_str` did not correspond to a known
/// TerrainMaterial.
#[error("cannot convert `{0}` into TerrainMaterial")]
UnknownMaterial(String),
}

/// Constructs an enum named `TerrainMaterials` for all values contained in
Expand Down Expand Up @@ -136,6 +140,18 @@ macro_rules! material_colors {
}
}
}

impl FromStr for TerrainMaterials {
type Err = CrateError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {$(
stringify!($name) => Ok(Self::$name),
)*
_ => Err(MaterialColorsError::UnknownMaterial(s.to_string()).into()),
}
}
}
};
}

Expand Down Expand Up @@ -265,4 +281,18 @@ mod test {

assert_eq!(blob, "AAAAAAAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/");
}

#[test]
fn from_str_materials() {
assert!(TerrainMaterials::from_str("Grass").is_ok());
assert!(TerrainMaterials::from_str("Concrete").is_ok());
assert!(TerrainMaterials::from_str("Rock").is_ok());
assert!(TerrainMaterials::from_str("Asphalt").is_ok());
assert!(TerrainMaterials::from_str("Salt").is_ok());
assert!(TerrainMaterials::from_str("Pavement").is_ok());

assert!(TerrainMaterials::from_str("A name I am certain Roblox will never add").is_err());
// `from_str` is case-sensitive
assert!(TerrainMaterials::from_str("gRaSs").is_err());
}
}

0 comments on commit b9d6a42

Please sign in to comment.