Skip to content

Commit

Permalink
refactor: some function arguments & project module
Browse files Browse the repository at this point in the history
  • Loading branch information
nokazn committed Feb 17, 2024
1 parent 1cb4e60 commit 15adf6c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct Cache {
}

impl Cache {
pub fn new<T: AsRef<Path>>(base_dir: T, cache_dir: Option<T>) -> Result<Self> {
pub fn new(base_dir: impl AsRef<Path>, cache_dir: Option<impl AsRef<Path>>) -> Result<Self> {
const DEFAULT_CACHE_DIR: &'static str = ".cache/syncnm";

let base_dir = exists_dir(base_dir)?;
Expand All @@ -92,7 +92,7 @@ impl Cache {
})
}

pub fn save<T: Into<String>>(&self, key: T) -> Result<()> {
pub fn save(&self, key: impl Into<String>) -> Result<()> {
let key = key.into();
let cache = self.cache_dir.join(&key);
create_symlink_dir(&self.base_dir, cache)?;
Expand All @@ -106,7 +106,7 @@ impl Cache {
exists_dir(self.cache_dir.join(current_hash.to_string())).ok()
}

pub fn restore<T: Into<String>>(&self, key: T) -> Result<()> {
pub fn restore(&self, key: impl Into<String>) -> Result<()> {
let cache = self.cache_dir.join(key.into());
if cache.is_dir() {
if let Some(current) = self.find_current_cache() {
Expand Down
4 changes: 2 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Default for PackageManagerKind {
}

impl PackageManagerKind {
pub fn lockfile_names(&self) -> Vec<&str> {
pub fn to_lockfile_names(&self) -> Vec<&str> {
match self {
PackageManagerKind::Npm => vec!["package-lock.json"],
PackageManagerKind::Yarn => vec!["yarn.lock"],
Expand All @@ -30,7 +30,7 @@ impl PackageManagerKind {
}
}

pub fn corepack_name(&self) -> Option<&'static str> {
pub fn to_corepack_name(&self) -> Option<&'static str> {
match self {
PackageManagerKind::Npm => Some("npm"),
PackageManagerKind::Yarn => Some("yarn"),
Expand Down
6 changes: 3 additions & 3 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ pub struct Lockfile {
}

impl Lockfile {
pub fn new<T: AsRef<Path>>(dir_path: T) -> Result<Self> {
pub fn new(dir_path: impl AsRef<Path>) -> Result<Self> {
match Lockfile::try_to_read_lockfile(&dir_path) {
Some((kind, path)) => Ok(Self { kind, path }),
None => Err(Error::NoLockfileError(dir_path.as_ref().to_path_buf())),
}
}

fn try_to_read_lockfile<T: AsRef<Path>>(dir_path: T) -> Option<(PackageManagerKind, PathBuf)> {
fn try_to_read_lockfile(dir_path: impl AsRef<Path>) -> Option<(PackageManagerKind, PathBuf)> {
for kind in PackageManagerKind::iter() {
for lockfile in kind.lockfile_names() {
for lockfile in kind.to_lockfile_names() {
let file_path = dir_path.as_ref().join(lockfile);
if file_path.exists() {
return Some((kind, file_path));
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ mod cache;
mod core;
mod errors;
mod lockfile;
mod package_json;
mod project;
mod utils;
mod workspaces;

use crate::{lockfile::Lockfile, package_json::ProjectRoot, utils::hash::Hashable};
use crate::{lockfile::Lockfile, project::ProjectRoot, utils::hash::Hashable};
use env_logger;

fn main() {
Expand Down
18 changes: 9 additions & 9 deletions src/package_json.rs → src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use crate::{

type Dependencies = BTreeMap<String, String>;

fn to_package_json_path<T: AsRef<Path>>(base_dir: T) -> PathBuf {
fn to_package_json_path(base_dir: impl AsRef<Path>) -> PathBuf {
const PACKAGE_JSON: &str = "package.json";
base_dir.as_ref().join(PACKAGE_JSON)
}

/// Ignore `node_modules` directory
fn is_valid_base_dir<T: AsRef<Path>>(base_dir: T) -> bool {
fn is_valid_base_dir(base_dir: impl AsRef<Path>) -> bool {
const IGNORED: [&str; 1] = ["node_modules"];
let path = base_dir.as_ref().to_path_buf();
if !path.is_dir() {
Expand Down Expand Up @@ -52,7 +52,7 @@ struct PackageJson {
}

impl PackageJson {
fn new<T: AsRef<Path>>(base_dir: T) -> Result<Self> {
fn new(base_dir: impl AsRef<Path>) -> Result<Self> {
let file_path = to_package_json_path(base_dir);
let contents = fs::read_to_string(&file_path);
match contents {
Expand Down Expand Up @@ -99,7 +99,7 @@ struct WorkspacePackage {
}

impl WorkspacePackage {
fn new<T: AsRef<Path>>(base_dir: T, kind: PackageManagerKind) -> Result<Self> {
fn new(base_dir: impl AsRef<Path>, kind: PackageManagerKind) -> Result<Self> {
let base_dir = base_dir.as_ref().to_path_buf();
if !is_valid_base_dir(&base_dir) {
return Err(Error::InvalidWorkspaceError(base_dir));
Expand All @@ -113,7 +113,7 @@ impl WorkspacePackage {
})
}

fn validate_package_json_fields<T: AsRef<Path>>(self, base_dir: T) -> Result<Self> {
fn validate_package_json_fields(self, base_dir: impl AsRef<Path>) -> Result<Self> {
let package_json_path = to_package_json_path(&base_dir);
match self.kind {
PackageManagerKind::Yarn
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Hashable for ProjectRoot {
}

impl ProjectRoot {
pub fn new<T: AsRef<Path>>(base_dir: T, kind: PackageManagerKind) -> Result<Self> {
pub fn new(base_dir: impl AsRef<Path>, kind: PackageManagerKind) -> Result<Self> {
let original = PackageJson::new(&base_dir)?;
Ok(
Self {
Expand All @@ -198,8 +198,8 @@ impl ProjectRoot {
)?
}

fn resolve_workspaces<T: AsRef<Path>>(
base_dir: T,
fn resolve_workspaces(
base_dir: impl AsRef<Path>,
kind: PackageManagerKind,
patterns: Option<Vec<String>>,
) -> BTreeMap<String, WorkspacePackage> {
Expand Down Expand Up @@ -257,7 +257,7 @@ impl ProjectRoot {
}
}

fn validate_package_json_fields<T: AsRef<Path>>(self, base_dir: T) -> Result<Self> {
fn validate_package_json_fields(self, base_dir: impl AsRef<Path>) -> Result<Self> {
match self.kind {
PackageManagerKind::Yarn
if self.original.workspaces.clone().unwrap_or_default().len() > 0
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
errors::{Error, Paths},
};

pub fn exists_dir<T: AsRef<Path>>(dir: T) -> Result<PathBuf> {
pub fn exists_dir(dir: impl AsRef<Path>) -> Result<PathBuf> {
let dir = dir
.as_ref()
.to_path_buf()
Expand Down
2 changes: 1 addition & 1 deletion src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use crate::{core::Result, errors::Error};

pub fn to_absolute_path<T: AsRef<Path>>(path: T) -> Result<PathBuf> {
pub fn to_absolute_path(path: impl AsRef<Path>) -> Result<PathBuf> {
let path = path.as_ref();
let absolute_path = if path.is_absolute() {
path.to_path_buf()
Expand Down
2 changes: 1 addition & 1 deletion src/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl PnpmWorkspace {
.map_err(|_| Error::ParseError(Paths::Multiple(file_paths.to_vec())))
}

fn to_pnpm_workspace<T: AsRef<Path>>(base_dir: T) -> [PathBuf; 2] {
fn to_pnpm_workspace(base_dir: impl AsRef<Path>) -> [PathBuf; 2] {
const PNPM_WORKSPACE: [&str; 2] = ["pnpm-workspace.yaml", "pnpm-workspace.yml"];
let base_dir = base_dir.as_ref().to_path_buf();
PNPM_WORKSPACE.map(|p| base_dir.join(p))
Expand Down

0 comments on commit 15adf6c

Please sign in to comment.