mindustry logic execution, map- and schematic- parsing and rendering
-rw-r--r--src/block/mod.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/block/mod.rs b/src/block/mod.rs
index e9f9a23..5f03b85 100644
--- a/src/block/mod.rs
+++ b/src/block/mod.rs
@@ -1,4 +1,6 @@
use std::borrow::Cow;
+use std::collections::HashMap;
+use std::collections::hash_map::Entry;
pub trait BlockLogic
{
@@ -72,3 +74,47 @@ impl From<Rotation> for u8
}
}
}
+
+pub struct BlockRegistry<'l>
+{
+ blocks: HashMap<&'l str, &'l Block>,
+}
+
+impl<'l> BlockRegistry<'l>
+{
+ pub fn new() -> Self
+ {
+ Self{blocks: HashMap::new()}
+ }
+
+ pub fn register(&mut self, block: &'l Block) -> Result<(), &'l Block>
+ {
+ let key = match block.name
+ {
+ Cow::Borrowed(r) => r,
+ Cow::Owned(ref s) => s,
+ };
+ match self.blocks.entry(key)
+ {
+ Entry::Occupied(e) => Err(e.get()),
+ Entry::Vacant(e) =>
+ {
+ e.insert(block);
+ Ok(())
+ },
+ }
+ }
+
+ pub fn get(&self, name: &str) -> Option<&'l Block>
+ {
+ self.blocks.get(name).map(|&r| r)
+ }
+}
+
+impl<'l> AsRef<HashMap<&'l str, &'l Block>> for BlockRegistry<'l>
+{
+ fn as_ref(&self) -> &HashMap<&'l str, &'l Block>
+ {
+ &self.blocks
+ }
+}