2024-04-08 15:19:06 +01:00
|
|
|
use std::{
|
|
|
|
ops::Deref,
|
|
|
|
path::PathBuf,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
|
|
|
|
|
|
|
use tempfile::{tempdir, TempDir};
|
2024-04-10 11:36:00 +01:00
|
|
|
use tracing::{debug, info};
|
2024-04-08 15:19:06 +01:00
|
|
|
|
2024-04-27 18:32:26 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:19:06 +01:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum FileSystem {
|
2024-04-27 18:11:51 +01:00
|
|
|
Real(RealFileSystem),
|
|
|
|
Temp(TempFileSystem),
|
2024-04-08 15:19:06 +01:00
|
|
|
}
|
|
|
|
impl FileSystem {
|
2024-04-27 18:32:26 +01:00
|
|
|
#[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::real()] instead")]
|
2024-04-08 15:19:06 +01:00
|
|
|
pub fn new_real(cwd: Option<PathBuf>) -> Self {
|
2024-04-27 18:32:26 +01:00
|
|
|
real(cwd)
|
2024-04-08 15:19:06 +01:00
|
|
|
}
|
2024-04-27 18:32:26 +01:00
|
|
|
#[deprecated(since = "1.1.0", note = "Use [kxio::filesystem::temp()] instead")]
|
2024-04-08 15:19:06 +01:00
|
|
|
pub fn new_temp() -> std::io::Result<Self> {
|
2024-04-27 18:32:26 +01:00
|
|
|
temp()
|
2024-04-08 15:19:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Deref for FileSystem {
|
2024-04-27 18:11:51 +01:00
|
|
|
type Target = dyn FileSystemLike;
|
2024-04-08 15:19:06 +01:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match self {
|
|
|
|
Self::Real(env) => env,
|
|
|
|
Self::Temp(env) => env,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 18:11:51 +01:00
|
|
|
pub trait FileSystemLike: Sync + Send + std::fmt::Debug {
|
2024-04-08 15:19:06 +01:00
|
|
|
fn cwd(&self) -> &PathBuf;
|
|
|
|
|
|
|
|
fn in_cwd(&self, name: &str) -> PathBuf {
|
|
|
|
self.cwd().join(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_file(&self, file_name: &str, content: &str) -> std::io::Result<PathBuf> {
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{LineWriter, Write};
|
|
|
|
|
|
|
|
let path = self.in_cwd(file_name);
|
2024-04-10 11:36:00 +01:00
|
|
|
debug!("writing to {:?}", path);
|
2024-04-08 15:19:06 +01:00
|
|
|
let file = File::create(path.clone())?;
|
|
|
|
let mut file = LineWriter::new(file);
|
|
|
|
file.write_all(content.as_bytes())?;
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_exists(&self, name: &PathBuf) -> bool {
|
|
|
|
use std::fs::File;
|
|
|
|
File::open(name).is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_file(&self, file_name: &str) -> std::io::Result<String> {
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
let path = self.in_cwd(file_name);
|
2024-04-10 11:36:00 +01:00
|
|
|
debug!("reading from {:?}", path);
|
2024-04-08 15:19:06 +01:00
|
|
|
let mut file = File::open(path)?;
|
|
|
|
let mut content = String::new();
|
|
|
|
file.read_to_string(&mut content)?;
|
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2024-04-27 18:11:51 +01:00
|
|
|
pub struct RealFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
cwd: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2024-04-27 18:11:51 +01:00
|
|
|
pub struct TempFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
cwd: PathBuf,
|
2024-04-27 18:11:51 +01:00
|
|
|
|
|
|
|
// Handle to the temporary directory
|
|
|
|
// When this handle is dropped the directory is deleted
|
|
|
|
_temp_dir: Arc<Mutex<TempDir>>,
|
2024-04-08 15:19:06 +01:00
|
|
|
}
|
|
|
|
|
2024-04-27 18:11:51 +01:00
|
|
|
impl FileSystemLike for TempFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
fn cwd(&self) -> &PathBuf {
|
|
|
|
&self.cwd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 18:11:51 +01:00
|
|
|
impl FileSystemLike for RealFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
fn cwd(&self) -> &PathBuf {
|
|
|
|
&self.cwd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 18:11:51 +01:00
|
|
|
impl RealFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
const fn new(cwd: PathBuf) -> Self {
|
|
|
|
Self { cwd }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 18:11:51 +01:00
|
|
|
impl TempFileSystem {
|
2024-04-08 15:19:06 +01:00
|
|
|
fn new() -> std::io::Result<Self> {
|
|
|
|
let temp_dir = tempdir()?;
|
|
|
|
info!("temp dir: {:?}", temp_dir.path());
|
|
|
|
let cwd = temp_dir.path().to_path_buf();
|
|
|
|
let temp_dir = Arc::new(Mutex::new(temp_dir));
|
2024-04-27 18:11:51 +01:00
|
|
|
Ok(Self {
|
|
|
|
cwd,
|
|
|
|
_temp_dir: temp_dir,
|
|
|
|
})
|
2024-04-08 15:19:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_cwd() {
|
|
|
|
let cwd = PathBuf::from("/tmp");
|
2024-04-27 18:11:51 +01:00
|
|
|
let env = RealFileSystem::new(cwd.clone());
|
2024-04-08 15:19:06 +01:00
|
|
|
assert_eq!(env.cwd(), &cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_create_on_temp_fs() -> std::io::Result<()> {
|
2024-04-27 18:11:51 +01:00
|
|
|
let env = TempFileSystem::new()?;
|
2024-04-08 15:19:06 +01:00
|
|
|
assert!(env.cwd().exists());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_create_on_real_fs() {
|
|
|
|
let cwd = PathBuf::from("/tmp");
|
2024-04-27 18:11:51 +01:00
|
|
|
let env = RealFileSystem::new(cwd.clone());
|
2024-04-08 15:19:06 +01:00
|
|
|
assert_eq!(env.cwd(), &cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_write_and_read_file() -> std::io::Result<()> {
|
2024-04-27 18:11:51 +01:00
|
|
|
let env = TempFileSystem::new()?;
|
2024-04-08 15:19:06 +01:00
|
|
|
let file_name = "test.txt";
|
|
|
|
let content = "Hello, World!";
|
|
|
|
let path = env.write_file(file_name, content)?;
|
|
|
|
assert_eq!(env.read_file(file_name)?, content);
|
|
|
|
assert!(path.exists());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|