pub mod filesystem;
use std::{
ops::Deref,
sync::Arc,
};
pub use filesystem::*;
pub mod errors;
pub use errors::Result;
pub mod macros;
pub mod memory;
pub mod physical;
mod uri_path;
pub use {
memory::MemoryFileSystem,
physical::PhysicalFileSystem,
uri_path::*,
};
pub enum FS {
Physical(Arc<PhysicalFileSystem>),
Memory(Arc<MemoryFileSystem>),
}
impl From<PhysicalFileSystem> for FS {
fn from(fs: PhysicalFileSystem) -> Self {
FS::Physical(Arc::new(fs))
}
}
impl From<MemoryFileSystem> for FS {
fn from(fs: MemoryFileSystem) -> Self {
FS::Memory(Arc::new(fs))
}
}
impl std::hash::Hash for FS {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::mem::discriminant(self).hash(state);
match self {
| FS::Physical(fs) => (Arc::as_ptr(fs) as usize).hash(state),
| FS::Memory(fs) => (Arc::as_ptr(fs) as usize).hash(state),
}
}
}
impl Clone for FS {
fn clone(&self) -> Self {
match self {
| FS::Physical(fs) => FS::Physical(Arc::clone(fs)),
| FS::Memory(fs) => FS::Memory(Arc::clone(fs)),
}
}
}
impl std::fmt::Debug for FS {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
| FS::Physical(fs) => write!(f, "{fs:?}"),
| FS::Memory(fs) => write!(f, "{fs:?}"),
}
}
}
impl Deref for FS {
type Target = dyn FileSystem;
fn deref(&self) -> &Self::Target {
match self {
| FS::Physical(fs) => fs.deref(),
| FS::Memory(fs) => fs.deref(),
}
}
}
impl FileSystem for FS {
fn uri(&self) -> crate::Uri {
self.deref().uri()
}
fn open(
&self,
path: &crate::Uri,
options: OpenOptions,
) -> Result<Box<dyn FileHandle>> {
self.deref().open(path, options)
}
fn read(&self, path: &crate::Uri) -> Result<Vec<u8>> {
self.deref().read(path)
}
fn read_to_new_rope(&self, path: &crate::Uri) -> Result<ropey::Rope> {
self.deref().read_to_new_rope(path)
}
fn write(&self, path: &crate::Uri, data: &[u8]) -> Result<()> {
self.deref().write(path, data)
}
fn write_str(&self, path: &crate::Uri, data: &str) -> Result<()> {
self.deref().write_str(path, data)
}
fn append(&self, path: &crate::Uri, data: &[u8]) -> Result<()> {
self.deref().append(path, data)
}
fn delete(&self, path: &crate::Uri) -> Result<()> {
self.deref().delete(path)
}
fn metadata(&self, path: &crate::Uri) -> Result<Metadata> {
self.deref().metadata(path)
}
fn root(&self) -> Result<Box<dyn DirEntry>> {
self.deref().root()
}
fn dir(&self, path: &crate::Uri) -> Result<Box<dyn DirEntry>> {
self.deref().dir(path)
}
fn find(
&self,
path: &crate::Uri,
glob: &[String],
) -> Result<im::Vector<Arc<dyn DirEntry>>> {
self.deref().find(path, glob)
}
fn iter_files(
&self,
) -> Result<Box<dyn Iterator<Item = (crate::Uri, Vec<u8>)> + '_>> {
self.deref().iter_files()
}
}
#[cfg(test)]
mod tests;