directed_visit/
direct.rs

1use crate::{DirectorVisitor, Visit, VisitMut, Visitor};
2
3/// A wrapper for a [Direct] implementation.
4#[derive(Debug)]
5pub struct Director<'dv, D: ?Sized, V: ?Sized>(DirectorVisitor<'dv, D, V>);
6
7impl<'dv, D: ?Sized, V: ?Sized> Director<'dv, D, V> {
8    pub(crate) fn new(data: DirectorVisitor<'dv, D, V>) -> Self {
9        Self(data)
10    }
11
12    /// Direct from this node to a sub-node with the [Visit] implementation.
13    pub fn direct<NN: ?Sized>(this: &mut Self, node: &NN)
14    where
15        D: Direct<V, NN>,
16        V: Visit<NN>,
17    {
18        <V as Visit<NN>>::visit(Visitor::new(this.0.reborrow()), node)
19    }
20
21    /// Direct from this node to a sub-node with the [VisitMut] implementation.
22    pub fn direct_mut<NN: ?Sized>(this: &mut Self, node: &mut NN)
23    where
24        D: DirectMut<V, NN>,
25        V: VisitMut<NN>,
26    {
27        <V as VisitMut<NN>>::visit_mut(Visitor::new(this.0.reborrow()), node)
28    }
29}
30
31impl<D: ?Sized, V: ?Sized> std::ops::Deref for Director<'_, D, V> {
32    type Target = D;
33
34    fn deref(&self) -> &Self::Target {
35        self.0.direct
36    }
37}
38
39impl<D: ?Sized, V: ?Sized> std::ops::DerefMut for Director<'_, D, V> {
40    fn deref_mut(&mut self) -> &mut Self::Target {
41        self.0.direct
42    }
43}
44
45/// Determines how to traverse the nodes within the input. This must be implemented for
46/// all node types in the input.
47pub trait Direct<V: ?Sized, N: ?Sized> {
48    /// Determines all the sub-nodes of the given node. For each sub-node, call
49    /// `Director::direct(&mut director, &node.my_subnode)`.
50    fn direct(director: Director<'_, Self, V>, node: &N);
51}
52
53/// Determines how to traverse the nodes within the input. This must be implemented for
54/// all node types in the input.
55pub trait DirectMut<V: ?Sized, N: ?Sized> {
56    /// Determines all the sub-nodes of the given node. For each sub-node, call
57    /// `Director::direct_mut(&mut director, &mut node.my_subnode)`.
58    fn direct_mut(director: Director<'_, Self, V>, node: &mut N);
59}