mindustry logic execution, map- and schematic- parsing and rendering
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::any::type_name;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;

pub trait RegistryEntry {
    fn get_name(&self) -> &str;
}

pub struct Registry<'l, E: RegistryEntry + fmt::Debug + 'static> {
    by_name: HashMap<&'l str, &'l E>,
}

impl<'l, E: RegistryEntry + fmt::Debug + 'static> Default for Registry<'l, E> {
    fn default() -> Self {
        Self {
            by_name: HashMap::new(),
        }
    }
}

impl<'l, E: RegistryEntry + fmt::Debug + 'static> Registry<'l, E> {
    pub fn register(&mut self, val: &'l E) -> Result<&'l E, RegisterError<'l, E>> {
        match self.by_name.entry(val.get_name()) {
            Entry::Occupied(e) => Err(RegisterError(e.get())),
            Entry::Vacant(e) => Ok(e.insert(val)),
        }
    }

    #[must_use]
    pub fn get(&self, name: &str) -> Option<&'l E> {
        self.by_name.get(name).copied()
    }
}

#[derive(Clone, Copy, Debug)]
pub struct RegisterError<'l, E: RegistryEntry + fmt::Debug + 'static>(pub &'l E);

impl<'l, E: RegistryEntry + fmt::Debug + 'static> fmt::Display for RegisterError<'l, E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {:?} already exists",
            type_name::<E>(),
            self.0.get_name()
        )
    }
}

impl<'l, E: RegistryEntry + fmt::Debug + 'static> Error for RegisterError<'l, E> {}