Skip to content

Commit

Permalink
chore: fix clippy lint errors (#58)
Browse files Browse the repository at this point in the history
* fix most lint error

* fix shell module inception

* fix version module inception
  • Loading branch information
numToStr authored May 9, 2021
1 parent 3df197f commit f0e2d4d
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 54 deletions.
10 changes: 5 additions & 5 deletions src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -57,7 +57,7 @@ impl Alias {
Ok(aliases)
}

pub fn hashmap<'a, P: AsRef<Path>>(path: P) -> anyhow::Result<HashMap<String, Vec<String>>> {
pub fn hashmap<P: AsRef<Path>>(path: P) -> anyhow::Result<HashMap<String, Vec<String>>> {
let list = std::fs::read_dir(&path)?;
let mut aliases: HashMap<String, Vec<String>> = HashMap::new();

Expand All @@ -71,19 +71,19 @@ 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()]);
}
}

Ok(aliases)
}

pub fn destination(&self) -> anyhow::Result<PathBuf> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Archive {
pub fn extract_into<P: AsRef<Path>>(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)]
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -133,5 +133,5 @@ pub struct Cli {
}

pub fn parse() -> Cli {
return Cli::parse();
Cli::parse()
}
7 changes: 2 additions & 5 deletions src/cmd/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ impl super::Command for Install {
type InitResult = ();

fn init(&self, config: Config) -> anyhow::Result<Self::InitResult> {
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()
Expand Down
1 change: 0 additions & 1 deletion src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
let release_dir = &config.release_dir();
Expand Down
10 changes: 2 additions & 8 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand All @@ -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()))
}

Expand Down
2 changes: 1 addition & 1 deletion src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/shell/bash.rs
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/shell/shell.rs → src/shell/common.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions src/shell/fish.rs
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
13 changes: 6 additions & 7 deletions src/shell/pwsh.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/shell/zsh.rs
Original file line number Diff line number Diff line change
@@ -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());
}
Expand Down
6 changes: 3 additions & 3 deletions src/version/version.rs → src/version/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::node_version::NodeVersion;
use super::NodeVersion;
use colored::*;
use serde::Deserialize;
use std::env::current_dir;
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)]
Expand Down Expand Up @@ -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::<u64>()?)),
(Some(major), Some(minor)) => Ok(Self::MajorMinor(
Expand Down
6 changes: 3 additions & 3 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
2 changes: 1 addition & 1 deletion src/version/node_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn is_numeric(version: &str) -> bool {
impl NodeVersion {
pub fn parse(version: &str) -> anyhow::Result<Self> {
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()))
Expand Down

0 comments on commit f0e2d4d

Please sign in to comment.