rune/compile/
names.rs

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#[cfg(test)]
mod tests;

use core::mem::replace;

use crate::alloc;
use crate::alloc::btree_map::{self, BTreeMap};
use crate::alloc::prelude::*;
use crate::item::{Component, ComponentRef, IntoComponent};

/// A tree of names.
#[derive(Default, Debug)]
pub struct Names {
    root: Node,
}

impl TryClone for Names {
    fn try_clone(&self) -> alloc::Result<Self> {
        Ok(Self {
            root: self.root.try_clone()?,
        })
    }
}

impl Names {
    /// Insert the given item as an import.
    pub(crate) fn insert<I>(&mut self, iter: I) -> alloc::Result<bool>
    where
        I: IntoIterator,
        I::Item: IntoComponent,
    {
        let mut current = &mut self.root;

        for c in iter {
            current = current
                .children
                .entry(c.into_component()?)
                .or_try_default()?;
        }

        Ok(replace(&mut current.term, true))
    }

    /// Test if the given import exists.
    pub(crate) fn contains<I>(&self, iter: I) -> alloc::Result<bool>
    where
        I: IntoIterator,
        I::Item: IntoComponent,
    {
        Ok(self.find_node(iter)?.map(|n| n.term).unwrap_or_default())
    }

    /// Test if we contain the given prefix.
    pub(crate) fn contains_prefix<I>(&self, iter: I) -> alloc::Result<bool>
    where
        I: IntoIterator,
        I::Item: IntoComponent,
    {
        Ok(self.find_node(iter)?.is_some())
    }

    /// Iterate over all known components immediately under the specified `iter`
    /// path.
    pub(crate) fn iter_components<'a, I>(
        &'a self,
        iter: I,
    ) -> alloc::Result<impl Iterator<Item = ComponentRef<'a>> + 'a>
    where
        I: 'a + IntoIterator,
        I::Item: IntoComponent,
    {
        let iter = if let Some(current) = self.find_node(iter)? {
            current.children.keys()
        } else {
            btree_map::Keys::default()
        };

        Ok(iter.map(|c| c.as_component_ref()))
    }

    /// Find the node corresponding to the given path.
    fn find_node<I>(&self, iter: I) -> alloc::Result<Option<&Node>>
    where
        I: IntoIterator,
        I::Item: IntoComponent,
    {
        let mut current = &self.root;

        for c in iter {
            let c = c.into_component()?;

            let Some(c) = current.children.get(&c) else {
                return Ok(None);
            };

            current = c;
        }

        Ok(Some(current))
    }
}

#[derive(Default, Debug)]
struct Node {
    /// If the node is terminating.
    term: bool,
    /// The children of this node.
    children: BTreeMap<Component, Node>,
}

impl TryClone for Node {
    fn try_clone(&self) -> alloc::Result<Self> {
        Ok(Self {
            term: self.term,
            children: self.children.try_clone()?,
        })
    }
}