semver-query 0.3.1

semantic versioning data query tool
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

use std::{fmt::Debug, io};
use std::fmt;
use jsonpath_rust::JsonPath;
use luaparse::ast::Expr;
use luaparse::ast::Statement::If;
use luaparse::token::Symbol as symb;
use luaparse::token::TokenValue::Symbol;
use std::error::Error;
use regex::Regex;
use luaparse::{parse};
use serde::{Deserialize, Serialize};
use serde_json::{Value};
use std::cmp::Ordering;
use std::str::FromStr;


pub const ASCENDING_ORDER: &str = "asc";
pub const DESCENDING_ORDER: &str = "desc";
pub const NO_ORDER: &str = "none";


#[derive(Debug,Serialize, Deserialize)]
struct SemVer {
    major: u16,
    minor: u16,
    patch: u16,
    pre_release: Option<String>, 
    build_number: Option<String>,
    has_v_prefix: bool,
}

impl Ord for SemVer {
    fn cmp(&self, other: &Self) -> Ordering {
        let core = self.major
            .cmp(&other.major)
            .then(self.minor.cmp(&other.minor))
            .then(self.patch.cmp(&other.patch));

        /////// done by copilot
        if core != Ordering::Equal {
            return core;
        }

         match (&self.pre_release, &other.pre_release) {
            (None, None) => Ordering::Equal,
            (None, Some(_)) => Ordering::Greater, // final > prerelease
            (Some(_), None) => Ordering::Less,    // prerelease < final
            (Some(a), Some(b)) => compare_prerelease(a, b),
        }
        ///////////
    }
}

impl PartialOrd for SemVer {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for SemVer {
    fn eq(&self, other: &Self) -> bool {
        self.major == other.major && self.minor == other.minor &&
        self.patch == other.patch && 
        self.pre_release == other.pre_release
    }
}

impl Eq for SemVer {}

#[derive(Copy, Clone)]
pub enum SortOrder {
    Ascending,Descending,None
}

#[derive(Debug, PartialEq, Eq)]
pub struct ParseSortOrderError;

impl FromStr for SortOrder {
    type Err = ParseSortOrderError;
    fn from_str(order: &str) -> Result<Self, Self::Err> {
        match order {
            ASCENDING_ORDER => {
                Ok(SortOrder::Ascending)
            }
            DESCENDING_ORDER => {
                Ok(SortOrder::Descending)
            }
            NO_ORDER => {
               Ok(SortOrder::None) 
            }
            _ => {
                Err(ParseSortOrderError)
            }
        }
    }
}

impl fmt::Display for SortOrder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let order: &str;
        match self {
            SortOrder::Ascending => {
                order = ASCENDING_ORDER;
            }
            SortOrder::Descending => {
                order = DESCENDING_ORDER;
            }
            SortOrder::None => {
              order = NO_ORDER;
            }
        }

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

#[derive(Debug)]
struct QueryTraversalResult {
    identifiers: Vec<String>,
    comparators: Vec<luaparse::token::Symbol>,
    literals: Vec<String>,
    connectors: Vec<luaparse::token::Symbol>,
    errors: Vec<String>,
}

impl fmt::Display for SemVer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut sem_ver: String; 
        if self.has_v_prefix {
            sem_ver = format!("v{}.{}.{}", self.major, self.minor, self.patch);
        } else {
            sem_ver = format!("{}.{}.{}", self.major, self.minor, self.patch);
        }   
        match self.pre_release.clone() {
            Some(pre_release) => {
                sem_ver.push_str(format!("-{}", pre_release).as_str());
            }
            _ => {}
        }

        match self.build_number.clone() {
            Some(build_number) => {
                sem_ver.push_str(format!("+{}", build_number).as_str());
            }
            _ => {}
        }

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

impl QueryTraversalResult {
    pub fn new() -> QueryTraversalResult {
        QueryTraversalResult {
            identifiers: Vec::new(),
            comparators: Vec::new(),
            literals: Vec::new(),
            connectors: Vec::new(), 
            errors: Vec::new(),
        }
    }
}

// function written by copilot
fn is_numeric_identifier(identifier: &str) -> bool {
    !identifier.is_empty() && identifier.chars().all(|c| c.is_ascii_digit())
}

// function written by copilot
fn compare_prerelease(a: &str, b: &str) -> Ordering {
    let a_parts: Vec<&str> = a.split('.').collect();
    let b_parts: Vec<&str> = b.split('.').collect();
    for (a_part, b_part) in a_parts.iter().zip(b_parts.iter()) {
        let a_is_num = is_numeric_identifier(a_part);
        let b_is_num = is_numeric_identifier(b_part);
        match (a_is_num, b_is_num) {
            (true, true) => {
                // Both numeric: compare by numeric value
                let a_num = a_part.parse::<u64>().unwrap_or(0);
                let b_num = b_part.parse::<u64>().unwrap_or(0);
                let ord = a_num.cmp(&b_num);
                if ord != Ordering::Equal {
                    return ord;
                }
            }
            (true, false) => {
                // Numeric identifiers have lower precedence than non-numeric
                return Ordering::Less;
            }
            (false, true) => {
                return Ordering::Greater;
            }
            (false, false) => {
                let ord = a_part.cmp(b_part);
                if ord != Ordering::Equal {
                    return ord;
                }
            }
        }
    }
    // All shared identifiers equal; shorter prerelease has lower precedence
    a_parts.len().cmp(&b_parts.len())
}


pub fn query_semver(query: &String, semver_entries: Vec<String>, strict: bool, sort_order: SortOrder, limit: usize) -> Result<Vec<String>, Box<dyn Error>> {
    // source: https://github.com/semver/semver/blob/master/semver.md?plain=1#L346
    let semver_regex = Regex::new(r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$")?;

    let mut parsed_sem_vers :Vec<SemVer> = vec![];

    let mut has_v_prefix: bool = false;
    for mut semver in semver_entries {
        if semver.starts_with("v") {
            semver = semver.replacen("v", "", 1);
            has_v_prefix = true;
        }
        if !semver_regex.is_match(semver.as_str()) {
            if strict {
                return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, format!("{semver} does not follow the semantic versioning format"))));
            }
            continue
        }

        let captures = semver_regex.captures(semver.as_str()).
        ok_or(Box::new(io::Error::new(io::ErrorKind::InvalidInput, "failed parsing input")))?;

        parsed_sem_vers.push(SemVer{
            major: *(&captures["major"].parse::<u16>()?),
            minor: *(&captures["minor"].parse::<u16>()?),
            patch: *(&captures["patch"].parse::<u16>()?),
            pre_release: captures.name("prerelease").map(|m|m.as_str().to_string()),
            build_number: captures.name("buildmetadata").map(|m|m.as_str().to_string()),
            has_v_prefix: has_v_prefix,
        });
    }

    match sort_order {
         SortOrder::Ascending => {
            parsed_sem_vers.sort();
        }
         SortOrder::Descending => {
            parsed_sem_vers.sort_by(|a, b| b.cmp(a));
        }
        _ => {}
    }


let buf = format!(r#"
local major
local minor
local patch
local prerelease
local buildmetadata 

if {query} then
    print("query")
end
"#);

    match parse(buf.as_str()) {
        Ok(block) => {
            //println!("statement length: {}", block.statements.len());
            match block.statements[5].clone() {
                If(if_statement) => {
                    //println!("the condition: {:?}", if_statement.condition);
                    let mut traversal_result = QueryTraversalResult::new();
                    traverse_bin_op_expression(if_statement.condition, &mut traversal_result);
                    if traversal_result.errors.len() > 0 {
                        return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, format!("error parsing query: {:?}", traversal_result.errors))));
                    }
                    // TODO: add an additional check here on variable names: only major, minor, patch are supported for now
                    let jsonpath_query = convert_to_jsonpath_syntax(&traversal_result);
                    // Parse the string of data into serde_json::Value.
                    let json = serde_json::to_string(&parsed_sem_vers)?;
                    let v: Value = serde_json::from_str(json.as_str())?;
                    let mut res_json: Vec<Value> =  v.query(jsonpath_query.as_str())?.iter().map(|v| (*v).clone()).collect();
                    if limit != 0 {
                        res_json.truncate(limit);
                    }
                    let mut final_result: Vec<String> = Vec::new();
                    for val in res_json {
                        final_result.push(serde_json::from_value::<SemVer>(val)?.to_string());
                    }

                    return Ok(final_result);
                }
                _ => {}
            }
        }
        Err(err) => {
            // print error only for debugging
             // println!("{}", err);
            return Err(Box::new(io::Error::new(io::ErrorKind::InvalidInput, "unable to parse query")));
        }
    }

    Ok(vec![String::new()])
}


fn traverse_bin_op_expression(node: Expr, traversal_result: &mut QueryTraversalResult)  {
    match node {
        Expr::BinOp(binop_expr) => {
              if let Expr::BinOp(_) = *binop_expr.left {
                match binop_expr.op.0.token.value.clone() {
                    Symbol(luaparse::token::Symbol::And) => {traversal_result.connectors.push(luaparse::token::Symbol::And)},
                    Symbol(luaparse::token::Symbol::Or) => {traversal_result.connectors.push(luaparse::token::Symbol::Or)},
                    x => {traversal_result.errors.push(format!("unsupported logical operator {:?}, only and/or are supported", x))}
                }
                
                match *binop_expr.right {
                    Expr::BinOp(_) => {}
                    _ => {
                      traversal_result.errors.push(format!("invalid expression {}", binop_expr.right.to_string()));
                    }
                }
              } else {
                match binop_expr.op.0.token.value.clone() {
                    Symbol(luaparse::token::Symbol::Greater) => {traversal_result.comparators.push(luaparse::token::Symbol::Greater)},
                    Symbol(luaparse::token::Symbol::GreaterEqual) => {traversal_result.comparators.push(luaparse::token::Symbol::GreaterEqual)},
                    Symbol(luaparse::token::Symbol::Less) => {traversal_result.comparators.push(luaparse::token::Symbol::Less)},
                    Symbol(luaparse::token::Symbol::LessEqual) => {traversal_result.comparators.push(luaparse::token::Symbol::LessEqual)},
                    Symbol(luaparse::token::Symbol::Equal) => {traversal_result.comparators.push(luaparse::token::Symbol::Equal)},
                    Symbol(luaparse::token::Symbol::NotEqual) => {traversal_result.comparators.push(luaparse::token::Symbol::NotEqual)},
                    Symbol(any) => {traversal_result.errors.push(format!("unsupported operator {}, only >, >=, <, <=, ==, ~= are supported", symb::to_string(&any)))}
                    _ => {traversal_result.errors.push(String::from("invalid syntax"))}
                }
              }

            traverse_bin_op_expression(*binop_expr.clone().left, traversal_result);
            traverse_bin_op_expression(*binop_expr.clone().right, traversal_result);
        }
        Expr::Prefix(prefix_exp) => {
            traversal_result.identifiers.push(prefix_exp.to_string())
        }
        Expr::Number(number_lit) => {
            traversal_result.literals.push(number_lit.to_string())
        }
        Expr::String(string) => {
            traversal_result.literals.push(string.to_string())
        }
        _ => {

        }
    }
}


fn convert_to_jsonpath_syntax(traversal_result: &QueryTraversalResult) -> String {
    let mut jsonpath_query = String::from("$[?");

    for i in 0..traversal_result.identifiers.len() {
        jsonpath_query.push_str("@.");
        jsonpath_query.push_str(traversal_result.identifiers[i].as_str());
        jsonpath_query.push_str(" ");
        jsonpath_query.push_str(comparator_to_jsonpath_string(traversal_result.comparators[i]));
        jsonpath_query.push_str(" ");
        jsonpath_query.push_str(traversal_result.literals[i].as_str());
        if i != traversal_result.identifiers.len() -1 {
            jsonpath_query.push_str(" ");
            jsonpath_query.push_str(lua_boolean_operator_to_jsonpath_string(traversal_result.connectors[i]));
            jsonpath_query.push_str(" ");
        }
    }

    jsonpath_query.push_str("]");
    jsonpath_query
}

fn lua_boolean_operator_to_jsonpath_string(symbol: luaparse::token::Symbol) -> &'static str {
    match symbol {
        luaparse::token::Symbol::And => {"&&"}
        luaparse::token::Symbol::Or => {"||"}
        _ => {"unknown"}
    }
} 

fn comparator_to_jsonpath_string(smbl: luaparse::token::Symbol) -> &'static str {
    if smbl == luaparse::token::Symbol::NotEqual {
        return "!="
    }

    return smbl.as_str()
}



#[cfg(test)]
mod tests {

    use std::{fs};

    use super::*;

    struct FailureTestCase {
        query: &'static str,
        static_input_data: Vec<&'static str>,
        error_message: Option<&'static str>,
    }

    #[test]
    fn invalid_query_returns_error() {
        let input_data = "0.1.1";
        let cases: Vec<FailureTestCase> = vec![FailureTestCase{
            query: "$%^",
            static_input_data: vec![input_data],
            error_message: Some("unable to parse query"),
        }, FailureTestCase{
            query: "a + b",
            static_input_data: vec![input_data],
            error_message: Some("error parsing query: [\"unsupported operator `+`, only >, >=, <, <=, ==, ~= are supported\"]"),
        }, FailureTestCase{
            query: "minor += major",
            static_input_data: vec![input_data],
            error_message: Some("unable to parse query"),
        }, FailureTestCase{
            query: "minor === major",
            static_input_data: vec![input_data],
            error_message: Some("unable to parse query"),
        }
        ];
        for i in 0..cases.len() {
            let input: Vec<String> = cases[i].static_input_data.iter().map(|x|String::from(*x)).collect();
            match query_semver(&cases[i].query.to_string(), input, true, SortOrder::None, 0) {
                Ok(_) => {
                    assert!(false, "case {} failed: error is expected for query: {}", i, cases[i].query)
                },
                Err(err) => {
                    assert_eq!(err.to_string(), cases[i].error_message.unwrap(), "case {} failed", i)
                }
            }
        }
    }

    #[test]
    fn invalid_semver_format_returns_error() {
        let query = "major >= 1";
        let cases: Vec<FailureTestCase> = vec![FailureTestCase{
            query: query,
            static_input_data: vec!["foo bar"],
            error_message: Some("foo bar does not follow the semantic versioning format"),

        }, FailureTestCase{
            query: query,
            static_input_data: vec!["x.1.0"],
            error_message: Some("x.1.0 does not follow the semantic versioning format"),

        }, FailureTestCase{
            query: query,
            static_input_data: vec!["1.x.2"],
            error_message: Some("1.x.2 does not follow the semantic versioning format"),
        }, FailureTestCase{
            query: query,
            static_input_data: vec!["1.2.x"],
            error_message: Some("1.2.x does not follow the semantic versioning format"),
        }
        ];
        for i in 0..cases.len() {
            let input: Vec<String> = cases[i].static_input_data.iter().map(|x|String::from(*x)).collect();
            match query_semver(&cases[i].query.to_string(), input, true, SortOrder::None, 0) {
                Ok(_) => {
                    assert!(false, "case {} failed: error is expected for query: {}", i, cases[i].query)
                },
                Err(err) => {
                    assert_eq!(err.to_string(), cases[i].error_message.unwrap(), "case {} failed", i)
                }
            }
        }
    }

    #[test]
    fn queries_set_1() -> Result<(), Box<dyn Error>>{
      let input_data  = fs::read_to_string("src/test_data/keycloak/input.txt")?;
      let input_set: Vec<String> = input_data.lines().map(|ln|String::from(ln)).collect();

        let queries: Vec<&str> = vec![
            "major >= 20",
            "major <= 20 and minor > 0",
            "major == 26 and minor > 0 and patch > 0",
            "major >= 23 and major <= 26 and minor > 0",
            "major >= 17 and major <= 20 and patch > 1 and patch <= 3",
        ];
        for i in 0..queries.len() {
            for order in [SortOrder::None, SortOrder::Ascending, SortOrder::Descending] {
                let expectation_file = fs::read_to_string(format!("src/test_data/keycloak/case_{i}_expectation_{order}.txt"))?;
                let expected_result: Vec<String> = expectation_file.lines().map(|ln|String::from(ln)).collect();

                match query_semver(&String::from(queries[i]),
                input_set.clone(), true, order, 0) {
                    Ok(actual_result) => {
                        assert_eq!(actual_result, expected_result, "case {} failed (sort: {}): expected: {:?}, got: {:?}", i, order, expected_result, actual_result);
                        //assert!(actual_result.is_sorted())
                    },
                    Err(err) => {
                        assert!(false, "case {} failed: error occurred: {}", i, err);
                    }
                }
          }
        }
        Ok(())
    }

     #[test]
    fn queries_set_2() -> Result<(), Box<dyn Error>> {
      let input_data  = fs::read_to_string("src/test_data/kubernetes/input.txt")?;
      let input_set: Vec<String> = input_data.lines().map(|ln|String::from(ln)).collect();

        let queries: Vec<&str> = vec![
            "minor >= 30",
            "minor >= 29 and patch > 0",
            "minor == 34",
            "minor == 29 and pre_release ~= 'alpha.0' and pre_release ~= 'alpha.1' and pre_release ~= 'alpha.2' and pre_release ~= 'alpha.3'",
            "patch == 7",
        ];
        for i in 0..queries.len() {
            for order in [SortOrder::None, SortOrder::Ascending, SortOrder::Descending] {
                let expectation_file = fs::read_to_string(format!("src/test_data/kubernetes/case_{i}_expectation_{order}.txt"))?;
                let expected_result: Vec<String> = expectation_file.lines().map(|ln|String::from(ln)).collect();

                match query_semver(&String::from(queries[i]),
                input_set.clone(), true, order, 0) {
                    Ok(actual_result) => {
                        assert_eq!(actual_result, expected_result, "case {} failed (sort: {}): expected: {:?}, got: {:?}", i, order, 
                        expected_result, actual_result);
                    },
                    Err(err) => {
                        assert!(false, "case {} failed: error occurred: {}", i, err);
                    }
                }
            }
        }
        Ok(())
    }

    #[test]
    fn queries_set_3() -> Result<(), Box<dyn Error>>{
      let input_data  = fs::read_to_string("src/test_data/tensorflow/input.txt")?;
      let input_set: Vec<String> = input_data.lines().map(|ln|String::from(ln)).collect();

        let queries: Vec<&str> = vec![
            "major == 2 and minor == 0",
            "major == 1 and minor == 15 and patch > 0",
            "major == 2 and minor >= 7 and minor <= 9",
            "minor == 17 and patch > 0",
        ];
        for i in 0..queries.len() {
            for order in [SortOrder::None, SortOrder::Ascending, SortOrder::Descending] {
                let expectation_file = fs::read_to_string(format!("src/test_data/tensorflow/case_{i}_expectation_{order}.txt"))?;
                let expected_result: Vec<String> = expectation_file.lines().map(|ln|String::from(ln)).collect();

                match query_semver(&String::from(queries[i]),
                input_set.clone(), true, order, 0) {
                    Ok(actual_result) => {
                        assert_eq!(actual_result, expected_result, "case {} failed (sort: {}): expected: {:?}, got: {:?}", i, order, expected_result, actual_result);
                    },
                    Err(err) => {
                        assert!(false, "case {} failed: error occurred: {}", i, err);
                    }
                }
            }
        }
        Ok(())
    }

       #[test]
    fn with_limit() -> Result<(), Box<dyn Error>>{
      let input_data  = fs::read_to_string("src/test_data/kubernetes/input.txt")?;
      let input_set: Vec<String> = input_data.lines().map(|ln|String::from(ln)).collect();

        let limits = vec![10, 5, 13, 7, 16];
        for i in 0..limits.len() {
            let expected_limit = limits[i];
                match query_semver(&String::from("major == 1"),
                input_set.clone(), true, SortOrder::None, expected_limit) {
                    Ok(actual_result) => {
                        assert_eq!(actual_result.len(), limits[i], "case {} failed: expected result size: {}, got: {}", i, limits[i], actual_result.len());
                    },
                    Err(err) => {
                        assert!(false, "case {} failed: error occurred: {}", i, err);
                    }
            }
        }
        Ok(())
    }
}