Skip to content

Commit

Permalink
refactor: some internal refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nokazn committed Feb 2, 2024
1 parent 911c1c9 commit 0904457
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Lockfile {
impl Lockfile {
pub fn new<T: AsRef<Path>>(dir_path: T) -> Result<Self, String> {
match Lockfile::try_to_read_lockfile(&dir_path) {
Some((kind, path)) => Ok(Lockfile { kind, path }),
Some((kind, path)) => Ok(Self { kind, path }),
None => Err(format!(
"No lockfile at `{}`",
to_absolute_path(&dir_path).to_string_lossy()
Expand Down
17 changes: 9 additions & 8 deletions src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct PackageJson {
}

impl PackageJson {
fn new<T: AsRef<Path>>(base_dir: T) -> Result<PackageJson, Error> {
fn new<T: AsRef<Path>>(base_dir: T) -> Result<Self, Error> {
let file_path = to_package_json_path(base_dir);
let contents = fs::read_to_string(&file_path);
match contents {
Expand All @@ -74,7 +74,7 @@ struct PackageDependencies {

impl PackageDependencies {
fn new(raw: PackageJson) -> Self {
PackageDependencies {
Self {
dependencies: raw.dependencies.unwrap_or_default(),
dev_dependencies: raw.devDependencies.unwrap_or_default(),
peer_dependencies: raw.peerDependencies.unwrap_or_default(),
Expand Down Expand Up @@ -102,7 +102,7 @@ impl WorkspacePackage {
return Err(Error::InvalidWorkspaceError(base_dir));
}
let original = PackageJson::new(&base_dir)?;
Ok(WorkspacePackage {
Ok(Self {
original: original.clone(),
base_dir,
kind,
Expand Down Expand Up @@ -182,13 +182,13 @@ impl Hashable for ProjectRoot {
}

impl ProjectRoot {
pub fn new<T: AsRef<Path>>(base_dir: T, kind: PackageManagerKind) -> Result<ProjectRoot, Error> {
pub fn new<T: AsRef<Path>>(base_dir: T, kind: PackageManagerKind) -> Result<Self, Error> {
let original = PackageJson::new(&base_dir)?;
Ok(ProjectRoot {
Ok(Self {
original: original.clone(),
kind,
root: PackageDependencies::new(original.clone()),
workspaces: ProjectRoot::resolve_workspaces(base_dir, kind, original.workspaces),
workspaces: Self::resolve_workspaces(base_dir, kind, original.workspaces),
})
}

Expand All @@ -199,8 +199,9 @@ impl ProjectRoot {
) -> BTreeMap<String, WorkspacePackage> {
let workspaces = Workspaces::new(base_dir.as_ref().to_path_buf(), kind, patterns);
let mut workspace_map = BTreeMap::<String, WorkspacePackage>::new();
for p in workspaces.packages.iter() {
if let Ok(w) = WorkspacePackage::new(&p, kind).and_then(|w| w.validate_package_json_fields(p))
for path in workspaces.packages.iter() {
if let Ok(w) =
WorkspacePackage::new(&path, kind).and_then(|w| w.validate_package_json_fields(path))
{
let (name, fallback) = w.get_package_name();
if workspace_map.get(&name).is_none() {
Expand Down

0 comments on commit 0904457

Please sign in to comment.