dedukti_parse/
symb.rs

1//! Symbols consisting of a relative module path and a name.
2
3use alloc::vec::Vec;
4use core::fmt::{self, Display};
5
6/// Symbol consisting of a relative module path and a name.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct Symb<S> {
9    /// module path (`["a", "b"]` for symbol `"a.b.c"`)
10    pub path: Vec<S>,
11    /// symbol name (`"c"` for symbol `"a.b.c"`)
12    pub name: S,
13}
14
15impl<S> Symb<S> {
16    /// Create a new symbol with an empty module path.
17    pub fn new(name: S) -> Self {
18        let path = Vec::new();
19        Self { path, name }
20    }
21
22    /// Map over all parts of the symbol.
23    pub fn map<T>(self, f: impl Fn(S) -> T) -> Symb<T> {
24        Symb {
25            path: self.path.into_iter().map(&f).collect(),
26            name: f(self.name),
27        }
28    }
29
30    /// Replace the symbol name with the given name, and
31    /// move the previous symbol name at the end of the module path.
32    pub fn push(&mut self, name: S) {
33        self.path.push(core::mem::replace(&mut self.name, name));
34    }
35}
36
37impl<S: Display> Display for Symb<S> {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        self.path.iter().try_for_each(|p| write!(f, "{}.", p))?;
40        self.name.fmt(f)
41    }
42}