use alloc::string::String;
use alloc::vec::Vec;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileType {
File,
Dir,
Symlink,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirEntry {
pub name: String,
pub file_type: FileType,
}
pub trait Vfs {
fn read(&self, path: &str) -> Result<Vec<u8>>;
fn write(&self, path: &str, data: &[u8]) -> Result<()>;
fn exists(&self, path: &str) -> bool;
fn metadata(&self, path: &str) -> Result<FileType>;
fn create_dir_all(&self, path: &str) -> Result<()>;
fn read_dir(&self, path: &str) -> Result<Vec<DirEntry>>;
fn remove_file(&self, path: &str) -> Result<()>;
fn rename(&self, from: &str, to: &str) -> Result<()>;
fn create_new(&self, path: &str, data: &[u8]) -> Result<()> {
if self.exists(path) {
return Err(Error::Io(alloc::format!("{path}: already exists")));
}
self.write(path, data)
}
fn open_relative(&self, path: &str) -> Result<Self>
where
Self: Sized,
{
let _ = path;
Err(Error::Unsupported(
"vfs: this backend cannot open an alternate location".into(),
))
}
}
#[cfg(feature = "std")]
mod std_impl;
#[cfg(feature = "std")]
pub use std_impl::StdFs;