Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
GJKrupa committed Oct 4, 2023
1 parent 4c4dc67 commit a757a77
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use clap::Parser;
use crate::rules::{parse_rules, VersionMatch};
use crate::sdkman::{SdkMan, ToolManager};

pub mod sdkman;
pub mod rules;
pub mod sdkman;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -28,13 +28,16 @@ struct Args {
fn main() {
let args = Args::parse();

let sdkman = SdkMan{
let sdkman = SdkMan {
dry_run: args.dry_run,
no_uninstall: args.no_uninstall
no_uninstall: args.no_uninstall,
};
let rules = parse_rules(fs::read_to_string(args.file).expect("Failed to read input file"));
for (name, candidate) in rules.expect("Rules file could not be parsed").candidates {
let installed: HashSet<_> = sdkman.installed_versions(name.clone()).into_iter().collect();
let installed: HashSet<_> = sdkman
.installed_versions(name.clone())
.into_iter()
.collect();
let available = sdkman.available_versions(name.clone());
let mut required: HashSet<String> = HashSet::new();
let mut default: Option<String> = None;
Expand Down
50 changes: 29 additions & 21 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use regex::Regex;
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_yaml::{self, Error};
Expand All @@ -8,7 +8,7 @@ use serde_yaml::{self, Error};
pub struct Version {
pub pattern: String,
pub default: Option<bool>,
pub exclude: Option<Vec<String>>
pub exclude: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
Expand All @@ -18,10 +18,10 @@ pub struct Candidate {

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Rules {
pub candidates: HashMap<String,Candidate>
pub candidates: HashMap<String, Candidate>,
}

pub fn parse_rules(data: String) -> Result<Rules,Error> {
pub fn parse_rules(data: String) -> Result<Rules, Error> {
serde_yaml::from_str(&data)
}

Expand All @@ -31,15 +31,19 @@ pub trait VersionMatch {

impl VersionMatch for Version {
fn get_matching(&self, name: String, available: Vec<String>) -> Option<String> {
let pattern = Regex::new(self.pattern.as_str()).expect(format!("Invalid regex for {}: {}", name, self.pattern).as_str());
let mut matches: Vec<String> = available.iter()
let pattern = Regex::new(self.pattern.as_str())
.expect(format!("Invalid regex for {}: {}", name, self.pattern).as_str());

Check failure on line 35 in src/rules/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

use of `expect` followed by a function call

error: use of `expect` followed by a function call --> src/rules/mod.rs:35:14 | 35 | .expect(format!("Invalid regex for {}: {}", name, self.pattern).as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| panic!("Invalid regex for {}: {}", name, self.pattern))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call = note: `-D clippy::expect-fun-call` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::expect_fun_call)]`
let mut matches: Vec<String> = available
.iter()
.filter(|it| pattern.is_match(it))
.map(|it| it.to_string())
.collect();

if let Some(exclude) = self.exclude.as_ref() {
let exclude_pattern = Regex::new(exclude.join("|").as_str()).expect(format!("Invalid regex for {}: {}", name, exclude.join("|")).as_str());
matches = matches.into_iter()
let exclude_pattern = Regex::new(exclude.join("|").as_str())
.expect(format!("Invalid regex for {}: {}", name, exclude.join("|")).as_str());

Check failure on line 44 in src/rules/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

use of `expect` followed by a function call

error: use of `expect` followed by a function call --> src/rules/mod.rs:44:18 | 44 | .expect(format!("Invalid regex for {}: {}", name, exclude.join("|")).as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| panic!("Invalid regex for {}: {}", name, exclude.join("|")))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
matches = matches
.into_iter()
.filter(|it| !exclude_pattern.is_match(it))
.collect();

Check failure on line 48 in src/rules/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression can be written more simply using `.retain()`

error: this expression can be written more simply using `.retain()` --> src/rules/mod.rs:45:13 | 45 | / matches = matches 46 | | .into_iter() 47 | | .filter(|it| !exclude_pattern.is_match(it)) 48 | | .collect(); | |__________________________^ help: consider calling `.retain()` instead: `matches.retain(|it| !exclude_pattern.is_match(it))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_retain = note: `-D clippy::manual-retain` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::manual_retain)]`
}
Expand Down Expand Up @@ -68,7 +72,8 @@ candidates:
exclude:
- \".*alpha.*\"
- \".*-rc.*\"
default: true".to_string();
default: true"
.to_string();
let expected: Rules = Rules {
candidates: hashmap! {
"java".to_string() => Candidate{
Expand Down Expand Up @@ -97,7 +102,7 @@ candidates:
}
]
}
}
},
};

match parse_rules(input) {
Expand All @@ -111,45 +116,48 @@ candidates:

#[test]
fn versions_matched() {
let version = Version{
let version = Version {
pattern: "^21.*$".to_string(),
default: None,
exclude: Some(vec![
"-zulu".to_string(),
"-graalce".to_string(),
])
exclude: Some(vec!["-zulu".to_string(), "-graalce".to_string()]),
};
let available = vec![
"21.0.0-zulu".to_string(),
"21.0.0-graalce".to_string(),
"21.0.0-amzn".to_string(),
"21.0.0".to_string(),
];
assert_eq!(version.get_matching("java".to_string(), available), Some("21.0.0-amzn".to_string()));
assert_eq!(
version.get_matching("java".to_string(), available),
Some("21.0.0-amzn".to_string())
);
}

#[test]
fn versions_matched_with_no_exclusion() {
let version = Version{
let version = Version {
pattern: "^21.*$".to_string(),
default: None,
exclude: None
exclude: None,
};
let available = vec![
"21.0.0-zulu".to_string(),
"21.0.0-graalce".to_string(),
"21.0.0-amzn".to_string(),
"21.0.0".to_string(),
];
assert_eq!(version.get_matching("java".to_string(), available), Some("21.0.0-zulu".to_string()));
assert_eq!(
version.get_matching("java".to_string(), available),
Some("21.0.0-zulu".to_string())
);
}

#[test]
fn versions_not_matched() {
let version = Version{
let version = Version {
pattern: "^11.*$".to_string(),
default: None,
exclude: None
exclude: None,
};
let available = vec![
"21.0.0-zulu".to_string(),
Expand Down
33 changes: 24 additions & 9 deletions src/sdkman/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ mod private {
if equals_pattern.is_match(line) {
equals += 1;
} else if equals == 2 {
let columns: Vec<&str> = line.split_whitespace()
let columns: Vec<&str> = line
.split_whitespace()
.filter(|word| *word != "*" && *word != ">")
.collect();

Expand Down Expand Up @@ -83,9 +84,10 @@ mod test {

#[test]
fn sample_kotlin_versions_parsed_in_order() {
let candidate = SdkManCandidate{
let candidate = SdkManCandidate {
name: "kotlin".to_string(),
output: "================================================================================
output:
"================================================================================
Available Kotlin Versions
================================================================================
> * 1.9.0 1.4.20 1.2.70 1.1.4
Expand All @@ -95,17 +97,21 @@ Available Kotlin Versions
+ - local version
* - installed
> - currently in use
================================================================================".to_string()
================================================================================"
.to_string(),
};
let expected = vec!["1.9.0", "1.8.20", "1.4.20", "1.4.10", "1.2.70", "1.2.61", "1.1.4", "1.1.3-2"];
let expected = vec![
"1.9.0", "1.8.20", "1.4.20", "1.4.10", "1.2.70", "1.2.61", "1.1.4", "1.1.3-2",
];
assert_eq!(candidate.available_versions(), expected);
}

#[test]
fn sample_java_versions_parsed_in_order() {
let candidate = SdkManCandidate{
let candidate = SdkManCandidate {
name: "java".to_string(),
output: "================================================================================
output:
"================================================================================
Available Java Versions for macOS ARM 64bit
================================================================================
Vendor | Use | Version | Dist | Status | Identifier
Expand All @@ -125,9 +131,18 @@ Use TAB completion to discover available versions
Or install a specific version by Identifier:
$ sdk install java 17.0.8.1-tem
Hit Q to exit this list view
================================================================================".to_string()
================================================================================"
.to_string(),
};
let expected = vec!["21-amzn", "20.0.2-amzn", "22.1.0.1.r17-gln", "22.1.0.1.r11-gln", "21-graalce", "20.0.2-graalce", "20.0.1-graalce"];
let expected = vec![
"21-amzn",
"20.0.2-amzn",
"22.1.0.1.r17-gln",
"22.1.0.1.r11-gln",
"21-graalce",
"20.0.2-graalce",
"20.0.1-graalce",
];
assert_eq!(candidate.available_versions(), expected);
}
}
15 changes: 7 additions & 8 deletions src/sdkman/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{env, io};
use crate::sdkman::candidate::{Candidate, SdkManCandidate};
use std::fs::read_dir;
use std::io::Write;
use crate::sdkman::candidate::{Candidate, SdkManCandidate};
use std::{env, io};

mod candidate;

Expand All @@ -15,7 +15,7 @@ pub trait ToolManager {

pub struct SdkMan {
pub dry_run: bool,
pub no_uninstall: bool
pub no_uninstall: bool,
}

impl ToolManager for SdkMan {
Expand All @@ -32,11 +32,11 @@ impl ToolManager for SdkMan {
} else {
Vec::new()
}
},
}
Err(_) => {
panic!("SDKMAN_DIR not set. Is SDKMAN installed?")
}
}
};
}

fn available_versions(&self, candidate: String) -> Vec<String> {
Expand All @@ -50,7 +50,8 @@ impl ToolManager for SdkMan {
SdkManCandidate {
name: candidate,
output: String::from_utf8(output.stdout).unwrap(),
}.available_versions()
}
.available_versions()
}

fn install(&self, candidate: String, version: String) {
Expand Down Expand Up @@ -121,5 +122,3 @@ impl ToolManager for SdkMan {
}
}
}


0 comments on commit a757a77

Please sign in to comment.