Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ all-features = true

[dependencies]
serde = { version = "1.0.138", features = ["derive"] }
anyhow = "1.0.58"
serde_json = "1.0.138"
derive_builder = "0.20.2"
thiserror = "2.0.12"

[features]
default = []
Expand Down
21 changes: 21 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::path::PathBuf;

use thiserror::Error as ThisError;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(ThisError, Debug)]
pub enum Error {
#[error(transparent)]
IO(#[from] std::io::Error),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error("Couldn't find an available \"{filename}\" from {}.", .current_dir.display())]
NotFound {
filename: String,
current_dir: PathBuf,
},
#[error("Couldn't find an available {0} file")]
MissingPackageJson(String),
}

30 changes: 11 additions & 19 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::{format_err, Result};
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use crate::error::{Error, Result};

use self::write_options::WriteOptions;

pub(crate) mod write_options;
Expand All @@ -15,11 +16,10 @@ pub fn find_closest_file<P: AsRef<Path>>(filename: &str, current_dir: P) -> Resu
return Ok(file_path);
}
if !current_dir.pop() {
return Err(format_err!(
"Couldn't find an available \"{}\" from {}.",
filename,
current_dir.display()
));
return Err(Error::NotFound {
filename: filename.to_string(),
current_dir,
});
}
}
}
Expand All @@ -32,12 +32,8 @@ where
let mut file = File::open(file_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let serialized_json = serde_json::from_str(&contents);

match serialized_json {
Ok(json) => Ok(json),
Err(error) => Err(format_err!(error)),
}
Ok(serde_json::from_str(&contents)?)
}

pub fn write_json<Json, FilePath>(
Expand All @@ -53,15 +49,11 @@ where
serde_json::to_string_pretty(&json)
} else {
serde_json::to_string(&json)
};
}?;

match package_json {
Ok(json_content) => {
File::create(file_path)?.write_all(json_content.as_bytes())?;
Ok(())
}
Err(error) => Err(format_err!(error)),
}
File::create(file_path)?.write_all(package_json.as_bytes())?;

Ok(())
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
//! ## How to locate the closest `package.json` file
//!
//! ```no_run
//! use package_json::PackageJsonManager;
//! use package_json::{error::Result, PackageJsonManager};
//! use std::path::Path;
//!
//! # use anyhow::Result;
//! # fn main() -> Result<()> {
//! let mut manager = PackageJsonManager::new();
//! // based on the current working directory
Expand Down Expand Up @@ -62,6 +61,7 @@
//! ```
//!

pub mod error;
mod fs;
mod manager;
mod schema;
Expand Down
16 changes: 3 additions & 13 deletions src/manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::{Error, Result};
use crate::fs;
use crate::fs::write_options::WriteOptions;
use crate::PackageJson;
use anyhow::{format_err, Result};
use std::env;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -100,12 +100,7 @@ impl PackageJsonManager {
self.json = json;
})
})
.unwrap_or_else(|| {
Err(format_err!(
"Couldn't find an available {} file.",
PACKAGE_JSON_FILENAME
))
})
.unwrap_or_else(|| Err(Error::MissingPackageJson(PACKAGE_JSON_FILENAME.to_string())))
}

///
Expand Down Expand Up @@ -163,12 +158,7 @@ impl PackageJsonManager {
.expect("self.write_options should not be None"),
)
})
.unwrap_or_else(|| {
Err(format_err!(
"Couldn't find an available {} file.",
PACKAGE_JSON_FILENAME
))
})
.unwrap_or_else(|| Err(Error::MissingPackageJson(PACKAGE_JSON_FILENAME.to_string())))
}

/// Write the current `package.json` content to the specific `package.json` file.
Expand Down