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
52
53
54
55
56
57
58
59
60
61
62
63
64
use super::{CommandAlias, Event};
use crate::storage::{DataStore, Repository};
use crate::world;
use rand::prelude::*;
use std::collections::HashSet;
use std::fmt;

pub struct AppMeta {
    pub command_aliases: HashSet<CommandAlias>,
    pub demographics: world::Demographics,
    pub event_dispatcher: &'static dyn Fn(Event),
    pub rng: SmallRng,
    pub repository: Repository,
}

impl AppMeta {
    pub fn new<F: Fn(Event)>(
        data_store: impl DataStore + 'static,
        event_dispatcher: &'static F,
    ) -> Self {
        Self {
            command_aliases: HashSet::default(),
            demographics: world::Demographics::default(),
            event_dispatcher,
            repository: Repository::new(data_store),
            rng: SmallRng::from_entropy(),
        }
    }
}

impl fmt::Debug for AppMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "AppMeta {{ command_aliases: {:?}, demographics: {:?}, repository: {:?} }}",
            self.command_aliases, self.demographics, self.repository,
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::storage::NullDataStore;
    use crate::world::Demographics;
    use std::collections::HashMap;

    #[test]
    fn debug_test() {
        let mut app_meta = app_meta();
        app_meta.demographics = Demographics::new(HashMap::new());

        assert_eq!(
            "AppMeta { command_aliases: {}, demographics: Demographics { groups: GroupMapWrapper({}) }, repository: Repository { data_store_enabled: false, recent: [] } }",
            format!("{:?}", app_meta),
        );
    }

    fn event_dispatcher(_event: Event) {}

    fn app_meta() -> AppMeta {
        AppMeta::new(NullDataStore, &event_dispatcher)
    }
}