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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use super::*;

/// Connects all the bread crumbs to form a json pointer like string, for debugging
pub fn connect_bread_crumbs(bread_crumb: &[String]) -> String {
    let mut res = String::from('/');
    res.push_str(bread_crumb.join("/").as_str());
    res
}

/// ## Check trait for OApi
///
/// This trait allows struct of the OApi crates and extensions to be validated at
/// runtime for logic errors
pub trait OApiCheckTrait {
    /// Check the current object, if any checks are to be performed, then checks
    /// its inner attributes
    fn oapi_check(
        &self,
        state: &SparseRoot<OApiDocument>,
        bread_crumb: &mut Vec<String>,
    ) -> Result<(), OApiError> {
        self.oapi_check_inner(state, bread_crumb)
    }

    /// Check every inner attributes of the object
    fn oapi_check_inner(
        &self,
        state: &SparseRoot<OApiDocument>,
        bread_crumb: &mut Vec<String>,
    ) -> Result<(), OApiError>;
}

macro_rules! impl_oapi_check_nothing {
    ($x:ident) => {
        impl OApiCheckTrait for $x {
            fn oapi_check_inner(
                &self,
                _state: &SparseRoot<OApiDocument>,
                _bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                Ok(())
            }
        }
    };
}

macro_rules! impl_oapi_check_iter {
    ($x:ident) => {
        impl<T> OApiCheckTrait for $x<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                for i in self.iter() {
                    i.oapi_check(state, bread_crumb)?;
                }
                Ok(())
            }
        }
    };
}

macro_rules! impl_oapi_check_sparse {
    () => {
        impl<T> OApiCheckTrait for SparseSelector<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                match self {
                    SparseSelector::Ref(x) => x.oapi_check(state, bread_crumb),
                    SparseSelector::Obj(x) => x.oapi_check(state, bread_crumb),
                    _ => Ok(()),
                }
            }
        }

        impl<T> OApiCheckTrait for sppparse::SparseRefRaw<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                self.val().oapi_check(state, bread_crumb)
            }
        }

        impl<T> OApiCheckTrait for sppparse::SparseRefRawInline<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                self.val().oapi_check(state, bread_crumb)
            }
        }

        impl<T> OApiCheckTrait for sppparse::SparseRef<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                self.val().oapi_check(state, bread_crumb)
            }
        }

        impl<T> OApiCheckTrait for sppparse::SparsePointedValue<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                match self {
                    SparsePointedValue::Ref(x) => x.oapi_check(state, bread_crumb),
                    SparsePointedValue::RefRaw(x) => x.oapi_check(state, bread_crumb),
                    SparsePointedValue::Obj(x) => x.oapi_check(state, bread_crumb),
                    _ => Ok(()),
                }
            }
        }
    };
}

macro_rules! impl_oapi_check_special_types {
    () => {
        impl<'a> OApiCheckTrait for &'a [u8] {
            fn oapi_check_inner(
                &self,
                _state: &SparseRoot<OApiDocument>,
                _bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                Ok(())
            }
        }

        impl<T> OApiCheckTrait for Option<T>
        where
            T: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                match self {
                    Some(x) => x.oapi_check(state, bread_crumb),
                    None => Ok(()),
                }
            }
        }

        impl<K, V> OApiCheckTrait for HashMap<K, V>
        where
            V: OApiCheckTrait,
        {
            fn oapi_check_inner(
                &self,
                state: &SparseRoot<OApiDocument>,
                bread_crumb: &mut Vec<String>,
            ) -> Result<(), OApiError> {
                for i in self.values() {
                    i.oapi_check(state, bread_crumb)?;
                }
                Ok(())
            }
        }
    };
}

macro_rules! impl_oapi_check {
    ($t:tt) => {
        impl_oapi_check_sparse!();
        impl_oapi_check_nothing!(bool);
        impl_oapi_check_nothing!(i8);
        impl_oapi_check_nothing!(i16);
        impl_oapi_check_nothing!(i32);
        impl_oapi_check_nothing!(i64);
        impl_oapi_check_nothing!(isize);
        impl_oapi_check_nothing!(u8);
        impl_oapi_check_nothing!(u16);
        impl_oapi_check_nothing!(u32);
        impl_oapi_check_nothing!(u64);
        impl_oapi_check_nothing!(i128);
        impl_oapi_check_nothing!(usize);
        impl_oapi_check_nothing!(f32);
        impl_oapi_check_nothing!(f64);
        impl_oapi_check_nothing!(char);
        impl_oapi_check_nothing!(String);
        impl_oapi_check_nothing!(Version);
        impl_oapi_check_nothing!(Value);
        impl_oapi_check_special_types!();
        impl_oapi_check_iter!(Vec);
    };
}

impl_oapi_check!(OApiDocumentCore);