openapi_utils/
types.rs

1use openapiv3::*;
2
3/// Extension methods for Type
4pub trait TypeExt {
5    /// true if this type is `Type::Boolean`
6    fn is_bool(&self) -> bool;
7
8    /// true if this type is `Type::Integer`
9    fn is_integer(&self) -> bool;
10
11    /// true if this type is `Type::Number`
12    fn is_number(&self) -> bool;
13
14    /// true if this type is `Type::String`
15    fn is_string(&self) -> bool;
16}
17
18impl TypeExt for Type {
19    fn is_bool(&self) -> bool {
20        matches!(self, Type::Boolean(_))
21    }
22
23    fn is_integer(&self) -> bool {
24        matches!(self, Type::Integer(_))
25    }
26
27    fn is_number(&self) -> bool {
28        matches!(self, Type::Number(_))
29    }
30
31    fn is_string(&self) -> bool {
32        matches!(self, Type::String(_))
33    }
34}
35
36/// Extends the `IntegerType` with convenience methods
37pub trait IntegerTypeExt {
38    /// Returns the minimum and maximum information as a tuple of i64
39    /// If there is no minimum or maximum, the minimum i64 or maximum i64 values are used.
40    fn min_max(&self) -> (i64, i64);
41}
42
43impl IntegerTypeExt for IntegerType {
44    fn min_max(&self) -> (i64, i64) {
45        let the_min = match self.minimum {
46            Some(minimum) => {
47                if self.exclusive_minimum {
48                    minimum + 1
49                } else {
50                    minimum
51                }
52            }
53            None => i64::min_value(),
54        };
55
56        let the_max = match self.maximum {
57            Some(maximum) => {
58                if self.exclusive_maximum {
59                    maximum - 1
60                } else {
61                    maximum
62                }
63            }
64            None => i64::max_value(),
65        };
66        (the_min, the_max)
67    }
68}
69
70
71/// Extends the `NumberType` with convenience methods
72pub trait NumberTypeExt {
73    /// Returns the minimum and maximum information as a tuple of f64
74    /// If there is no minimum or maximum, the minimum f64 or maximum f64 values are used.
75    fn min_max(&self) -> (f64, f64);
76}
77
78impl NumberTypeExt for NumberType {
79    fn min_max(&self) -> (f64, f64) {
80        let the_min = match self.minimum {
81            Some(minimum) => {
82                if self.exclusive_minimum {
83                    minimum + 1.0
84                } else {
85                    minimum
86                }
87            }
88            None => core::f64::MIN,
89        };
90
91        let the_max = match self.maximum {
92            Some(maximum) => {
93                if self.exclusive_maximum {
94                    maximum - 1.0
95                } else {
96                    maximum
97                }
98            }
99            None => core::f64::MAX,
100        };
101        (the_min, the_max)
102    }
103}