Compare commits
3 commits
1de6990727
...
6768d52211
Author | SHA1 | Date | |
---|---|---|---|
6768d52211 | |||
faa9f291d4 | |||
a84248eca3 |
6 changed files with 100 additions and 3 deletions
|
@ -63,7 +63,7 @@ steps:
|
||||||
when:
|
when:
|
||||||
- event: tag
|
- event: tag
|
||||||
# INFO: https://woodpecker-ci.org/plugins/Gitea%20Release
|
# INFO: https://woodpecker-ci.org/plugins/Gitea%20Release
|
||||||
image: docker.io/woodpeckerci/plugin-gitea-release:0.3
|
image: docker.io/woodpeckerci/plugin-gitea-release:latest
|
||||||
settings:
|
settings:
|
||||||
base_url: https://git.kemitix.net
|
base_url: https://git.kemitix.net
|
||||||
api_key:
|
api_key:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kxio"
|
name = "kxio"
|
||||||
version = "1.1.1"
|
version = "1.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
|
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
|
||||||
description = "Provides injectable Filesystem and Network resources to make code more testable"
|
description = "Provides injectable Filesystem and Network resources to make code more testable"
|
||||||
|
|
|
@ -2,17 +2,93 @@ use super::Result;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
/// A handle for accessing the filesystem.
|
||||||
pub trait FileSystemLike {
|
pub trait FileSystemLike {
|
||||||
|
/// Returns the base directory for the filesystem that this handle can access.
|
||||||
|
///
|
||||||
|
/// Many methods expect a [Path] paremeter. This [Path] must be within the base directory of the
|
||||||
|
/// handle. Call [FileSystemLike::base] to get this.
|
||||||
|
///
|
||||||
|
/// To construct a [Path] within the filesystem:
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::new(std::env::current_dir()?);
|
||||||
|
/// let file_name = fs.base().join("foo");
|
||||||
|
/// ```
|
||||||
fn base(&self) -> &Path;
|
fn base(&self) -> &Path;
|
||||||
|
|
||||||
|
/// Creates a directory at the path. The parent directory must already exists, if it doesn't
|
||||||
|
/// consider using [FileSystemLike::dir_create_all] instead.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::temp()?;
|
||||||
|
/// let dir_name = fs.base().join("subdir");
|
||||||
|
/// fs.dir_create(&dir_name)?;
|
||||||
|
/// ```
|
||||||
fn dir_create(&self, path: &Path) -> Result<()>;
|
fn dir_create(&self, path: &Path) -> Result<()>;
|
||||||
|
|
||||||
|
/// Creates a directory and any missing parents as the path.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::temp()?;
|
||||||
|
/// let path_name = fs.base().join("subdir").join("child");
|
||||||
|
/// fs.dir_create_all(&path_name)?;
|
||||||
|
/// ```
|
||||||
fn dir_create_all(&self, path: &Path) -> Result<()>;
|
fn dir_create_all(&self, path: &Path) -> Result<()>;
|
||||||
|
|
||||||
|
/// Reads a file into a [String]. Will attempt to read the whole file into memory in one go.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::new(std::env::current_dir()?);
|
||||||
|
/// let file_name = fs.base().join("existing_file.txt");
|
||||||
|
/// let file_contents = fs.file_read_to_string(&file_name)?;
|
||||||
|
/// ```
|
||||||
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
fn file_read_to_string(&self, path: &Path) -> Result<String>;
|
||||||
|
|
||||||
|
/// Writes a string slice to a file.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::temp()?;
|
||||||
|
/// let file_name = fs.base().join("new_file");
|
||||||
|
/// let contents = "contents of new file";
|
||||||
|
/// fs.file_write(&file_name, contents)?;
|
||||||
|
/// ```
|
||||||
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;
|
||||||
|
|
||||||
|
/// Checks that the path exists.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::new(std::env::current_dir()?);
|
||||||
|
/// let path = fs.base().join("foo");
|
||||||
|
/// let exists: bool = fs.path_exists(&path)?;
|
||||||
|
/// ```
|
||||||
fn path_exists(&self, path: &Path) -> Result<bool>;
|
fn path_exists(&self, path: &Path) -> Result<bool>;
|
||||||
|
|
||||||
|
/// Checks whether the path is a directory.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::new(std::env::current_dir()?);
|
||||||
|
/// let path = fs.base().join("foo");
|
||||||
|
/// let is_dir: bool = fs.path_is_dir(&path)?;
|
||||||
|
/// ```
|
||||||
fn path_is_dir(&self, path: &Path) -> Result<bool>;
|
fn path_is_dir(&self, path: &Path) -> Result<bool>;
|
||||||
|
|
||||||
|
/// Checks whether the path is a file.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::new(std::env::current_dir()?);
|
||||||
|
/// let path = fs.base().join("foo");
|
||||||
|
/// let is_file: bool = fs.path_is_file(&path)?;
|
||||||
|
/// ```
|
||||||
fn path_is_file(&self, path: &Path) -> Result<bool>;
|
fn path_is_file(&self, path: &Path) -> Result<bool>;
|
||||||
|
|
||||||
|
/// Creates a new [PathBuf] within the filesystem.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::temp()?;
|
||||||
|
/// let path_of = fs.path_of("subdir/file.txt")?;
|
||||||
|
/// let path = fs.base().join("subdir").join("file.txt");
|
||||||
|
/// assert_eq!(path_of, path);
|
||||||
|
/// ```
|
||||||
fn path_of(&self, path: PathBuf) -> Result<PathBuf>;
|
fn path_of(&self, path: PathBuf) -> Result<PathBuf>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use derive_more::From;
|
||||||
|
|
||||||
use crate::fs::like::FileSystemLike;
|
use crate::fs::like::FileSystemLike;
|
||||||
|
|
||||||
mod like;
|
pub mod like;
|
||||||
mod real;
|
mod real;
|
||||||
mod temp;
|
mod temp;
|
||||||
|
|
||||||
|
@ -22,14 +22,33 @@ impl std::error::Error for Error {}
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
pub type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
|
/// Creates a new handle for accessing the filesystem.
|
||||||
|
///
|
||||||
|
/// The base parameter is the root directory that all operations must be within.
|
||||||
pub const fn new(base: PathBuf) -> FileSystem {
|
pub const fn new(base: PathBuf) -> FileSystem {
|
||||||
FileSystem::Real(real::new(base))
|
FileSystem::Real(real::new(base))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new handle for accessing a temporary directory on the filesystem.
|
||||||
|
///
|
||||||
|
/// When this handle is dropped, the temporary directory is deleted.
|
||||||
|
///
|
||||||
|
/// Calling base on the handle will return the temporary directory.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// This will create a temporary directory and create a file inside it called 'foo':
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let fs = kxio::fs::temp()?;
|
||||||
|
/// let file_name = fs.base().join("foo");
|
||||||
|
/// fs.file_write(&file_name, "contents")?;
|
||||||
|
/// ```
|
||||||
pub fn temp() -> Result<FileSystem> {
|
pub fn temp() -> Result<FileSystem> {
|
||||||
temp::new().map(FileSystem::Temp)
|
temp::new().map(FileSystem::Temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub enum FileSystem {
|
pub enum FileSystem {
|
||||||
Real(real::RealFileSystem),
|
Real(real::RealFileSystem),
|
||||||
Temp(temp::TempFileSystem),
|
Temp(temp::TempFileSystem),
|
||||||
|
|
|
@ -4,6 +4,7 @@ pub const fn new(base: PathBuf) -> RealFileSystem {
|
||||||
RealFileSystem { base }
|
RealFileSystem { base }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct RealFileSystem {
|
pub struct RealFileSystem {
|
||||||
base: PathBuf,
|
base: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ pub(super) fn new() -> super::Result<TempFileSystem> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
pub struct TempFileSystem {
|
pub struct TempFileSystem {
|
||||||
real: super::real::RealFileSystem,
|
real: super::real::RealFileSystem,
|
||||||
_temp_dir: Arc<Mutex<TempDir>>,
|
_temp_dir: Arc<Mutex<TempDir>>,
|
||||||
|
|
Loading…
Reference in a new issue