mdbook_journal/journal/topic/
map.rs

1use crate::prelude::*;
2
3/// Journal Topic Map
4///
5/// Simple collection of topics indexed by name
6///
7#[derive(Debug, Default)]
8pub struct TopicMap {
9    map: BTreeMap<TopicName, Topic>,
10}
11
12impl TopicMap {
13    pub(crate) fn insert(mut self, topic: Topic) -> Result<Self> {
14        if self.map.contains_key(&topic.name) {
15            bail!("Topic with key {} already taken!", &topic.name);
16        }
17        self.map.insert(topic.name.clone(), topic);
18        Ok(self)
19    }
20
21    pub(crate) fn iter(&self) -> impl Iterator<Item = &Topic> {
22        self.map.values()
23    }
24
25    pub(crate) fn find<S>(&self, name: &S) -> Option<&Topic>
26    where
27        S: AsRef<str>,
28    {
29        self.map.get(name.as_ref())
30    }
31
32    pub(crate) fn find_mut<S>(&mut self, name: &S) -> Option<&mut Topic>
33    where
34        S: AsRef<str>,
35    {
36        self.map.get_mut(name.as_ref())
37    }
38}