From 7d732a051e695353db5a3ddbb26ab766ff736043 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 14 Jan 2022 18:09:42 +0530 Subject: refactor parsing code, simplify tests --- lib/src/lints/faster_groupby.rs | 4 ++-- lib/src/session.rs | 39 +++++++++++++++------------------------ 2 files changed, 17 insertions(+), 26 deletions(-) (limited to 'lib/src') diff --git a/lib/src/lints/faster_groupby.rs b/lib/src/lints/faster_groupby.rs index c496125..2a36f5f 100644 --- a/lib/src/lints/faster_groupby.rs +++ b/lib/src/lints/faster_groupby.rs @@ -27,7 +27,7 @@ use rnix::{ /// /// Replace `lib.groupBy` with `builtins.groupBy`: /// -/// ``` +/// ```nix /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; /// ``` #[lint( @@ -40,7 +40,7 @@ struct FasterGroupBy; impl Rule for FasterGroupBy { fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option { - let lint_version = "nix (Nix) 2.5".parse::().unwrap(); + let lint_version = "2.5".parse::().unwrap(); if_chain! { if sess.version() >= &lint_version; if let NodeOrToken::Node(node) = node; diff --git a/lib/src/session.rs b/lib/src/session.rs index 8d142ec..0fb8ae2 100644 --- a/lib/src/session.rs +++ b/lib/src/session.rs @@ -29,20 +29,15 @@ fn parse_number(s: &str) -> Option { } fn parse_version(s: &str) -> Option { - match s.split(' ').collect::>().as_slice() { - [_, _, version] => { - let mut parts = version.split('.'); - let major = parse_number(parts.next()?)?; - let minor = parse_number(parts.next()?)?; - let patch = parts.next().map(|p| parse_number(p)).flatten(); - Some(Version { - major, - minor, - patch, - }) - } - _ => None, - } + let mut parts = s.split('.'); + let major = parse_number(parts.next()?)?; + let minor = parse_number(parts.next()?)?; + let patch = parts.next().map(|p| parse_number(p)).flatten(); + Some(Version { + major, + minor, + patch, + }) } impl FromStr for Version { @@ -67,37 +62,33 @@ impl SessionInfo { } } -pub fn get_nix_version() -> Option { - "nix (Nix) 2.4pre20211006_53e4794".parse::().ok() -} - #[cfg(test)] mod tests { use super::*; #[test] fn parse_trivial() { - let v = "nix (Nix) 1.6.1".parse::().ok(); + let v = "1.6.1".parse::().ok(); assert!(v.is_some()) } #[test] fn parse() { - let v = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + let v = "2.4pre20211006_53e4794".parse::().ok(); assert!(v.is_some()) } #[test] fn compare_trivial() { - let v1 = "nix (Nix) 1.6.1".parse::().ok(); - let v2 = "nix (Nix) 1.7.2".parse::().ok(); + let v1 = "1.6.1".parse::().ok(); + let v2 = "1.7.2".parse::().ok(); assert!(v2 > v1); } #[test] fn compare() { - let v1 = "nix (Nix) 1.7".parse::().ok(); - let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + let v1 = "1.7".parse::().ok(); + let v2 = "2.4pre20211006_53e4794".parse::().ok(); assert!(v2 >= v1); } } -- cgit v1.2.3