schematools/
tools.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::fmt::Display;
use std::str::{Chars, FromStr};

use crate::error::Error;
use crate::scope::SchemaScope;
use serde::Serialize;
use serde_json::Value;

pub fn each_node_mut<F>(
    root: &mut Value,
    context: &mut SchemaScope,
    path: &str,
    mut f: F,
) -> Result<(), Error>
where
    F: FnMut(&mut Value, &[String], &mut SchemaScope) -> Result<(), Error>,
{
    let parts = path
        .trim_matches('/')
        .split('/')
        .map(|s| s.to_string())
        .collect::<Vec<String>>();

    each_mut(root, context, &parts, 0, &mut vec![], &mut f)
}

fn each_mut<F>(
    node: &mut Value,
    context: &mut SchemaScope,
    path: &[String],
    index: usize,
    parts: &mut Vec<String>,
    f: &mut F,
) -> Result<(), Error>
where
    F: FnMut(&mut Value, &[String], &mut SchemaScope) -> Result<(), Error>,
{
    match path.get(index) {
        None => f(node, parts, context),
        Some(search) => {
            if let [type_, search_key] = &search.split(':').collect::<Vec<&str>>()[..] {
                match *search_key {
                    "*" => match node {
                        Value::Object(ref mut map) => {
                            for (key, value) in map {
                                context.push_str(type_, key);

                                parts.push(key.clone());
                                each_mut(value, context, path, index + 1, parts, f)?;
                                parts.pop();

                                context.pop();
                            }

                            Ok(())
                        }
                        _ => Err(Error::NotImplemented),
                    },
                    real_path => {
                        context.push_str(type_, real_path);

                        if let Some(ref mut found) = node.pointer_mut(&["/", real_path].join("")) {
                            each_mut(found, context, path, index + 1, parts, f)?;
                        }

                        context.pop();

                        Ok(())
                    }
                }
            } else {
                panic!("Incorrect path: {}", search);
            }
        }
    }
}

pub fn each_node<F>(
    root: &Value,
    context: &mut SchemaScope,
    path: &str,
    mut f: F,
) -> Result<(), Error>
where
    F: FnMut(&Value, &[String], &mut SchemaScope) -> Result<(), Error>,
{
    let parts = path
        .trim_matches('/')
        .split('/')
        .map(|s| s.to_string())
        .collect::<Vec<String>>();

    each(root, context, &parts, 0, &mut vec![], &mut f)
}

fn each<F>(
    node: &Value,
    context: &mut SchemaScope,
    path: &[String],
    index: usize,
    parts: &mut Vec<String>,
    f: &mut F,
) -> Result<(), Error>
where
    F: FnMut(&Value, &[String], &mut SchemaScope) -> Result<(), Error>,
{
    match path.get(index) {
        None => f(node, parts, context),
        Some(search) => {
            if let [type_, search_key] = &search.split(':').collect::<Vec<&str>>()[..] {
                match *search_key {
                    "*" => match node {
                        Value::Object(ref map) => {
                            for (key, value) in map {
                                context.push_str(type_, key);

                                parts.push(key.clone());
                                each(value, context, path, index + 1, parts, f)?;
                                parts.pop();

                                context.pop();
                            }

                            Ok(())
                        }
                        _ => Err(Error::NotImplemented),
                    },
                    real_path => {
                        context.push_str(type_, real_path);

                        if let Some(ref mut found) = node.pointer(&["/", real_path].join("")) {
                            each(found, context, path, index + 1, parts, f)?;
                        }

                        context.pop();

                        Ok(())
                    }
                }
            } else {
                panic!("Incorrect path: {}", search);
            }
        }
    }
}

pub struct ArgumentsExtractor<'a> {
    chars: Chars<'a>,
}

impl<'a> ArgumentsExtractor<'a> {
    pub fn new(command: &'a str) -> Self {
        Self {
            chars: command.chars(),
        }
    }
}

impl Iterator for ArgumentsExtractor<'_> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let mut out = String::new();
        let mut escaped = false;
        let mut quote_char = None;
        for c in &mut self.chars {
            if escaped {
                out.push(c);
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if let Some(qc) = quote_char {
                if c == qc {
                    quote_char = None;
                } else {
                    out.push(c);
                }
            } else if c == '\'' || c == '"' {
                quote_char = Some(c);
            } else if c.is_whitespace() {
                if !out.is_empty() {
                    return Some(out);
                } else {
                    continue;
                }
            } else {
                out.push(c);
            }
        }

        if !out.is_empty() {
            Some(out)
        } else {
            None
        }
    }
}

pub fn fill_parameters(phrase: &str, data: (impl Serialize + Clone)) -> Result<String, Error> {
    let chars = phrase.chars();
    let mut result = String::new();

    let mut current = String::new();
    let mut parameter = false;
    for c in chars {
        if c == '%' {
            parameter = !parameter;

            if !current.is_empty() {
                let path = format!("/{}", current.replace('.', "/"));

                if let Some(value) = serde_json::json!(data).pointer(&path) {
                    result.push_str(&match value {
                        Value::String(s) => Ok(s.clone()),
                        Value::Number(n) => Ok(n.to_string()),
                        _ => Err(Error::CannotFillParameters(path)),
                    }?);

                    current.clear();
                } else {
                    return Err(Error::CannotFillParameters(path));
                }
            }

            continue;
        } else if parameter {
            current.push(c)
        } else {
            result.push(c);
        }
    }

    Ok(result)
}

pub fn bump_suffix_number(phrase: &str) -> String {
    let chars = phrase.chars();
    let mut result: Vec<u32> = vec![];

    for c in chars.rev() {
        if c.is_numeric() {
            result.push(c.to_digit(10).unwrap());
            continue;
        } else {
            break;
        }
    }

    if result.is_empty() {
        let new_phrase = phrase.to_string();
        new_phrase + "2"
    } else {
        let new_phrase = phrase[..phrase.len() - result.len()].to_string();
        let sum = result.iter().rev().fold(0, |acc, elem| acc * 10 + elem) + 1;
        new_phrase + &sum.to_string()
    }
}

#[derive(Default)]
pub struct Filter {
    conditions: Vec<ConditionSet>,
}

pub struct ConditionSet {
    conditions: Vec<Condition>,
}

impl FromStr for ConditionSet {
    type Err = Error;

    fn from_str(data: &str) -> Result<Self, Self::Err> {
        Ok(Self {
            conditions: data
                .split(',')
                .map(Condition::from_str)
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

impl ConditionSet {
    pub fn check(&self, data: &Value) -> bool {
        self.conditions
            .iter()
            .map(|c| c.check(data))
            .all(|result| result)
    }
}

struct Condition {
    pub field: String, // json pointer
    pub operator: ConditionOperator,
    pub value: Value, // Value
}

impl FromStr for Condition {
    type Err = Error;

    fn from_str(data: &str) -> Result<Self, Self::Err> {
        let operator = ConditionOperator::from_str(data)?;

        if let [field, value] = data.split(&operator.to_string()).collect::<Vec<_>>()[..] {
            Ok(Self {
                field: format!("/{}", field.replace('.', "/")),
                value: serde_json::from_str(value).unwrap(),
                operator,
            })
        } else {
            Err(Error::IncorrectFilterError(data.to_string()))
        }
    }
}

impl Condition {
    pub fn check(&self, data: &Value) -> bool {
        match data.pointer(&self.field) {
            Some(retrieved) => match self.operator {
                ConditionOperator::Eq | ConditionOperator::Eqq => retrieved == &self.value,
                ConditionOperator::Neq => retrieved != &self.value,
            },
            None => self.operator == ConditionOperator::Neq,
        }
    }
}

#[derive(Eq, PartialEq)]
enum ConditionOperator {
    Eq,
    Eqq,
    Neq,
}

impl Display for ConditionOperator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str = match *self {
            Self::Eq => "=",
            Self::Eqq => "==",
            Self::Neq => "!=",
        }
        .to_string();

        write!(f, "{}", str)
    }
}

impl FromStr for ConditionOperator {
    type Err = Error;

    fn from_str(data: &str) -> Result<ConditionOperator, Self::Err> {
        if data.contains("==") {
            Ok(Self::Eqq)
        } else if data.contains("!=") {
            Ok(Self::Neq)
        } else if data.contains('=') {
            Ok(Self::Eq)
        } else {
            Err(Error::IncorrectFilterError(data.to_string()))
        }
    }
}

impl Filter {
    pub fn new(filters: &[String]) -> Result<Self, Error> {
        Ok(Self {
            conditions: filters
                .iter()
                .map(|s| ConditionSet::from_str(s))
                .collect::<Result<Vec<_>, _>>()?,
        })
    }

    pub fn check(&self, data: &Value, default: bool) -> bool {
        if self.conditions.is_empty() {
            return default;
        }

        self.conditions
            .iter()
            .map(|c| c.check(data))
            .any(|result| result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_suffix_number_empty() {
        let result = bump_suffix_number("asd");

        assert_eq!(result, "asd2".to_string());
    }

    #[test]
    fn test_extract_suffix_number_success() {
        let result = bump_suffix_number("asd543");

        assert_eq!(result, "asd544".to_string());
    }

    #[test]
    fn test_fill_parameters() {
        let given = serde_json::json!({
            "options": {
                "test": "10",
                "num": 2
            }
        });

        let result =
            fill_parameters("some variable %options.test% ok %options.num%", given).unwrap();

        assert_eq!(result, "some variable 10 ok 2".to_string());
    }

    #[test]
    fn test_argument_extractor() {
        let given = "codegen openapi -f - --templates-dir codegen/ --format \"gofmt -w\" --target-dir pkg/client/ -o namespace=testing -o clientName=TestingClient";

        let result: Vec<String> = ArgumentsExtractor::new(given).collect();

        assert_eq!(
            result,
            vec![
                "codegen",
                "openapi",
                "-f",
                "-",
                "--templates-dir",
                "codegen/",
                "--format",
                "gofmt -w",
                "--target-dir",
                "pkg/client/",
                "-o",
                "namespace=testing",
                "-o",
                "clientName=TestingClient"
            ]
            .to_vec()
        );
    }
}