diff --git a/src/alias.rs b/src/alias.rs index 617dcbf..29f8789 100644 --- a/src/alias.rs +++ b/src/alias.rs @@ -2,7 +2,7 @@ use crate::version::NodeVersion; use std::collections::HashMap; use std::path::{Path, PathBuf}; -pub fn pretty_path_name<'a>(path: &'a PathBuf) -> &'a str { +pub fn pretty_path_name(path: &'_ Path) -> &'_ str { path.file_name().unwrap().to_str().unwrap() } @@ -57,7 +57,7 @@ impl Alias { Ok(aliases) } - pub fn hashmap<'a, P: AsRef>(path: P) -> anyhow::Result>> { + pub fn hashmap>(path: P) -> anyhow::Result>> { let list = std::fs::read_dir(&path)?; let mut aliases: HashMap> = HashMap::new(); @@ -71,7 +71,7 @@ impl Alias { aliases .entry(pretty_path_name(&dest).to_string()) .and_modify(|e| e.push(alias.name().to_string())) - .or_insert(vec![alias.name().to_string()]); + .or_insert_with(|| vec![alias.name().to_string()]); } } @@ -79,11 +79,11 @@ impl Alias { } pub fn destination(&self) -> anyhow::Result { - std::fs::read_link(&self.path).map_err(|e| anyhow::Error::new(e)) + std::fs::read_link(&self.path).map_err(anyhow::Error::new) } pub fn remove(&self) -> anyhow::Result<()> { - crate::symlink::remove_symlink(&self.path).map_err(|e| anyhow::Error::new(e)) + crate::symlink::remove_symlink(&self.path).map_err(anyhow::Error::new) } pub fn name(&self) -> &str { diff --git a/src/archive.rs b/src/archive.rs index ebe374b..c1cea48 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -15,7 +15,7 @@ impl Archive { pub fn extract_into>(self, path: P) -> anyhow::Result<()> { let xz_stream = xz2::read::XzDecoder::new(self.reader); let mut archive = tar::Archive::new(xz_stream); - archive.unpack(path).map_err(|e| anyhow::Error::new(e)) + archive.unpack(path).map_err(anyhow::Error::new) } #[cfg(windows)] diff --git a/src/cli.rs b/src/cli.rs index 0aea4bf..cbdc94f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -96,7 +96,7 @@ impl SubCommand { } } -const VERSION_HELP: &'static str = r#" +const VERSION_HELP: &str = r#" Versions: Numeric version numbers can be complete or partial semver, with an optional leading 'v'. @@ -133,5 +133,5 @@ pub struct Cli { } pub fn parse() -> Cli { - return Cli::parse(); + Cli::parse() } diff --git a/src/cmd/install.rs b/src/cmd/install.rs index 6f1d720..df1ba19 100644 --- a/src/cmd/install.rs +++ b/src/cmd/install.rs @@ -15,12 +15,9 @@ impl super::Command for Install { type InitResult = (); fn init(&self, config: Config) -> anyhow::Result { - let can_install = match &self.version { - Version::Full(NodeVersion::Alias(_)) => false, - _ => true, - }; + let is_alias = matches!(&self.version, Version::Full(NodeVersion::Alias(_))); - if !can_install { + if is_alias { anyhow::bail!( "Unable to install the version {}", &self.version.to_string().bold() diff --git a/src/downloader.rs b/src/downloader.rs index 4471131..4647d05 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -6,7 +6,6 @@ use crate::{archive::Archive, progress_bar::Bar}; use colored::*; use indicatif::HumanBytes; use std::path::PathBuf; -use ureq; pub fn download(r: &Release, config: &Config) -> anyhow::Result { let release_dir = &config.release_dir(); diff --git a/src/fetcher.rs b/src/fetcher.rs index 66490b7..2ede125 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -34,10 +34,7 @@ impl Fetcher { pub fn lts(&self) -> anyhow::Result<&Release> { self.list .iter() - .find(|x| match x.lts { - Lts::Yes(_) => true, - _ => false, - }) + .find(|x| matches!(x.lts, Lts::Yes(_))) .ok_or_else(|| anyhow::anyhow!("Unable to find {} release", "lts".bold())) } @@ -51,10 +48,7 @@ impl Fetcher { pub fn latest(&self) -> anyhow::Result<&Release> { self.list .iter() - .find(|x| match x.lts { - Lts::No(_) => true, - _ => false, - }) + .find(|x| matches!(x.lts, Lts::No(_))) .ok_or_else(|| anyhow::anyhow!("Unable to find {} release", "latest".bold())) } diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 9a7fa50..97f41b7 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -2,7 +2,7 @@ use indicatif::{ProgressBar, ProgressStyle}; use std::io::Read; -const TEMPLATE: &'static str = +const TEMPLATE: &str = "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})"; pub struct Bar { diff --git a/src/shell/bash.rs b/src/shell/bash.rs index 5c0b1b1..fe8801e 100644 --- a/src/shell/bash.rs +++ b/src/shell/bash.rs @@ -1,10 +1,11 @@ -use std::path::PathBuf; +use super::Shell; +use std::path::Path; #[derive(Debug)] pub struct Bash; -impl super::shell::Shell for Bash { - fn path(&self, path: &PathBuf, append: bool) -> String { +impl Shell for Bash { + fn path(&self, path: &Path, append: bool) -> String { if append { return format!("export PATH=$PATH:{:?};", path.display()); } diff --git a/src/shell/shell.rs b/src/shell/common.rs similarity index 82% rename from src/shell/shell.rs rename to src/shell/common.rs index 31d82c9..4e1c157 100644 --- a/src/shell/shell.rs +++ b/src/shell/common.rs @@ -1,8 +1,8 @@ use clap::Clap; -use std::path::PathBuf; +use std::path::Path; pub trait Shell { - fn path(&self, path: &PathBuf, append: bool) -> String; + fn path(&self, path: &Path, append: bool) -> String; fn env_var(&self, name: &str, val: &str) -> String; fn use_on_cd(&self) -> String; } diff --git a/src/shell/fish.rs b/src/shell/fish.rs index 12807da..a91eb78 100644 --- a/src/shell/fish.rs +++ b/src/shell/fish.rs @@ -1,11 +1,11 @@ -use clap::Clap; -use std::path::PathBuf; +use super::Shell; +use std::path::Path; -#[derive(Debug, Clap, PartialEq, Eq)] +#[derive(Debug)] pub struct Fish; -impl super::shell::Shell for Fish { - fn path(&self, path: &PathBuf, append: bool) -> String { +impl Shell for Fish { + fn path(&self, path: &Path, append: bool) -> String { if append { return format!("set -gx PATH $PATH {:?};", path.display()); } diff --git a/src/shell/mod.rs b/src/shell/mod.rs index 4facbf6..9ac0f08 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -1,7 +1,7 @@ -mod shell; +mod common; pub mod bash; pub mod fish; pub mod pwsh; pub mod zsh; -pub use shell::*; +pub use common::*; diff --git a/src/shell/pwsh.rs b/src/shell/pwsh.rs index 4b99049..587d273 100644 --- a/src/shell/pwsh.rs +++ b/src/shell/pwsh.rs @@ -1,13 +1,12 @@ -use clap::Clap; -use std::ffi::OsString; -use std::path::PathBuf; +use super::Shell; +use std::path::Path; -#[derive(Debug, Clap, PartialEq, Eq)] +#[derive(Debug)] pub struct Pwsh; -impl super::shell::Shell for Pwsh { - fn path(&self, path: &PathBuf, append: bool) -> String { - let path_env = std::env::var_os("PATH").unwrap_or(OsString::new()); +impl Shell for Pwsh { + fn path(&self, path: &Path, append: bool) -> String { + let path_env = std::env::var_os("PATH").unwrap_or_default(); let mut split_paths: Vec<_> = std::env::split_paths(&path_env).collect(); if append { diff --git a/src/shell/zsh.rs b/src/shell/zsh.rs index 3490493..1468659 100644 --- a/src/shell/zsh.rs +++ b/src/shell/zsh.rs @@ -1,11 +1,11 @@ -use clap::Clap; -use std::path::PathBuf; +use super::Shell; +use std::path::Path; -#[derive(Debug, Clap, PartialEq, Eq)] +#[derive(Debug)] pub struct Zsh; -impl super::shell::Shell for Zsh { - fn path(&self, path: &PathBuf, append: bool) -> String { +impl Shell for Zsh { + fn path(&self, path: &Path, append: bool) -> String { if append { return format!("export PATH=$PATH:{:?};", path.display()); } diff --git a/src/version/version.rs b/src/version/common.rs similarity index 98% rename from src/version/version.rs rename to src/version/common.rs index 42c11bb..efe4bae 100644 --- a/src/version/version.rs +++ b/src/version/common.rs @@ -1,4 +1,4 @@ -use super::node_version::NodeVersion; +use super::NodeVersion; use colored::*; use serde::Deserialize; use std::env::current_dir; @@ -6,7 +6,7 @@ use std::fs; use std::io::{BufRead, BufReader, Read}; use std::str::FromStr; -const PACKAGE_JSON: &'static str = "package.json"; +const PACKAGE_JSON: &str = "package.json"; const NODE_FILES: [&str; 3] = [".nvmrc", ".node-version", PACKAGE_JSON]; #[derive(Debug, Clone, PartialEq, Eq)] @@ -116,7 +116,7 @@ impl FromStr for Version { match NodeVersion::parse(s) { Ok(v) => Ok(Self::Full(v)), Err(e) => { - let mut parts = s.trim_start_matches("v").split("."); + let mut parts = s.trim_start_matches('v').split('.'); match (parts.next(), parts.next()) { (Some(major), None) => Ok(Self::Major(major.parse::()?)), (Some(major), Some(minor)) => Ok(Self::MajorMinor( diff --git a/src/version/mod.rs b/src/version/mod.rs index e0a4e18..ab91de3 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -1,5 +1,5 @@ +mod common; mod node_version; -mod version; -pub use node_version::NodeVersion; -pub use version::Version; +pub use common::*; +pub use node_version::*; diff --git a/src/version/node_version.rs b/src/version/node_version.rs index 8f7638d..a225fe8 100644 --- a/src/version/node_version.rs +++ b/src/version/node_version.rs @@ -16,7 +16,7 @@ fn is_numeric(version: &str) -> bool { impl NodeVersion { pub fn parse(version: &str) -> anyhow::Result { let lower_case = version.to_lowercase(); - let trimmed = lower_case.trim_start_matches("v"); + let trimmed = lower_case.trim_start_matches('v'); if trimmed.starts_with("lts-") || trimmed.starts_with("lts/") { Ok(Self::Lts(trimmed[4..].to_string()))