cherrybomb_oas/legacy/
param.rs

1use super::legacy_oas::*;
2use super::refs::*;
3use super::schema::*;
4use super::utils::*;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
9pub struct Parameter {
10    pub name: String,
11    #[serde(rename = "in")]
12    pub param_in: String,
13    pub description: Option<String>,
14    pub required: Option<bool>,
15    pub deprecated: Option<bool>,
16    #[serde(rename = "allowEmptyValue")]
17    pub allow_empty_value: Option<bool>,
18    //Any
19    pub example: Option<Value>,
20    pub examples: Option<Examples>,
21    pub style: Option<String>,
22    pub explode: Option<bool>,
23    #[serde(rename = "allowReserved")]
24    pub allow_reserved: Option<bool>,
25    pub schema: Option<SchemaRef>,
26}
27impl Parameter {
28    pub fn from(&self) -> QuePay {
29        match self.param_in.to_lowercase().as_str() {
30            "query" => QuePay::Query,
31            "header" => QuePay::Headers,
32            "path" => QuePay::Path,
33            "cookie" => QuePay::Headers,
34            _ => QuePay::None,
35        }
36    }
37    pub fn name(&self) -> String {
38        self.name.clone()
39    }
40    pub fn required(&self) -> bool {
41        if let Some(r) = self.required {
42            r
43        } else {
44            false
45        }
46    }
47    pub fn schema(&self) -> SchemaRef {
48        if let Some(s) = self.schema.clone() {
49            s
50        } else {
51            SchemaRef::default()
52        }
53    }
54    pub fn to_desc(&self) -> ParamDescriptor {
55        ParamDescriptor {
56            name: self.name.clone(),
57            from: self.from(),
58            value: ValueDescriptor::default(),
59        }
60    }
61}
62pub type ParamEnum /*<T>*/ = Vec<Option<SchemaStrInt>>;
63/*
64fn parse_enum_to_int(e:&ParamEnum<String>)->ParamEnum<i64>{
65    let mut v = vec![];
66    for p in e{
67        if let Some(pp) = p{
68            v.push(Some(pp.parse::<i64>().unwrap()));
69        }else{
70            v.push(None);
71        }
72    }
73    v
74}*/
75#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
76pub struct ParamInt {
77    min: Option<f64>,
78    max: Option<f64>,
79    multiple_of: i64,
80    pub p_enum: Option<ParamEnum>,
81    pub default: Option<SchemaStrInt>,
82}
83impl ParamInt {
84    pub fn new(schema: &Schema) -> Self {
85        ParamInt {
86            min: schema.minimum,
87            max: schema.maximum,
88            multiple_of: if let Some(m) = schema.multiple_of {
89                m
90            } else {
91                0
92            },
93            p_enum: schema.schema_enum.clone(),
94            default: schema.default.clone(),
95        }
96    }
97}
98#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
99pub struct ParamString {
100    min_length: i64,
101    max_length: i64,
102    pub p_enum: Option<ParamEnum>,
103    pub default: Option<SchemaStrInt>,
104}
105impl ParamString {
106    pub fn new(schema: &Schema) -> Self {
107        ParamString {
108            min_length: if let Some(m) = schema.min_length {
109                m
110            } else {
111                0
112            },
113            max_length: if let Some(m) = schema.max_length {
114                m
115            } else {
116                i64::MAX
117            },
118            p_enum: schema.schema_enum.clone(),
119            default: schema.default.clone(),
120        }
121    }
122}
123#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
124pub enum ParamValue {
125    Integer(ParamInt),
126    String(ParamString),
127    Object,
128    Array,
129    Boolean,
130    None,
131}
132impl Default for ParamValue {
133    fn default() -> Self {
134        Self::None
135    }
136}
137impl ParamValue {
138    pub fn from(schema: &Schema) -> Self {
139        let v = if let Some(t) = schema.schema_type.clone() {
140            t
141        } else {
142            String::new()
143        };
144        match v.to_lowercase().as_str() {
145            "integer" => ParamValue::Integer(ParamInt::new(schema)),
146            "number" => ParamValue::Integer(ParamInt::new(schema)),
147            "string" => ParamValue::String(ParamString::new(schema)),
148            "object" => Self::Object,
149            "array" => Self::Array,
150            "boolean" => Self::Boolean,
151            _ => {
152                /*println!("{:?}",v);*/
153                Self::None
154            }
155        }
156    }
157}
158#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
159pub struct Param {
160    pub name: String,
161    pub p_type: String,
162    pub format: String,
163    pub description: String,
164    //pub properties:String,
165    pub params: Vec<Param>,
166    pub from: String,
167    pub value: ParamValue,
168    pub dm: QuePay,
169    pub required: bool,
170}
171impl Param {
172    pub fn change_prop(name: String, param: Self, from: String) -> Self {
173        Param {
174            name,
175            p_type: param.p_type,
176            format: param.format,
177            description: param.description,
178            //properties:param.properties,
179            params: param.params,
180            value: param.value,
181            required: param.required,
182            dm: QuePay::Payload,
183            from,
184        }
185    }
186    pub fn change_from(param: Self, from: String) -> Self {
187        Param {
188            name: param.name,
189            p_type: param.p_type,
190            format: param.format,
191            description: param.description,
192            //properties:param.properties,
193            params: param.params,
194            value: param.value,
195            required: param.required,
196            dm: QuePay::Payload,
197            from,
198        }
199    }
200    pub fn required(schema: &Schema, _p_type: &str, requireds: Vec<String>) -> bool {
201        let name = if let Some(n) = schema.title.clone() {
202            n
203        } else {
204            String::new()
205        };
206        requireds.contains(&name)
207        /* match &p_type.to_lowercase().as_str(){
208            "object"|"" =>,
209            "boolean"|"integer"|"string"|"array"=>requireds.contains(,
210        }*/
211    }
212    fn object_to_param(swagger: &Value, schema: Schema, p_type: String, required: bool) -> Param {
213        let mut params: Vec<Param> = vec![];
214        let s1 = schema.clone();
215        let requireds = if let Some(r) = schema.required {
216            r
217        } else {
218            vec![]
219        };
220        if let Some(items) = schema.items {
221            let inner = items.inner(swagger);
222            let r = Self::required(&inner, &p_type, requireds.clone());
223            params.push(Param::change_from(
224                Self::schema_rec(swagger, inner, r),
225                String::from("items"),
226            ));
227        }
228        if let Some(all) = schema.all_of {
229            for sc in all {
230                params.push(Param::change_from(
231                    Self::schema_rec(swagger, sc.inner(swagger), true),
232                    String::from("all"),
233                ));
234            }
235        }
236        if let Some(any) = schema.any_of {
237            for sc in any {
238                let r = Self::required(&sc.inner(swagger), &p_type, requireds.clone());
239                params.push(Param::change_from(
240                    Self::schema_rec(swagger, sc.inner(swagger), r),
241                    String::from("any"),
242                ));
243            }
244        }
245        if let Some(one) = schema.one_of {
246            for sc in one {
247                let r = Self::required(&sc.inner(swagger), &p_type, requireds.clone());
248                params.push(Param::change_from(
249                    Self::schema_rec(swagger, sc.inner(swagger), r),
250                    String::from("one"),
251                ));
252            }
253        }
254        if let Some(not) = schema.not {
255            let r = Self::required(&s1, &p_type, requireds.clone());
256            params.push(Param::change_from(
257                Self::schema_rec(swagger, not.inner(swagger), r),
258                String::from("not"),
259            ));
260        }
261        if let Some(prop) = schema.properties {
262            for (p_name, p) in prop {
263                let r = Self::required(&p.inner(swagger), &p_type, requireds.clone());
264                params.push(Param::change_prop(
265                    p_name,
266                    Self::schema_rec(swagger, p.inner(swagger), r),
267                    String::from("prop"),
268                ));
269            }
270        }
271        let name = if let Some(n) = schema.title {
272            n
273        } else {
274            String::new()
275        };
276        let description = if let Some(d) = schema.description {
277            d
278        } else {
279            String::new()
280        };
281        let format = if let Some(f) = schema.format {
282            println!("{f}");
283            f
284        } else {
285            String::new()
286        };
287        Param {
288            name,
289            description,
290            p_type,
291            format,
292            params,
293            from: String::new(),
294            value: ParamValue::from(&s1),
295            required,
296            dm: QuePay::Payload,
297        }
298    }
299    fn simple_to_param(schema: Schema, p_type: String, required: bool) -> Self {
300        let s1 = schema.clone();
301        let name = if let Some(n) = schema.title {
302            n
303        } else {
304            String::new()
305        };
306        let description = if let Some(d) = schema.description {
307            d
308        } else {
309            String::new()
310        };
311        let format = if let Some(f) = schema.format {
312            f
313        } else {
314            String::new()
315        };
316        Param {
317            name,
318            description,
319            p_type,
320            format,
321            params: vec![],
322            from: String::new(),
323            value: ParamValue::from(&s1),
324            required,
325            dm: QuePay::Payload,
326        }
327    }
328    pub fn schema_rec(swagger: &Value, schema: Schema, required: bool) -> Self {
329        let p_type = if let Some(t) = schema.schema_type.clone() {
330            t
331        } else {
332            String::new()
333        };
334        match p_type.to_lowercase().as_str() {
335            "array" | "" | "object" => Self::object_to_param(swagger, schema, p_type, required),
336            "number" | "boolean" | "integer" | "string" => {
337                Self::simple_to_param(schema, String::from("string"), required)
338            }
339            _ => {
340                println!("{p_type:?}");
341                Param::default()
342            }
343        }
344    }
345    pub fn schema_to_params(
346        swagger: &Value,
347        schema: SchemaRef,
348        name: String,
349        required: bool,
350    ) -> Self {
351        let mut params: Vec<Param> = vec![];
352        let mut schemas: Vec<Schema> = vec![];
353        let sc = schema.inner(swagger);
354        if let Some(all) = sc.all_of {
355            schemas.extend(
356                all.iter()
357                    .map(|s| s.inner(swagger))
358                    .collect::<Vec<Schema>>(),
359            );
360        }
361        if let Some(any) = sc.any_of {
362            schemas.extend(
363                any.iter()
364                    .map(|s| s.inner(swagger))
365                    .collect::<Vec<Schema>>(),
366            );
367        }
368        if let Some(one) = sc.one_of {
369            schemas.extend(
370                one.iter()
371                    .map(|s| s.inner(swagger))
372                    .collect::<Vec<Schema>>(),
373            );
374        }
375        if let Some(not) = sc.not {
376            schemas.push(not.inner(swagger));
377        }
378        if let Some(props) = sc.properties {
379            for (_, p) in props {
380                schemas.push(p.inner(swagger));
381            }
382        }
383        for schema in schemas {
384            let p_type = if let Some(t) = schema.schema_type.clone() {
385                t
386            } else {
387                String::new()
388            };
389            let requireds = if let Some(r) = schema.required.clone() {
390                r
391            } else {
392                vec![]
393            };
394            let r = Self::required(&schema, &p_type, requireds);
395            params.push(Self::schema_rec(swagger, schema.clone(), r));
396        }
397        Param {
398            name,
399            params,
400            required,
401            dm: QuePay::Payload,
402            ..Param::default()
403        }
404    }
405}