diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d8b388..472a5ab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/src/iter.rs b/src/iter.rs index 4a57d40..ac2a3f1 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -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, @@ -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, }; } @@ -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; @@ -48,4 +47,3 @@ impl VersionComponentIterator<'_> { return self.rest_of_version.is_empty() && self.carried_component.is_none(); } } - diff --git a/src/lib.rs b/src/lib.rs index 439972a..d2b5c0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] @@ -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)] diff --git a/src/parse.rs b/src/parse.rs index faee9f9..04ee72d 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,5 +1,5 @@ -use crate::string::*; use crate::component::*; +use crate::string::*; use crate::Flags; #[derive(PartialEq, Debug)] @@ -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; @@ -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: "", }; } @@ -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, ); } @@ -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 + ); } } diff --git a/src/string.rs b/src/string.rs index 27f35bf..349d2c4 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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; - }, + } } } } diff --git a/tests/suite.rs b/tests/suite.rs index 6be38a8..6b69eb0 100644 --- a/tests/suite.rs +++ b/tests/suite.rs @@ -1,6 +1,6 @@ use libversion::*; -use suite_parser::*; use std::env; +use suite_parser::*; mod suite_parser; @@ -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 { @@ -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 {}", diff --git a/tests/suite_parser/mod.rs b/tests/suite_parser/mod.rs index f67912c..1bd8900 100644 --- a/tests/suite_parser/mod.rs +++ b/tests/suite_parser/mod.rs @@ -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 { @@ -25,18 +25,17 @@ pub fn parse_test_suite(path: &str) -> Vec { 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(), + }); } }