feat(fs): add new contructors for real and temp
This commit is contained in:
parent
a917b21f50
commit
8d3f5df378
4 changed files with 32 additions and 3 deletions
|
@ -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 {
|
||||
|
|
|
@ -2,3 +2,6 @@
|
|||
pub mod filesystem;
|
||||
#[cfg(feature = "network")]
|
||||
pub mod network;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
12
src/tests/filesystem.rs
Normal file
12
src/tests/filesystem.rs
Normal 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(())
|
||||
}
|
5
src/tests/mod.rs
Normal file
5
src/tests/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
#[cfg(feature = "fs")]
|
||||
pub mod filesystem;
|
||||
|
||||
// #[cfg(feature = "network")]
|
||||
// pub mod network;
|
Loading…
Reference in a new issue