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 c513664979
commit 4711a2dc05
4 changed files with 31 additions and 3 deletions

View file

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

View file

@ -2,3 +2,6 @@
pub mod filesystem; pub mod filesystem;
#[cfg(feature = "network")] #[cfg(feature = "network")]
pub mod 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;