Compare commits

...

15 commits
v1.1.0 ... main

Author SHA1 Message Date
Renovate Bot
d90a1c42c8 fix(deps): update rust crate secrecy to 0.10
Some checks failed
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-09-17 23:16:55 +00:00
Renovate Bot
9943a0fe4e chore(deps): update docker.io/rust docker tag to v1.81
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-09-05 23:46:32 +00:00
Renovate Bot
a2e91a871b fix(deps): update rust crate derive_more to 1.0.0-beta
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-08-01 18:15:41 +00:00
Renovate Bot
ca7be5c484 chore(deps): update docker.io/rust docker tag to v1.80
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-07-25 20:45:42 +00:00
Renovate Bot
7335e78552 chore(deps): update docker.io/woodpeckerci/plugin-docker-buildx docker tag to v4.2
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-07-16 16:30:56 +00:00
Renovate Bot
9c819fd856 chore(deps): update docker.io/woodpeckerci/plugin-docker-buildx docker tag to v4.1
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-07-09 20:00:36 +00:00
Renovate Bot
3f805b3ad5 chore(deps): update docker.io/rust docker tag to v1.79
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-06-13 17:45:39 +00:00
274f70e485 feat: network: add from impl to help discard unit NetResponses
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-06-06 07:09:43 +01:00
d0aee99c83 feat(fs): add dir_read()
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-05-17 12:08:56 +01:00
Renovate Bot
93c74487a1 chore(deps): update docker.io/woodpeckerci/plugin-docker-buildx docker tag to v4
Some checks failed
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline failed
2024-05-07 10:47:42 +00:00
Renovate Bot
f261474ed0 chore(deps): update docker.io/rust docker tag to v1.78
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-05-02 17:45:41 +00:00
faa9f291d4 build(woodpecker): use latest release plugin
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline failed
ci/woodpecker/cron/woodpecker Pipeline was successful
2024-04-30 07:50:55 +01:00
a84248eca3 fix(fs): make FileSystems Clone and Debug
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2024-04-30 07:50:55 +01:00
595a376a62 fix(fs): add missing std::error::Error impl for fs::Error
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline failed
ci/woodpecker/cron/woodpecker Pipeline failed
2024-04-28 19:11:12 +01:00
6995dbedcb build(woodpecker): forgejo releases are marked as stable
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2024-04-28 15:40:25 +01:00
9 changed files with 141 additions and 8 deletions

View file

@ -2,7 +2,7 @@ steps:
update-builder-image:
when:
- event: cron
image: docker.io/woodpeckerci/plugin-docker-buildx:3.2
image: docker.io/woodpeckerci/plugin-docker-buildx:4.2
settings:
username: kemitix
repo: git.kemitix.net/kemitix/kxio-builder
@ -53,7 +53,7 @@ steps:
publish_to_crates_io:
when:
- event: tag
image: docker.io/rust:1.77
image: docker.io/rust:1.81
commands:
- cargo login "$CARGO_REGISTRY_TOKEN"
- cargo publish --registry crates-io --no-verify
@ -63,10 +63,10 @@ steps:
when:
- event: tag
# 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:
base_url: https://git.kemitix.net
api_key:
from_secret: FORGEJO_RELEASE_PLUGIN
target: main
prerelease: true
prerelease: false

View file

@ -1,6 +1,6 @@
[package]
name = "kxio"
version = "1.1.0"
version = "1.2.0"
edition = "2021"
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
description = "Provides injectable Filesystem and Network resources to make code more testable"
@ -23,7 +23,7 @@ tracing = "0.1"
async-trait = "0.1"
http = "1.1"
reqwest = "0.12"
secrecy = "0.8"
secrecy = "0.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-xml-rs = "0.6"
@ -33,8 +33,12 @@ thiserror = "1.0"
tempfile = "3.10"
path-clean = "1.0"
# error handling
derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] }
# boilerplate
derive_more = { version = "1.0.0-beta", features = [
"from",
"display",
"constructor",
] }
[dev-dependencies]
# testing

37
src/fs/dir_item.rs Normal file
View file

@ -0,0 +1,37 @@
use std::{
fs::{DirEntry, ReadDir},
path::PathBuf,
};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum DirItem {
File(PathBuf),
Dir(PathBuf),
SymLink(PathBuf),
Fifo(PathBuf),
Unsupported(PathBuf),
}
#[derive(Debug, derive_more::Constructor)]
pub struct DirItemIterator(ReadDir);
impl Iterator for DirItemIterator {
type Item = super::Result<DirItem>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(map_dir_item)
}
}
fn map_dir_item(item: std::io::Result<DirEntry>) -> super::Result<DirItem> {
let item = item?;
let file_type = item.file_type()?;
if file_type.is_dir() {
Ok(DirItem::Dir(item.path()))
} else if file_type.is_file() {
Ok(DirItem::File(item.path()))
} else if file_type.is_symlink() {
Ok(DirItem::SymLink(item.path()))
} else {
Ok(DirItem::Unsupported(item.path()))
}
}

View file

@ -1,3 +1,5 @@
use crate::fs::DirItem;
use super::Result;
use std::path::{Path, PathBuf};
@ -8,6 +10,9 @@ pub trait FileSystemLike {
fn dir_create(&self, path: &Path) -> Result<()>;
fn dir_create_all(&self, path: &Path) -> Result<()>;
/// Reads the items in a directory and returns them as an iterator.
fn dir_read(&self, path: &Path) -> Result<Box<dyn Iterator<Item = Result<DirItem>>>>;
fn file_read_to_string(&self, path: &Path) -> Result<String>;
fn file_write(&self, path: &Path, contents: &str) -> Result<()>;

View file

@ -8,6 +8,10 @@ mod like;
mod real;
mod temp;
mod dir_item;
pub use dir_item::DirItem;
pub use dir_item::DirItemIterator;
#[derive(Debug, From, derive_more::Display)]
pub enum Error {
Io(std::io::Error),
@ -17,7 +21,14 @@ pub enum Error {
base: PathBuf,
path: PathBuf,
},
#[display("Path must be a directory: {path:?}")]
NotADirectory {
path: PathBuf,
},
}
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;
pub const fn new(base: PathBuf) -> FileSystem {
@ -28,6 +39,7 @@ pub fn temp() -> Result<FileSystem> {
temp::new().map(FileSystem::Temp)
}
#[derive(Clone, Debug)]
pub enum FileSystem {
Real(real::RealFileSystem),
Temp(temp::TempFileSystem),

View file

@ -1,9 +1,12 @@
use std::path::{Path, PathBuf};
use crate::fs::{DirItem, DirItemIterator};
pub const fn new(base: PathBuf) -> RealFileSystem {
RealFileSystem { base }
}
#[derive(Clone, Debug)]
pub struct RealFileSystem {
base: PathBuf,
}
@ -23,6 +26,20 @@ impl super::FileSystemLike for RealFileSystem {
std::fs::create_dir_all(path).map_err(Into::into)
}
fn dir_read(
&self,
path: &Path,
) -> super::Result<Box<dyn Iterator<Item = super::Result<DirItem>>>> {
self.validate(path)?;
if !self.path_is_dir(path)? {
return Err(super::Error::NotADirectory {
path: path.to_path_buf(),
});
}
let read_dir = std::fs::read_dir(path)?;
Ok(Box::new(DirItemIterator::new(read_dir)))
}
fn file_read_to_string(&self, path: &Path) -> super::Result<String> {
self.validate(path)?;
std::fs::read_to_string(path).map_err(Into::into)

View file

@ -14,6 +14,7 @@ pub(super) fn new() -> super::Result<TempFileSystem> {
})
}
#[derive(Clone, Debug)]
pub struct TempFileSystem {
real: super::real::RealFileSystem,
_temp_dir: Arc<Mutex<TempDir>>,

View file

@ -36,3 +36,9 @@ impl<T> NetResponse<T> {
self.response_body
}
}
impl From<NetResponse<()>> for () {
fn from(_value: NetResponse<()>) -> Self {
// ()
}
}

View file

@ -107,6 +107,57 @@ mod dir_create_all {
}
}
mod dir_dir_read {
use crate::fs::DirItem;
use super::*;
#[test]
fn should_return_dir_items() -> TestResult {
let fs = fs::temp()?;
let file1 = fs.base().join("file-1");
let dir = fs.base().join("dir");
let file2 = dir.join("file-2");
fs.file_write(&file1, "file-1")?;
fs.dir_create(&dir)?;
fs.file_write(&file2, "file-2")?;
let items = fs
.dir_read(fs.base())?
.filter_map(|i| i.ok())
.collect::<Vec<_>>();
assert_eq!(items.len(), 2);
assert!(items.contains(&DirItem::File(file1)));
assert!(items.contains(&DirItem::Dir(dir)));
Ok(())
}
#[test]
fn should_fail_on_not_a_dir() -> TestResult {
let fs = fs::temp()?;
let path = fs.base().join("file");
fs.file_write(&path, "contents")?;
let_assert!(Err(fs::Error::NotADirectory { path: _path }) = fs.dir_read(&path));
Ok(())
}
#[test]
fn should_fail_on_path_traversal() -> TestResult {
let fs = fs::temp()?;
let path = fs.base().join("..").join("foo");
let_assert!(
Err(fs::Error::PathTraversal {
base: _base,
path: _path
}) = fs.dir_read(&path)
);
Ok(())
}
}
mod path_exists {
use super::*;
#[test]