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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::collections::VecDeque;

use vector_config_common::schema::{visit::Visitor, *};

/// A schema reference which can refer to either a schema definition or the root schema itself.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum SchemaReference {
    /// A defined schema.
    Definition(String),

    /// The root schema itself.
    Root,
}

impl std::fmt::Display for SchemaReference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Definition(name) => write!(f, "{}", name),
            Self::Root => write!(f, "<root>"),
        }
    }
}

impl<'a, T> From<&'a T> for SchemaReference
where
    T: AsRef<str> + ?Sized,
{
    fn from(value: &'a T) -> Self {
        Self::Definition(value.as_ref().to_string())
    }
}

impl AsRef<str> for SchemaReference {
    fn as_ref(&self) -> &str {
        match self {
            Self::Definition(name) => name.as_str(),
            Self::Root => "<root>",
        }
    }
}

/// The schema scope stack is used to understand where in the visiting of a root schema the visitor
/// currently is. All visiting will inherently start at the root, and continue to exist in the root
/// scope until a schema reference is resolved, and the visitor visits the resolved schema. Once
/// that happens, the resolved schema scope is pushed onto the stack, such that the scope is now the
/// schema reference. This can happen multiple times as subsequent schema references are resolved.
/// Once the visitor recurses back out of the resolved schema, it is popped from the stack.
#[derive(Debug, Default)]
pub struct SchemaScopeStack {
    stack: VecDeque<SchemaReference>,
}

impl SchemaScopeStack {
    pub fn push<S: Into<SchemaReference>>(&mut self, scope: S) {
        self.stack.push_front(scope.into());
    }

    pub fn pop(&mut self) -> Option<SchemaReference> {
        self.stack.pop_front()
    }

    pub fn current(&self) -> Option<&SchemaReference> {
        self.stack.front()
    }
}

pub trait ScopedVisitor: Visitor {
    fn push_schema_scope<S: Into<SchemaReference>>(&mut self, scope: S);

    fn pop_schema_scope(&mut self);

    fn get_current_schema_scope(&self) -> &SchemaReference;
}

pub fn visit_schema_object_scoped<SV: ScopedVisitor + ?Sized>(
    sv: &mut SV,
    definitions: &mut Map<String, Schema>,
    schema: &mut SchemaObject,
) {
    if let Some(reference) = schema.reference.as_ref() {
        let schema_def_key = get_cleaned_schema_reference(reference);
        let mut referenced_schema = definitions
            .get(schema_def_key)
            .cloned()
            .expect("schema reference should exist");

        if let Schema::Object(referenced_schema) = &mut referenced_schema {
            sv.push_schema_scope(schema_def_key);

            sv.visit_schema_object(definitions, referenced_schema);

            sv.pop_schema_scope();
        }

        definitions.insert(schema_def_key.to_string(), referenced_schema);
    }

    if let Some(sub) = &mut schema.subschemas {
        visit_vec_scoped(sv, definitions, &mut sub.all_of);
        visit_vec_scoped(sv, definitions, &mut sub.any_of);
        visit_vec_scoped(sv, definitions, &mut sub.one_of);
        visit_box_scoped(sv, definitions, &mut sub.not);
        visit_box_scoped(sv, definitions, &mut sub.if_schema);
        visit_box_scoped(sv, definitions, &mut sub.then_schema);
        visit_box_scoped(sv, definitions, &mut sub.else_schema);
    }

    if let Some(arr) = &mut schema.array {
        visit_single_or_vec_scoped(sv, definitions, &mut arr.items);
        visit_box_scoped(sv, definitions, &mut arr.additional_items);
        visit_box_scoped(sv, definitions, &mut arr.contains);
    }

    if let Some(obj) = &mut schema.object {
        visit_map_values_scoped(sv, definitions, &mut obj.properties);
        visit_map_values_scoped(sv, definitions, &mut obj.pattern_properties);
        visit_box_scoped(sv, definitions, &mut obj.additional_properties);
        visit_box_scoped(sv, definitions, &mut obj.property_names);
    }
}

fn visit_box_scoped<SV: ScopedVisitor + ?Sized>(
    sv: &mut SV,
    definitions: &mut Map<String, Schema>,
    target: &mut Option<Box<Schema>>,
) {
    if let Some(s) = target {
        if let Schema::Object(s) = s.as_mut() {
            sv.visit_schema_object(definitions, s);
        }
    }
}

fn visit_vec_scoped<SV: ScopedVisitor + ?Sized>(
    sv: &mut SV,
    definitions: &mut Map<String, Schema>,
    target: &mut Option<Vec<Schema>>,
) {
    if let Some(vec) = target {
        for s in vec {
            if let Schema::Object(s) = s {
                sv.visit_schema_object(definitions, s);
            }
        }
    }
}

fn visit_map_values_scoped<SV: ScopedVisitor + ?Sized>(
    sv: &mut SV,
    definitions: &mut Map<String, Schema>,
    target: &mut Map<String, Schema>,
) {
    for s in target.values_mut() {
        if let Schema::Object(s) = s {
            sv.visit_schema_object(definitions, s);
        }
    }
}

fn visit_single_or_vec_scoped<SV: ScopedVisitor + ?Sized>(
    sv: &mut SV,
    definitions: &mut Map<String, Schema>,
    target: &mut Option<SingleOrVec<Schema>>,
) {
    match target {
        None => {}
        Some(SingleOrVec::Single(s)) => {
            if let Schema::Object(s) = s.as_mut() {
                sv.visit_schema_object(definitions, s);
            }
        }
        Some(SingleOrVec::Vec(vec)) => {
            for s in vec {
                if let Schema::Object(s) = s {
                    sv.visit_schema_object(definitions, s);
                }
            }
        }
    }
}