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
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use super::base::SourceTrait;
#[cfg(feature = "look-ahead")]
use super::look_ahead_yes::PrebuiltData;
use crate::error::*;

pub struct SourceRoot<V, Imp>
where
    Imp: SourceTrait,
{
    #[cfg(feature = "look-ahead")]
    pub(super) prebuilt: PrebuiltData<Imp::PrebuiltCode>,

    pub(super) data: BTreeMap<String, V>,
    pub(super) root: PathBuf,
    pub(super) imp: Imp,
}

impl<V, Imp> SourceRoot<V, Imp>
where
    Imp: SourceTrait,
{
    pub(super) fn new(root: &Path, imp: Imp) -> Result<Self> {
        Ok(Self {
            #[cfg(feature = "look-ahead")]
            prebuilt: PrebuiltData::load(root, &imp),
            data: BTreeMap::new(),
            root: root.to_owned(),
            imp,
        })
    }
}

impl<V, Imp> SourceRoot<V, Imp>
where
    Imp: SourceTrait<Code = V>,
{
    pub fn find<'a, K: AsRef<str>>(&'a mut self, key: K) -> Result<Option<&'a V>> {
        let key = key.as_ref();
        self.ensure_compiled(key)?;
        Ok(self.data.get(key))
    }

    pub fn find_mut<K: AsRef<str>>(&mut self, key: K) -> Result<Option<&mut V>> {
        let key = key.as_ref();
        self.ensure_compiled(key)?;
        Ok(self.data.get_mut(key))
    }

    fn ensure_compiled(&mut self, key: &str) -> Result<()> {
        if self.data.contains_key(key) {
            Ok(())
        } else {
            self.compile(key)
        }
    }
}