Skip to main content

alux_shape_morph/
prefixed.rs

1//! Every name in a shape, under a word of its own.
2
3use crate::delegate::{delegate_members, delegate_shape_except_named};
4use alux_shape::{ShapeAlg, Words};
5
6/// A shape whose names all begin with one word.
7///
8/// What keeps two surfaces' declarations apart where both are emitted into one place, without either
9/// surface stating anything about the other.
10#[derive(Debug, Clone, Copy)]
11pub struct Prefixed<A> {
12    inner: A,
13    word: &'static str,
14}
15
16impl<A> Prefixed<A> {
17    /// Puts `word` before every name this shape states.
18    pub fn new(inner: A, word: &'static str) -> Self {
19        Self { inner, word }
20    }
21
22    /// The algebra beneath this transformation.
23    fn inner(&self) -> &A {
24        &self.inner
25    }
26}
27
28impl<A> Prefixed<A>
29where
30    A: ShapeAlg,
31{
32    /// States the name this transformation answers under.
33    fn rename(&self, words: Words<'_>, body: A::Ty) -> A::Ty {
34        let mut named = vec![self.word];
35        named.extend_from_slice(words);
36
37        self.inner().named(&named, body)
38    }
39}
40
41delegate_shape_except_named!(Prefixed);
42delegate_members!(Prefixed);