Merge branch 'danacus/enumstring' into 'master'

Fixing #1169 - use strum macros for BlockKind conversion

Closes #1169

See merge request veloren/veloren!2503
This commit is contained in:
Imbris 2021-06-26 16:52:26 +00:00
commit c214f2f940
3 changed files with 9 additions and 25 deletions

View File

@ -218,9 +218,8 @@ lazy_static! {
buff_pack
};
static ref BLOCK_KINDS: Vec<String> = terrain::block::BLOCK_KINDS
.keys()
.cloned()
static ref BLOCK_KINDS: Vec<String> = terrain::block::BlockKind::iter()
.map(|bk| bk.to_string())
.collect();
static ref SPRITE_KINDS: Vec<String> = terrain::sprite::SPRITE_KINDS

View File

@ -3,13 +3,11 @@ use crate::{
comp::{fluid_dynamics::LiquidKind, tool::ToolKind},
make_case_elim,
};
use enum_iterator::IntoEnumIterator;
use hashbrown::HashMap;
use lazy_static::lazy_static;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, fmt, ops::Deref};
use std::ops::Deref;
use strum_macros::{EnumIter, EnumString, ToString};
use vek::*;
make_case_elim!(
@ -23,8 +21,10 @@ make_case_elim!(
PartialEq,
Serialize,
Deserialize,
IntoEnumIterator,
FromPrimitive,
EnumString,
EnumIter,
ToString,
)]
#[repr(u8)]
pub enum BlockKind {
@ -88,22 +88,6 @@ impl BlockKind {
pub const fn has_color(&self) -> bool { self.is_filled() }
}
impl fmt::Display for BlockKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) }
}
lazy_static! {
pub static ref BLOCK_KINDS: HashMap<String, BlockKind> = BlockKind::into_enum_iter()
.map(|bk| (bk.to_string(), bk))
.collect();
}
impl<'a> TryFrom<&'a str> for BlockKind {
type Error = ();
fn try_from(s: &'a str) -> Result<Self, Self::Error> { BLOCK_KINDS.get(s).copied().ok_or(()) }
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Block {
kind: BlockKind,

View File

@ -43,6 +43,7 @@ use hashbrown::{HashMap, HashSet};
use humantime::Duration as HumanDuration;
use rand::Rng;
use specs::{storage::StorageEntry, Builder, Entity as EcsEntity, Join, WorldExt};
use std::str::FromStr;
use vek::*;
use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement};
use world::util::Sampler;
@ -515,7 +516,7 @@ fn handle_make_block(
action: &ChatCommand,
) -> CmdResult<()> {
if let Some(block_name) = scan_fmt_some!(&args, &action.arg_fmt(), String) {
if let Ok(bk) = BlockKind::try_from(block_name.as_str()) {
if let Ok(bk) = BlockKind::from_str(block_name.as_str()) {
let pos = position(server, target, "target")?;
server.state.set_block(
pos.0.map(|e| e.floor() as i32),