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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::parser::types::{FragmentSpread, InlineFragment, SelectionSet};
use crate::validation::visitor::{Visitor, VisitorContext};
use crate::Positioned;

pub struct DepthCalculate<'a> {
    max_depth: &'a mut i32,
    current_depth: i32,
}

impl<'a> DepthCalculate<'a> {
    pub fn new(max_depth: &'a mut i32) -> Self {
        *max_depth = -1;
        Self {
            max_depth,
            current_depth: -1,
        }
    }
}

impl<'ctx, 'a> Visitor<'ctx> for DepthCalculate<'a> {
    fn enter_selection_set(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _selection_set: &'ctx Positioned<SelectionSet>,
    ) {
        self.current_depth += 1;
        *self.max_depth = (*self.max_depth).max(self.current_depth);
    }

    fn exit_selection_set(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _selection_set: &'ctx Positioned<SelectionSet>,
    ) {
        self.current_depth -= 1;
    }

    fn enter_fragment_spread(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _fragment_spread: &'ctx Positioned<FragmentSpread>,
    ) {
        self.current_depth -= 1;
    }

    fn exit_fragment_spread(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _fragment_spread: &'ctx Positioned<FragmentSpread>,
    ) {
        self.current_depth += 1;
    }

    fn enter_inline_fragment(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _inline_fragment: &'ctx Positioned<InlineFragment>,
    ) {
        self.current_depth -= 1;
    }

    fn exit_inline_fragment(
        &mut self,
        _ctx: &mut VisitorContext<'ctx>,
        _inline_fragment: &'ctx Positioned<InlineFragment>,
    ) {
        self.current_depth += 1;
    }
}