Skip to content

Commit

Permalink
Cargo fmt and add to CI (fixes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Jan 23, 2024
1 parent dd03ce5 commit 92a31dc
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 78 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
- name: Format check
run: cargo fmt --check
- name: Test
run: cargo test
env:
Expand Down
20 changes: 9 additions & 11 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::mem;
use crate::Flags;
use crate::component::Component;
use crate::parse::{SomeComponents, get_next_version_component};

pub enum IteratedComponent {
use crate::parse::{get_next_version_component, SomeComponents};
use crate::Flags;
use std::mem;

}
pub enum IteratedComponent {}

pub struct VersionComponentIterator<'a> {
rest_of_version: &'a str,
Expand All @@ -16,11 +14,11 @@ pub struct VersionComponentIterator<'a> {

impl VersionComponentIterator<'_> {
pub fn new<'a>(version: &'a str, flags: Flags) -> VersionComponentIterator<'a> {
return VersionComponentIterator{
return VersionComponentIterator {
rest_of_version: version,
needs_trailing_component: flags.contains(Flags::LowerBound | Flags::UpperBound),
carried_component: None,
flags
flags,
};
}

Expand All @@ -29,14 +27,15 @@ impl VersionComponentIterator<'_> {
return component;
}

let (components, rest_of_version) = get_next_version_component(self.rest_of_version, self.flags);
let (components, rest_of_version) =
get_next_version_component(self.rest_of_version, self.flags);

self.rest_of_version = rest_of_version;

match components {
SomeComponents::One(component) => {
return component;
},
}
SomeComponents::Two(component1, component2) => {
self.carried_component = Some(component2);
return component1;
Expand All @@ -48,4 +47,3 @@ impl VersionComponentIterator<'_> {
return self.rest_of_version.is_empty() && self.carried_component.is_none();
}
}

12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use bitflags::bitflags;
use crate::iter::VersionComponentIterator;
use crate::compare::compare_components;
use crate::iter::VersionComponentIterator;
use bitflags::bitflags;

mod string;
mod parse;
mod component;
mod compare;
mod component;
mod iter;
mod parse;
mod string;

bitflags! {
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn version_compare4(v1: &str, v2: &str, v1_flags: Flags, v2_flags: Flags) ->
}

pub fn version_compare2(v1: &str, v2: &str) -> i8 {
return version_compare4(v1, v2, Flags::empty(), Flags::empty())
return version_compare4(v1, v2, Flags::empty(), Flags::empty());
}

#[cfg(test)]
Expand Down
131 changes: 94 additions & 37 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::string::*;
use crate::component::*;
use crate::string::*;
use crate::Flags;

#[derive(PartialEq, Debug)]
Expand All @@ -22,7 +22,8 @@ pub fn classify_keyword(s: &str, flags: Flags) -> KeywordClass {
return KeywordClass::PostRelease;
} else if string_has_prefix_ci(s, "patch") {
return KeywordClass::PostRelease;
} else if strings_are_equal_ci(s, "pl") { // patchlevel
} else if strings_are_equal_ci(s, "pl") {
// patchlevel
return KeywordClass::PostRelease;
} else if strings_are_equal_ci(s, "errata") {
return KeywordClass::PostRelease;
Expand All @@ -36,39 +37,48 @@ pub fn parse_token_to_component(s: &str, flags: Flags) -> (Component, &str) {
if is_alpha(s.chars().nth(0).unwrap()) {
let (alpha, rest) = split_alpha(s);
return (
Component{
Component {
precedence: match classify_keyword(alpha, flags) {
KeywordClass::Unknown => if flags.contains(Flags::AnyIsPatch) { ComponentPrecedence::PostRelease } else { ComponentPrecedence::PreRelease },
KeywordClass::Unknown => {
if flags.contains(Flags::AnyIsPatch) {
ComponentPrecedence::PostRelease
} else {
ComponentPrecedence::PreRelease
}
}
KeywordClass::PreRelease => ComponentPrecedence::PreRelease,
KeywordClass::PostRelease => ComponentPrecedence::PostRelease,
},
value: alpha,
},
rest
)
rest,
);
} else {
let s = skip_zeroes(s);
let (number, rest) = split_number(s);
return (
Component{
precedence: if number.is_empty() { ComponentPrecedence::Zero } else { ComponentPrecedence::NonZero },
Component {
precedence: if number.is_empty() {
ComponentPrecedence::Zero
} else {
ComponentPrecedence::NonZero
},
value: number,
},
rest
)
rest,
);
}
}

pub fn make_default_component(flags: Flags) -> Component<'static> {
return Component{
precedence:
if flags.contains(Flags::LowerBound) {
ComponentPrecedence::LowerBound
} else if flags.contains(Flags::UpperBound) {
ComponentPrecedence::UpperBound
} else {
ComponentPrecedence::Zero
},
return Component {
precedence: if flags.contains(Flags::LowerBound) {
ComponentPrecedence::LowerBound
} else if flags.contains(Flags::UpperBound) {
ComponentPrecedence::UpperBound
} else {
ComponentPrecedence::Zero
},
value: "",
};
}
Expand All @@ -89,20 +99,25 @@ pub fn get_next_version_component(s: &str, flags: Flags) -> (SomeComponents, &st

let (alpha, rest_after_alpha) = split_alpha(rest);

if !alpha.is_empty() && !rest_after_alpha.chars().nth(0).is_some_and(|c| is_number(c)) {
if !alpha.is_empty()
&& !rest_after_alpha
.chars()
.nth(0)
.is_some_and(|c| is_number(c))
{
return (
SomeComponents::Two(
component,
Component{
Component {
precedence: match classify_keyword(alpha, flags) {
KeywordClass::Unknown => ComponentPrecedence::LetterSuffix,
KeywordClass::PreRelease => ComponentPrecedence::PreRelease,
KeywordClass::PostRelease => ComponentPrecedence::PostRelease,
},
value: alpha
value: alpha,
},
),
rest_after_alpha
rest_after_alpha,
);
}

Expand All @@ -115,20 +130,62 @@ mod tests {

#[test]
fn test_classify_keyword() {
assert_eq!(classify_keyword("ALPHA", Flags::empty()), KeywordClass::PreRelease);
assert_eq!(classify_keyword("ALPHABET", Flags::empty()), KeywordClass::Unknown);
assert_eq!(classify_keyword("BETA", Flags::empty()), KeywordClass::PreRelease);
assert_eq!(classify_keyword("BETAKE", Flags::empty()), KeywordClass::Unknown);
assert_eq!(classify_keyword("RC", Flags::empty()), KeywordClass::PreRelease);
assert_eq!(classify_keyword("PRE", Flags::empty()), KeywordClass::PreRelease);
assert_eq!(classify_keyword("PRERELEASE", Flags::empty()), KeywordClass::PreRelease);
assert_eq!(classify_keyword("POST", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(classify_keyword("POSTRELEASE", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(classify_keyword("PATCH", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(classify_keyword("PATCHLEVEL", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(classify_keyword("PL", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(classify_keyword("ERRATA", Flags::empty()), KeywordClass::PostRelease);
assert_eq!(
classify_keyword("ALPHA", Flags::empty()),
KeywordClass::PreRelease
);
assert_eq!(
classify_keyword("ALPHABET", Flags::empty()),
KeywordClass::Unknown
);
assert_eq!(
classify_keyword("BETA", Flags::empty()),
KeywordClass::PreRelease
);
assert_eq!(
classify_keyword("BETAKE", Flags::empty()),
KeywordClass::Unknown
);
assert_eq!(
classify_keyword("RC", Flags::empty()),
KeywordClass::PreRelease
);
assert_eq!(
classify_keyword("PRE", Flags::empty()),
KeywordClass::PreRelease
);
assert_eq!(
classify_keyword("PRERELEASE", Flags::empty()),
KeywordClass::PreRelease
);
assert_eq!(
classify_keyword("POST", Flags::empty()),
KeywordClass::PostRelease
);
assert_eq!(
classify_keyword("POSTRELEASE", Flags::empty()),
KeywordClass::PostRelease
);
assert_eq!(
classify_keyword("PATCH", Flags::empty()),
KeywordClass::PostRelease
);
assert_eq!(
classify_keyword("PATCHLEVEL", Flags::empty()),
KeywordClass::PostRelease
);
assert_eq!(
classify_keyword("PL", Flags::empty()),
KeywordClass::PostRelease
);
assert_eq!(
classify_keyword("ERRATA", Flags::empty()),
KeywordClass::PostRelease
);

assert_eq!(classify_keyword("FOOBAR", Flags::empty()), KeywordClass::Unknown);
assert_eq!(
classify_keyword("FOOBAR", Flags::empty()),
KeywordClass::Unknown
);
}
}
6 changes: 3 additions & 3 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub fn strings_are_equal_ci(a: &str, b: &str) -> bool {
if to_lower(a_val) != to_lower(b_val) {
return false;
}
},
}
(None, None) => {
return true;
},
}
_ => {
return false;
},
}
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions tests/suite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libversion::*;
use suite_parser::*;
use std::env;
use suite_parser::*;

mod suite_parser;

Expand All @@ -9,15 +9,15 @@ fn parse_flags(flags: &str) -> Flags {

for flag in flags.chars() {
res |= match flag {
'p' => Flags::PIsPatch,
'a' => Flags::AnyIsPatch,
'l' => Flags::LowerBound,
'u' => Flags::UpperBound,
'p' => Flags::PIsPatch,
'a' => Flags::AnyIsPatch,
'l' => Flags::LowerBound,
'u' => Flags::UpperBound,
_ => Flags::empty(),
}
}

return res
return res;
}

fn parse_op(op: &str) -> i8 {
Expand Down Expand Up @@ -55,7 +55,11 @@ fn version_comparison_test_suite() {
left_flags,
right_flags,
);
println!("{} {}", if result == expected { "OK" } else { "FAILED" }, case.text);
println!(
"{} {}",
if result == expected { "OK" } else { "FAILED" },
case.text
);
assert!(
result == expected,
"Test suite case {}:{}: {} failed with unexpected result {}",
Expand Down
27 changes: 13 additions & 14 deletions tests/suite_parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use regex::Regex;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
use regex::Regex;

#[derive(Debug)]
pub struct TestCase {
Expand All @@ -25,18 +25,17 @@ pub fn parse_test_suite(path: &str) -> Vec<TestCase> {

for (line_number, line) in reader.lines().map(|line| line.unwrap()).enumerate() {
if let Some(captures) = re.captures(&line) {
let (_, [left_version, left_flags, expected_result, right_flags, right_version]) = captures.extract();
cases.push(
TestCase{
line_number: line_number + 1,
text: line.to_string(),
left_version: left_version.to_string(),
left_flags: left_flags.to_string(),
expected_result: expected_result.to_string(),
right_flags: right_flags.to_string(),
right_version: right_version.to_string(),
}
);
let (_, [left_version, left_flags, expected_result, right_flags, right_version]) =
captures.extract();
cases.push(TestCase {
line_number: line_number + 1,
text: line.to_string(),
left_version: left_version.to_string(),
left_flags: left_flags.to_string(),
expected_result: expected_result.to_string(),
right_flags: right_flags.to_string(),
right_version: right_version.to_string(),
});
}
}

Expand Down

0 comments on commit 92a31dc

Please sign in to comment.