-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathschema.rs
153 lines (151 loc) · 4.15 KB
/
schema.rs
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
use super::refs::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum SchemaStrInt {
Int(i64),
Str(String),
Bool(bool),
Float(f64),
}
impl Default for SchemaStrInt {
fn default() -> Self {
Self::Int(0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum AddProps {
Bool(bool),
Schema(Box<Schema>),
}
impl Default for AddProps {
fn default() -> Self {
Self::Bool(true)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum SchemaTypes {
Str(String),
Arr(Vec<String>),
Obj(HashMap<String,String>),
}
impl SchemaTypes{
pub fn as_str(&self)->String{
match self{
Self::Str(s) => s.to_string(),
Self::Arr(v) => v[0].to_string(),
Self::Obj(h) => h.get("type").unwrap().to_string(),
}
}
}
impl fmt::Display for SchemaTypes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Schema {
pub title: Option<String>,
#[serde(rename = "multipleOf")]
pub multiple_of: Option<i64>,
pub maximum: Option<f64>,
pub minimum: Option<f64>,
#[serde(rename = "exclusiveMaximum")]
pub exclusive_maximum: Option<String>,
#[serde(rename = "maxLength")]
pub max_length: Option<i64>,
#[serde(rename = "minLength")]
pub min_length: Option<i64>,
//String - STAY AWAY!(regex)
pub pattern: Option<String>,
#[serde(rename = "maxItems")]
pub max_items: Option<i64>,
#[serde(rename = "minItems")]
pub min_items: Option<i64>,
#[serde(rename = "uniqueItem")]
pub unique_items: Option<String>,
#[serde(rename = "maxProperties")]
pub max_properties: Option<i64>,
#[serde(rename = "minProperties")]
pub min_properties: Option<i64>,
//Array
pub items: Option<Box<SchemaRef>>,
pub required: Option<Vec<String>>,
#[serde(rename = "enum")]
pub schema_enum: Option<Vec<Option<SchemaStrInt>>>,
#[serde(rename = "type")]
pub schema_type: Option<SchemaTypes>,
#[serde(rename = "allOf")]
pub all_of: Option<Vec<SchemaRef>>,
#[serde(rename = "oneOf")]
pub one_of: Option<Vec<SchemaRef>>,
#[serde(rename = "anyOf")]
pub any_of: Option<Vec<SchemaRef>>,
pub not: Option<Box<SchemaRef>>,
//object
pub properties: Option<HashMap<String, SchemaRef>>,
#[serde(rename = "additionalProperties")]
pub additional_properties: Option<AddProps>,
pub description: Option<String>,
pub format: Option<String>,
pub default: Option<SchemaStrInt>,
pub example: Option<Value>,
//not in swagger
}
/*
pub struct SchemaLoc{
schema:SchemaRef,
location:&'static str,
}
impl Schema{
pub fn schemas(&self)->Vec<SchemaLoc>{
let mut schemas = vec![];
if let Some(all) = &self.all_of{
for s in all {
schemas.push(SchemaLoc{
schema:s.clone(),
location:"all",
});
}
}
if let Some(any) = &self.any_of{
for s in any {
schemas.push(SchemaLoc{
schema:s.clone(),
location:"any",
});
}
}
if let Some(one) = &self.one_of{
for s in one {
schemas.push(SchemaLoc{
schema:s.clone(),
location:"one",
});
}
}
/*
if let Some(not) = &self.not{
for s in not {
schemas.push(SchemaLoc{
schema:s.clone(),
location:"not",
});
}
}*/
if let Some(props) = &self.properties{
for (st,s) in props {
schemas.push(SchemaLoc{
schema:s.clone(),
location:"props",
});
}
}
schemas
}
}*/