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
//! Companion to [`std::ops::ControlFlow`] useful to implement visitor patterns.

use std::ops::ControlFlow;

/// Type to be returned by visitor closure to control the tree traversal flow.
pub enum VisitorControlFlow<B> {
    /// Continue tree traversal
    Continue,

    /// Continue tree traversal but skip the children of the current item.
    SkipBranch,

    /// Stop traversal and return this value.
    Break(B),
}

impl<B> VisitorControlFlow<B> {
    /// Indicates whether we should visit the children of the current nodeā€”or entirely stop
    /// traversal.
    ///
    /// Returning a [`ControlFlow`] enables key ergonomics by allowing the use of the short circuit
    /// operator (`?`) while extracting the flag to control traversal of children.
    pub fn visit_children(self) -> ControlFlow<B, bool> {
        match self {
            Self::Break(val) => ControlFlow::Break(val),
            Self::Continue => ControlFlow::Continue(true),
            Self::SkipBranch => ControlFlow::Continue(false),
        }
    }
}