feat(fs): add new contructors for real and temp

This commit is contained in:
Paul Campbell 2024-04-27 18:32:26 +01:00
parent a917b21f50
commit 84640bb2dd
4 changed files with 31 additions and 3 deletions

View file

@ -7,18 +7,27 @@ use std::{
use tempfile::{tempdir, TempDir};
use tracing::{debug, info};
pub fn real(cwd: Option<PathBuf>) -> FileSystem {
let cwd = cwd.unwrap_or_default();
FileSystem::Real(RealFileSystem::new(cwd))
}
pub fn temp() -> std::io::Result<FileSystem> {
TempFileSystem::new().map(FileSystem::Temp)
}
#[derive(Clone, Debug)]
pub enum FileSystem {
Real(RealFileSystem),
Temp(TempFileSystem),
}
impl FileSystem {
#[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::real()] instead")]
pub fn new_real(cwd: Option<PathBuf>) -> Self {
let cwd = cwd.unwrap_or_default();
Self::Real(RealFileSystem::new(cwd))
real(cwd)
}
#[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::temp()] instead")]
pub fn new_temp() -> std::io::Result<Self> {
TempFileSystem::new().map(Self::Temp)
temp()
}
}
impl Deref for FileSystem {

View file

@ -2,3 +2,6 @@
pub mod filesystem;
#[cfg(feature = "network")]
pub mod network;
#[cfg(test)]
mod tests;

12
src/tests/filesystem.rs Normal file
View file

@ -0,0 +1,12 @@
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn write_read_file_exists() -> TestResult {
let fs = crate::filesystem::temp()?;
let pathbuf = fs.write_file("foo", "content")?;
let c = fs.read_file("foo")?;
assert_eq!(c, "content");
assert!(fs.file_exists(&pathbuf));
Ok(())
}

4
src/tests/mod.rs Normal file
View file

@ -0,0 +1,4 @@
#[cfg(feature = "fs")]
pub mod filesystem;
#[cfg(feature = "network")]
pub mod network;