diff options
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/lints/faster_groupby.rs | 4 | ||||
-rw-r--r-- | lib/src/session.rs | 39 |
2 files changed, 17 insertions, 26 deletions
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::{ | |||
27 | /// | 27 | /// |
28 | /// Replace `lib.groupBy` with `builtins.groupBy`: | 28 | /// Replace `lib.groupBy` with `builtins.groupBy`: |
29 | /// | 29 | /// |
30 | /// ``` | 30 | /// ```nix |
31 | /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; | 31 | /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; |
32 | /// ``` | 32 | /// ``` |
33 | #[lint( | 33 | #[lint( |
@@ -40,7 +40,7 @@ struct FasterGroupBy; | |||
40 | 40 | ||
41 | impl Rule for FasterGroupBy { | 41 | impl Rule for FasterGroupBy { |
42 | fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { | 42 | fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { |
43 | let lint_version = "nix (Nix) 2.5".parse::<Version>().unwrap(); | 43 | let lint_version = "2.5".parse::<Version>().unwrap(); |
44 | if_chain! { | 44 | if_chain! { |
45 | if sess.version() >= &lint_version; | 45 | if sess.version() >= &lint_version; |
46 | if let NodeOrToken::Node(node) = node; | 46 | 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<u16> { | |||
29 | } | 29 | } |
30 | 30 | ||
31 | fn parse_version(s: &str) -> Option<Version> { | 31 | fn parse_version(s: &str) -> Option<Version> { |
32 | match s.split(' ').collect::<Vec<_>>().as_slice() { | 32 | let mut parts = s.split('.'); |
33 | [_, _, version] => { | 33 | let major = parse_number(parts.next()?)?; |
34 | let mut parts = version.split('.'); | 34 | let minor = parse_number(parts.next()?)?; |
35 | let major = parse_number(parts.next()?)?; | 35 | let patch = parts.next().map(|p| parse_number(p)).flatten(); |
36 | let minor = parse_number(parts.next()?)?; | 36 | Some(Version { |
37 | let patch = parts.next().map(|p| parse_number(p)).flatten(); | 37 | major, |
38 | Some(Version { | 38 | minor, |
39 | major, | 39 | patch, |
40 | minor, | 40 | }) |
41 | patch, | ||
42 | }) | ||
43 | } | ||
44 | _ => None, | ||
45 | } | ||
46 | } | 41 | } |
47 | 42 | ||
48 | impl FromStr for Version { | 43 | impl FromStr for Version { |
@@ -67,37 +62,33 @@ impl SessionInfo { | |||
67 | } | 62 | } |
68 | } | 63 | } |
69 | 64 | ||
70 | pub fn get_nix_version() -> Option<Version> { | ||
71 | "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok() | ||
72 | } | ||
73 | |||
74 | #[cfg(test)] | 65 | #[cfg(test)] |
75 | mod tests { | 66 | mod tests { |
76 | use super::*; | 67 | use super::*; |
77 | 68 | ||
78 | #[test] | 69 | #[test] |
79 | fn parse_trivial() { | 70 | fn parse_trivial() { |
80 | let v = "nix (Nix) 1.6.1".parse::<Version>().ok(); | 71 | let v = "1.6.1".parse::<Version>().ok(); |
81 | assert!(v.is_some()) | 72 | assert!(v.is_some()) |
82 | } | 73 | } |
83 | 74 | ||
84 | #[test] | 75 | #[test] |
85 | fn parse() { | 76 | fn parse() { |
86 | let v = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); | 77 | let v = "2.4pre20211006_53e4794".parse::<Version>().ok(); |
87 | assert!(v.is_some()) | 78 | assert!(v.is_some()) |
88 | } | 79 | } |
89 | 80 | ||
90 | #[test] | 81 | #[test] |
91 | fn compare_trivial() { | 82 | fn compare_trivial() { |
92 | let v1 = "nix (Nix) 1.6.1".parse::<Version>().ok(); | 83 | let v1 = "1.6.1".parse::<Version>().ok(); |
93 | let v2 = "nix (Nix) 1.7.2".parse::<Version>().ok(); | 84 | let v2 = "1.7.2".parse::<Version>().ok(); |
94 | assert!(v2 > v1); | 85 | assert!(v2 > v1); |
95 | } | 86 | } |
96 | 87 | ||
97 | #[test] | 88 | #[test] |
98 | fn compare() { | 89 | fn compare() { |
99 | let v1 = "nix (Nix) 1.7".parse::<Version>().ok(); | 90 | let v1 = "1.7".parse::<Version>().ok(); |
100 | let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); | 91 | let v2 = "2.4pre20211006_53e4794".parse::<Version>().ok(); |
101 | assert!(v2 >= v1); | 92 | assert!(v2 >= v1); |
102 | } | 93 | } |
103 | } | 94 | } |