use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::path::Path;
mod local;
pub use local::LocalFilesystem;
use crate::Errors;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum FilesystemErrors {
FilesystemNotFound,
FileNotFound,
FileNotSupported,
PermissionDenied,
}
#[async_trait]
pub trait Filesystem {
async fn read_file_by_path(&self, path: &str) -> Result<FileInfo, Errors>;
async fn write_file_by_path(&self, path: &str, content: &str) -> Result<(), Errors>;
async fn list_dir_by_path(&self, path: &str) -> Result<Vec<DirItemInfo>, Errors>;
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DirItemInfo {
pub path: String,
pub name: String,
pub is_file: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum FileFormat {
Unknown,
Binary,
Text(String),
}
pub fn get_format_from_path(path: &str) -> FileFormat {
if let Some(ext) = Path::new(path).extension() {
match ext.to_str().unwrap() {
"html" => FileFormat::Text("HTML".to_string()),
"css" => FileFormat::Text("CSS".to_string()),
"rs" => FileFormat::Text("Rust".to_string()),
"js" => FileFormat::Text("JavaScript".to_string()),
"jsx" => FileFormat::Text("JavaScript".to_string()),
"ts" => FileFormat::Text("TypeScript".to_string()),
"tsx" => FileFormat::Text("TypeScript".to_string()),
"php" => FileFormat::Text("PHP".to_string()),
"py" => FileFormat::Text("Python".to_string()),
_ => FileFormat::Unknown,
}
} else {
FileFormat::Unknown
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FileInfo {
pub content: String,
pub format: FileFormat,
pub path: String,
}
impl FileInfo {
pub fn new(path: &str, content: String) -> Self {
Self {
content,
format: get_format_from_path(path),
path: path.to_owned(),
}
}
}