From 777631d160deb79288a6a765ce56d8875ede77ee Mon Sep 17 00:00:00 2001 From: Patrick Owen Date: Sun, 23 Jun 2024 14:38:13 -0400 Subject: [PATCH] Store all Material types in an array --- common/src/world.rs | 87 +++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/common/src/world.rs b/common/src/world.rs index b1580c26..98a0f87b 100644 --- a/common/src/world.rs +++ b/common/src/world.rs @@ -50,55 +50,56 @@ pub enum Material { impl Material { pub const COUNT: usize = 40; + + pub const ALL: [Self; Self::COUNT] = [ + Material::Void, + Material::Dirt, + Material::Sand, + Material::Silt, + Material::Clay, + Material::Mud, + Material::SandyLoam, + Material::SiltyLoam, + Material::ClayLoam, + Material::RedSand, + Material::Limestone, + Material::Shale, + Material::Dolomite, + Material::Sandstone, + Material::RedSandstone, + Material::Marble, + Material::Slate, + Material::Granite, + Material::Diorite, + Material::Andesite, + Material::Gabbro, + Material::Basalt, + Material::Olivine, + Material::Water, + Material::Lava, + Material::Wood, + Material::Leaves, + Material::WoodPlanks, + Material::GreyBrick, + Material::WhiteBrick, + Material::Ice, + Material::IceSlush, + Material::Gravel, + Material::Snow, + Material::CoarseGrass, + Material::TanGrass, + Material::LushGrass, + Material::MudGrass, + Material::Grass, + Material::CaveGrass, + ]; } impl TryFrom for Material { type Error = (); fn try_from(value: u16) -> Result { - Ok(match value { - 0 => Material::Void, - 1 => Material::Dirt, - 2 => Material::Sand, - 3 => Material::Silt, - 4 => Material::Clay, - 5 => Material::Mud, - 6 => Material::SandyLoam, - 7 => Material::SiltyLoam, - 8 => Material::ClayLoam, - 9 => Material::RedSand, - 10 => Material::Limestone, - 11 => Material::Shale, - 12 => Material::Dolomite, - 13 => Material::Sandstone, - 14 => Material::RedSandstone, - 15 => Material::Marble, - 16 => Material::Slate, - 17 => Material::Granite, - 18 => Material::Diorite, - 19 => Material::Andesite, - 20 => Material::Gabbro, - 21 => Material::Basalt, - 22 => Material::Olivine, - 23 => Material::Water, - 24 => Material::Lava, - 25 => Material::Wood, - 26 => Material::Leaves, - 27 => Material::WoodPlanks, - 28 => Material::GreyBrick, - 29 => Material::WhiteBrick, - 30 => Material::Ice, - 31 => Material::IceSlush, - 32 => Material::Gravel, - 33 => Material::Snow, - 34 => Material::CoarseGrass, - 35 => Material::TanGrass, - 36 => Material::LushGrass, - 37 => Material::MudGrass, - 38 => Material::Grass, - 39 => Material::CaveGrass, - _ => Err(())?, - }) + Material::ALL.get(value as usize).ok_or(()).copied() } }