Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor local function to use AsRef, simplify write arguments #48

Merged
merged 3 commits into from
May 2, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.vscode/
158 changes: 89 additions & 69 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub struct LocalAsset {
impl LocalAsset {
/// A new asset is created with a path on the local filesystem and a
/// vector of bytes representing its contents
pub fn new(origin_path: &str, contents: Vec<u8>) -> Result<Self> {
pub fn new(origin_path: impl AsRef<Utf8Path>, contents: Vec<u8>) -> Result<Self> {
let origin_path = origin_path.as_ref();
Ok(LocalAsset {
filename: LocalAsset::filename(origin_path)?,
origin_path: origin_path.to_string(),
Expand All @@ -33,7 +34,8 @@ impl LocalAsset {

/// Loads an asset from a path on the local filesystem, returning a
/// LocalAsset struct
pub fn load(origin_path: &str) -> Result<LocalAsset> {
pub fn load(origin_path: impl AsRef<Utf8Path>) -> Result<LocalAsset> {
let origin_path = origin_path.as_ref();
match Path::new(origin_path).try_exists() {
Ok(_) => match fs::read(origin_path) {
Ok(contents) => Ok(LocalAsset {
Expand All @@ -55,7 +57,8 @@ impl LocalAsset {

/// Loads an asset from a path on the local filesystem, returning a
/// string of its contents
pub fn load_string(origin_path: &str) -> Result<String> {
pub fn load_string(origin_path: impl AsRef<Utf8Path>) -> Result<String> {
let origin_path = origin_path.as_ref();
match Path::new(origin_path).try_exists() {
Ok(_) => match fs::read_to_string(origin_path) {
Ok(contents) => Ok(contents),
Expand All @@ -73,7 +76,8 @@ impl LocalAsset {

/// Loads an asset from a path on the local filesystem, returning a
/// vector of bytes of its contents
pub fn load_bytes(origin_path: &str) -> Result<Vec<u8>> {
pub fn load_bytes(origin_path: impl AsRef<Utf8Path>) -> Result<Vec<u8>> {
let origin_path = origin_path.as_ref();
match Path::new(origin_path).try_exists() {
Ok(_) => match fs::read(origin_path) {
Ok(contents) => Ok(contents),
Expand All @@ -91,76 +95,87 @@ impl LocalAsset {

/// Writes an asset to a path on the local filesystem, determines the
/// filename from the origin path
pub fn write(&self, dest_dir: &str) -> Result<PathBuf> {
let dest_path = self.dest_path(dest_dir)?;
pub fn write(&self, dest_dir: impl AsRef<Utf8Path>) -> Result<PathBuf> {
let dest_dir = dest_dir.as_ref();
let dest_path = dest_dir.join(&self.filename);
match fs::write(&dest_path, &self.contents) {
Ok(_) => Ok(dest_path),
Ok(_) => Ok(dest_path.into()),
Err(details) => Err(AxoassetError::LocalAssetWriteFailed {
origin_path: self.origin_path.to_string(),
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
}),
}
}

/// Writes an asset to a path on the local filesystem, determines the
/// filename from the origin path
pub fn write_new(contents: &str, filename: &str, dest_dir: &str) -> Result<PathBuf> {
let dest_path = Path::new(dest_dir).join(filename);
match fs::write(&dest_path, contents) {
Ok(_) => Ok(dest_path),
/// Writes an asset to a path on the local filesystem
pub fn write_new(contents: &str, dest_path: impl AsRef<Utf8Path>) -> Result<PathBuf> {
let dest_path = dest_path.as_ref();
if Path::new(dest_path).file_name().is_none() {
return Err(AxoassetError::LocalAssetMissingFilename {
origin_path: dest_path.to_string(),
});
}
match fs::write(dest_path, contents) {
Ok(_) => Ok(dest_path.into()),
Err(details) => Err(AxoassetError::LocalAssetWriteNewFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
}),
}
}

/// Writes an asset and all of its parent directories on the local filesystem.
pub fn write_new_all(contents: &str, filename: &str, dest_dir: &str) -> Result<PathBuf> {
let dest_path = Path::new(dest_dir).join(filename);
pub fn write_new_all(contents: &str, dest_path: impl AsRef<Utf8Path>) -> Result<PathBuf> {
let dest_path = dest_path.as_ref();
if Path::new(dest_path).file_name().is_none() {
return Err(AxoassetError::LocalAssetMissingFilename {
origin_path: dest_path.to_string(),
});
}
let dest_dir = Path::new(dest_path).parent().unwrap();
match fs::create_dir_all(dest_dir) {
Ok(_) => (),
Err(details) => {
return Err(AxoassetError::LocalAssetWriteNewFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
})
}
}
LocalAsset::write_new(contents, filename, dest_dir)
LocalAsset::write_new(contents, dest_path)
}

/// Creates a new directory
pub fn create_dir(dest: &str) -> Result<PathBuf> {
let dest_path = PathBuf::from(dest);
match fs::create_dir(&dest_path) {
Ok(_) => Ok(dest_path),
pub fn create_dir(dest: impl AsRef<Utf8Path>) -> Result<PathBuf> {
let dest_path = dest.as_ref();
match fs::create_dir(dest_path) {
Ok(_) => Ok(dest_path.into()),
Err(details) => Err(AxoassetError::LocalAssetDirCreationFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
}),
}
}

/// Creates a new directory, including all parent directories
pub fn create_dir_all(dest: &str) -> Result<PathBuf> {
let dest_path = PathBuf::from(dest);
match fs::create_dir_all(&dest_path) {
Ok(_) => Ok(dest_path),
pub fn create_dir_all(dest: impl AsRef<Utf8Path>) -> Result<PathBuf> {
let dest_path = dest.as_ref();
match fs::create_dir_all(dest_path) {
Ok(_) => Ok(dest_path.into()),
Err(details) => Err(AxoassetError::LocalAssetDirCreationFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
}),
}
}

/// Removes a file
pub fn remove_file(dest: &str) -> Result<()> {
let dest_path = PathBuf::from(dest);
if let Err(details) = fs::remove_file(&dest_path) {
pub fn remove_file(dest: impl AsRef<Utf8Path>) -> Result<()> {
let dest_path = dest.as_ref();
if let Err(details) = fs::remove_file(dest_path) {
return Err(AxoassetError::LocalAssetRemoveFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
});
}
Expand All @@ -169,12 +184,12 @@ impl LocalAsset {
}

/// Removes a directory
pub fn remove_dir(dest: &str) -> Result<()> {
let dest_path = PathBuf::from(dest);
pub fn remove_dir(dest: impl AsRef<Utf8Path>) -> Result<()> {
let dest_path = dest.as_ref();
if dest_path.is_dir() {
if let Err(details) = fs::remove_dir(&dest_path) {
if let Err(details) = fs::remove_dir(dest_path) {
return Err(AxoassetError::LocalAssetRemoveFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
});
}
Expand All @@ -184,12 +199,12 @@ impl LocalAsset {
}

/// Removes a directory and all of its contents
pub fn remove_dir_all(dest: &str) -> Result<()> {
let dest_path = PathBuf::from(dest);
pub fn remove_dir_all(dest: impl AsRef<Utf8Path>) -> Result<()> {
let dest_path = dest.as_ref();
if dest_path.is_dir() {
if let Err(details) = fs::remove_dir_all(&dest_path) {
if let Err(details) = fs::remove_dir_all(dest_path) {
return Err(AxoassetError::LocalAssetRemoveFailed {
dest_path: dest_path.display().to_string(),
dest_path: dest_path.to_string(),
details,
});
}
Expand All @@ -199,7 +214,10 @@ impl LocalAsset {
}

/// Copies an asset from one location on the local filesystem to another
pub fn copy(origin_path: &str, dest_dir: &str) -> Result<PathBuf> {
pub fn copy(
origin_path: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<PathBuf> {
LocalAsset::load(origin_path)?.write(dest_dir)
}

Expand All @@ -215,11 +233,11 @@ impl LocalAsset {
/// Find a desired file in the provided dir or an ancestor of it.
///
/// On success returns the path to the found file.
pub fn search_ancestors<'a>(
start_dir: impl Into<&'a Utf8Path>,
pub fn search_ancestors(
start_dir: impl AsRef<Utf8Path>,
desired_filename: &str,
) -> Result<Utf8PathBuf> {
let start_dir = start_dir.into();
let start_dir = start_dir.as_ref();
// We want a proper absolute path so we can compare paths to workspace roots easily.
//
// Also if someone starts the path with ./ we should trim that to avoid weirdness.
Expand Down Expand Up @@ -249,15 +267,9 @@ impl LocalAsset {
}

/// Computes filename from provided origin path
pub fn filename(origin_path: &str) -> Result<String> {
if let Some(filename) = Path::new(origin_path).file_name() {
if let Some(filename) = filename.to_str() {
Ok(filename.to_string())
} else {
Err(AxoassetError::LocalAssetMissingFilename {
origin_path: origin_path.to_string(),
})
}
pub fn filename(origin_path: &Utf8Path) -> Result<String> {
if let Some(filename) = origin_path.file_name() {
Ok(filename.to_string())
} else {
Err(AxoassetError::LocalAssetMissingFilename {
origin_path: origin_path.to_string(),
Expand All @@ -266,38 +278,46 @@ impl LocalAsset {
}

/// Creates a new .tar.gz file from a provided directory
pub fn tar_gz_dir(origin_dir: &str, dest_dir: &str) -> Result<()> {
pub fn tar_gz_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
Utf8Path::new(origin_dir),
Utf8Path::new(dest_dir),
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Gzip,
)
}

/// Creates a new .tar.xz file from a provided directory
pub fn tar_xz_dir(origin_dir: &str, dest_dir: &str) -> Result<()> {
pub fn tar_xz_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
Utf8Path::new(origin_dir),
Utf8Path::new(dest_dir),
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Xzip,
)
}

/// Creates a new .tar.zstd file from a provided directory
pub fn tar_zstd_dir(origin_dir: &str, dest_dir: &str) -> Result<()> {
pub fn tar_zstd_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
Utf8Path::new(origin_dir),
Utf8Path::new(dest_dir),
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Zstd,
)
}

/// Creates a new .zip file from a provided directory
pub fn zip_dir(origin_dir: &str, dest_dir: &str) -> Result<()> {
zip_dir(Utf8Path::new(origin_dir), Utf8Path::new(dest_dir))
}

fn dest_path(&self, dest_dir: &str) -> Result<PathBuf> {
Ok(Path::new(dest_dir).join(&self.filename))
pub fn zip_dir(origin_dir: impl AsRef<Utf8Path>, dest_dir: impl AsRef<Utf8Path>) -> Result<()> {
zip_dir(
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
)
}
}
6 changes: 3 additions & 3 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ impl SourceFile {
}

/// SourceFile equivalent of [`LocalAsset::load`][]
pub fn load_local<'a>(origin_path: impl Into<&'a Utf8Path>) -> Result<SourceFile> {
let origin_path = origin_path.into();
pub fn load_local(origin_path: impl AsRef<Utf8Path>) -> Result<SourceFile> {
let origin_path = origin_path.as_ref();
let contents = LocalAsset::load_string(origin_path.as_str())?;
Ok(SourceFile {
inner: Arc::new(SourceFileInner {
filename: LocalAsset::filename(origin_path.as_str())?,
filename: LocalAsset::filename(origin_path)?,
origin_path: origin_path.to_string(),
contents,
}),
Expand Down
6 changes: 3 additions & 3 deletions tests/local_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ async fn it_creates_new_assets() {
fn it_creates_parent_directories() {
let dest = assert_fs::TempDir::new().unwrap();

let dest_dir = Path::new(&dest.as_os_str())
let dest_path = Path::new(&dest.as_os_str())
.join("subdir")
.join("test.md")
.display()
.to_string();
axoasset::LocalAsset::write_new_all("file content", "index.md", &dest_dir).unwrap();
axoasset::LocalAsset::write_new_all("file content", dest_path).unwrap();

assert!(Path::new(&dest.as_os_str()).join("subdir").exists());
}
Expand All @@ -55,7 +55,7 @@ fn it_creates_a_new_directory() {
.join("subdir")
.display()
.to_string();
axoasset::LocalAsset::create_dir(&dest_dir).unwrap();
axoasset::LocalAsset::create_dir(dest_dir).unwrap();

assert!(Path::new(&dest.as_os_str()).join("subdir").exists());
}
4 changes: 2 additions & 2 deletions tests/local_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn it_removes_both_file_and_directory() {
fs::create_dir_all(file_path.parent().unwrap()).unwrap();
fs::write(&file_path, "hello").unwrap();

axoasset::LocalAsset::remove_file(&file_path.display().to_string()).unwrap();
axoasset::LocalAsset::remove_file(file_path.display().to_string()).unwrap();
assert!(!file_path.exists());

axoasset::LocalAsset::remove_dir(&dir_path.display().to_string()).unwrap();
axoasset::LocalAsset::remove_dir(dir_path.display().to_string()).unwrap();
assert!(!dir_path.exists());
}
10 changes: 4 additions & 6 deletions tests/local_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ use image::ImageFormat;
#[test]
fn it_writes_a_new_file_from_string() {
let dest = assert_fs::TempDir::new().unwrap();
let dest_dir = Path::new(dest.to_str().unwrap());
let dest_file = Path::new(dest.to_str().unwrap()).join("contents.txt");

let filename = "contents.txt";
let contents = "CONTENTS";
axoasset::LocalAsset::write_new(contents, filename, &dest_dir.display().to_string()).unwrap();
let written_file = dest_dir.join(filename);
assert!(written_file.exists());
axoasset::LocalAsset::write_new(contents, dest_file.to_str().unwrap()).unwrap();
assert!(dest_file.exists());

let loaded_contents =
axoasset::LocalAsset::load_string(&written_file.display().to_string()).unwrap();
axoasset::LocalAsset::load_string(dest_file.display().to_string()).unwrap();
assert!(loaded_contents.contains(contents));
}

Expand Down