Skip to content

Commit

Permalink
refactor: code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Aug 19, 2024
1 parent 82259e7 commit 92eb098
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 52 deletions.
21 changes: 0 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ shlex = "1.3.0"
simd-json = "0.13.10"
strsim = "0.11.1"
terminal_size = "0.3.0"
thiserror = "1.0.63"
unicode-width = "0.1.13"

[dev-dependencies]
Expand Down
18 changes: 5 additions & 13 deletions src/cli/env_file.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
use dotenvy::Result;

#[derive(Clone, Debug)]
pub struct EnvFile {
inner: Vec<(String, String)>,
}

impl From<Vec<(String, String)>> for EnvFile {
fn from(inner: Vec<(String, String)>) -> Self {
Self { inner }
}
}
pub struct EnvFile(Vec<(String, String)>);

impl EnvFile {
pub fn from_path(path: &str) -> Result<Self> {
let file = dotenvy::from_filename_iter(path)?;
let env: Vec<(String, String)> = file.collect::<Result<_>>()?;
Ok(Self { inner: env })
dotenvy::from_filename_iter(path)
.and_then(|file| file.collect())
.map(Self)
}

pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
self.inner.iter().map(|(a, b)| (a, b))
self.0.iter().map(|(a, b)| (a, b))
}
}
6 changes: 3 additions & 3 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn handle(package_paths: impl Iterator<Item = PathBuf>) -> Result<bool> {
let mut lock = stdout().lock();

if found_package {
lock.write_all("\n".as_bytes())?;
lock.write_all(b"\n")?;
}

lock.write_all(package.make_prefix(None, Stream::Stdout).as_bytes())?;
Expand Down Expand Up @@ -53,11 +53,11 @@ pub fn handle(package_paths: impl Iterator<Item = PathBuf>) -> Result<bool> {
lock.write_all(" ".repeat(longest_pad + 2).as_bytes())?;
}
lock.write_all(line.as_bytes())?;
lock.write_all("\n".as_bytes())?;
lock.write_all(b"\n")?;
}
} else {
lock.write_all(content.as_bytes())?;
lock.write_all("\n".as_bytes())?;
lock.write_all(b"\n")?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Cli {
} else {
let found_package = list::handle(package_paths)?;
if !found_package {
Cli::command().print_help()?;
Self::command().print_help()?;
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{fs, path::Path};

use owo_colors::{OwoColorize as _, Stream};
use serde::Deserialize;
use thiserror::Error;

use indexmap::IndexMap;

Expand All @@ -17,12 +16,10 @@ pub struct PackageJson {
pub scripts: AIndexMap<String, String>,
}

#[derive(Error, Debug)]
#[derive(Debug)]
pub enum PackageJsonFromPathError {
#[error("error reading file")]
FileError(#[from] std::io::Error),
#[error("error parsing file")]
ParseError(#[from] simd_json::Error),
FileError(std::io::Error),
ParseError(simd_json::Error),
}

impl PackageJsonFromPathError {
Expand Down Expand Up @@ -55,8 +52,9 @@ impl PackageJsonFromPathError {

impl PackageJson {
pub fn from_path(path: &Path) -> Result<Self, PackageJsonFromPathError> {
let mut raw = fs::read(path)?;
Ok(simd_json::from_slice::<Self>(&mut raw)?)
use PackageJsonFromPathError::{FileError, ParseError};
let mut raw = fs::read(path).map_err(FileError)?;
simd_json::from_slice(&mut raw).map_err(ParseError)
}

pub fn from_path_safe(path: &Path) -> Option<Self> {
Expand Down
2 changes: 1 addition & 1 deletion src/run/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn exec(package_path: &Path, package_data: &PackageJson, args: &ExecArgs) ->

#[cfg(unix)]
{
Err::<(), _>(subproc.exec())?;
Err(subproc.exec())?;
Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions src/run/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use super::util::{make_patched_path, make_shell_cmd};

#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ScriptType {
Pre,
Post,
Expand All @@ -22,9 +22,9 @@ impl ScriptType {
#[must_use]
pub fn prefix(self) -> String {
match self {
ScriptType::Normal => String::new(),
ScriptType::Pre => "pre".into(),
ScriptType::Post => "post".into(),
Self::Normal => String::new(),
Self::Pre => "pre".into(),
Self::Post => "post".into(),
}
}
}
Expand Down

0 comments on commit 92eb098

Please sign in to comment.