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
use preserves::value::Set;

use crate::gen::schema::ModulePath;
use crate::gen::schema::Ref;

pub struct WalkState<T> {
    pub context: T,
    pub module_path: ModulePath,
    seen: Set<Ref>,
}

impl<T> WalkState<T> {
    pub fn new(context: T, module_path: ModulePath) -> Self {
        WalkState {
            context,
            module_path,
            seen: Set::new(),
        }
    }

    pub fn cycle_check<
        E,
        F: Fn(&T, &Ref) -> Option<E>,
        R,
        Ks: FnOnce(&mut Self, Option<E>) -> R,
        Kf: FnOnce() -> R,
    >(
        &mut self,
        r: &Ref,
        step: F,
        ks: Ks,
        kf: Kf,
    ) -> R {
        let r = r.qualify(&self.module_path);
        if self.seen.contains(&r) {
            kf()
        } else {
            self.seen.insert(r.clone());
            let maybe_e = step(&self.context, &r);
            let saved = std::mem::replace(&mut self.module_path, r.module);
            let result = ks(self, maybe_e);
            self.module_path = saved;
            result
        }
    }
}