mindustry logic execution, map- and schematic- parsing and rendering
another cool macro
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/content.rs | 11 | ||||
| -rw-r--r-- | src/item/mod.rs | 31 | ||||
| -rw-r--r-- | src/modifier.rs | 3 | ||||
| -rw-r--r-- | strconv/Cargo.toml | 12 | ||||
| -rw-r--r-- | strconv/src/lib.rs | 29 |
6 files changed, 52 insertions, 35 deletions
@@ -11,6 +11,7 @@ license = "GPL-3.0" flate2 = { version = "1.0", features = ["zlib"], default-features = false } base64 = "0.21.2" paste = "1.0.12" +strconv = { path = "strconv" } [[bin]] name = "plandustry" diff --git a/src/content.rs b/src/content.rs index 3172096..3e7fd0b 100644 --- a/src/content.rs +++ b/src/content.rs @@ -65,13 +65,20 @@ macro_rules! content_enum { } } - impl std::fmt::Display for $error - { + impl std::fmt::Display for $error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "no content of type {} for value {}", stringify!($ctype), self.0) } } + impl std::fmt::Display for $tname { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + $(Self::[<$val:camel>] => f.write_str(strconv::kebab2title!($val)),)* + } + } + } + impl std::error::Error for $error {} } }; diff --git a/src/item/mod.rs b/src/item/mod.rs index a08c71a..e7a6f06 100644 --- a/src/item/mod.rs +++ b/src/item/mod.rs @@ -1,5 +1,3 @@ -use std::fmt; - use crate::content::content_enum; pub mod storage; @@ -30,32 +28,3 @@ content_enum! { "dormant-cyst", } } - -impl fmt::Display for Type { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Copper => f.write_str("Copper"), - Self::Lead => f.write_str("Lead"), - Self::Metaglass => f.write_str("Metaglass"), - Self::Graphite => f.write_str("Graphite"), - Self::Sand => f.write_str("Sand"), - Self::Coal => f.write_str("Coal"), - Self::Titanium => f.write_str("Titanium"), - Self::Thorium => f.write_str("Thorium"), - Self::Scrap => f.write_str("Scrap"), - Self::Silicon => f.write_str("Silicon"), - Self::Plastanium => f.write_str("Plastanium"), - Self::PhaseFabric => f.write_str("Phase Fabric"), - Self::SurgeAlloy => f.write_str("Surge Alloy"), - Self::SporePod => f.write_str("Spore Pod"), - Self::BlastCompound => f.write_str("Blast Compound"), - Self::Pyratite => f.write_str("Pyratite"), - Self::Beryllium => f.write_str("Beryllium"), - Self::Tungsten => f.write_str("Tungsten"), - Self::Oxide => f.write_str("Oxide"), - Self::Carbide => f.write_str("Carbide"), - Self::FissileMatter => f.write_str("Fissile Matter"), - Self::DormantCyst => f.write_str("Dormant Cyst"), - } - } -} diff --git a/src/modifier.rs b/src/modifier.rs index 498b98c..7e86d47 100644 --- a/src/modifier.rs +++ b/src/modifier.rs @@ -1,8 +1,7 @@ use crate::content::content_enum; content_enum! { - pub enum Type / Modifier for u16 | TryFromU16Error - { + pub enum Type / Modifier for u16 | TryFromU16Error { "none", "burning", "freezing", diff --git a/strconv/Cargo.toml b/strconv/Cargo.toml new file mode 100644 index 0000000..29b6981 --- /dev/null +++ b/strconv/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "strconv" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +syn = "2.0.15" diff --git a/strconv/src/lib.rs b/strconv/src/lib.rs new file mode 100644 index 0000000..4b9d626 --- /dev/null +++ b/strconv/src/lib.rs @@ -0,0 +1,29 @@ +use proc_macro::TokenStream; +use syn::{parse_macro_input, LitStr}; + +#[proc_macro] +pub fn kebab2title(input: TokenStream) -> TokenStream { + let input_str = parse_macro_input!(input as LitStr).value(); + + let converted = kebab2title_impl(&input_str); + format!("\"{converted}\"").parse().unwrap() +} + +fn kebab2title_impl(data: &str) -> String { + let mut result = String::with_capacity(data.len()); + let mut capitalize_next = true; + + for c in data.chars() { + if c == '-' { + result.push(' '); + capitalize_next = true; + } else if capitalize_next { + result.push(c.to_ascii_uppercase()); + capitalize_next = false; + } else { + result.push(c); + } + } + + result +} |