gveditor_core_api/filesystems/
mod.rs1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::path::Path;
4mod local;
5pub use local::LocalFilesystem;
6
7use crate::Errors;
8
9#[derive(Serialize, Deserialize, Debug, Clone)]
11pub enum FilesystemErrors {
12 FilesystemNotFound,
13 FileNotFound,
14 FileNotSupported,
15 PermissionDenied,
16}
17
18#[async_trait]
20pub trait Filesystem {
21 async fn read_file_by_path(&self, path: &str) -> Result<FileInfo, Errors>;
22 async fn write_file_by_path(&self, path: &str, content: &str) -> Result<(), Errors>;
23 async fn list_dir_by_path(&self, path: &str) -> Result<Vec<DirItemInfo>, Errors>;
24}
25
26#[derive(Serialize, Deserialize, Debug, Clone)]
27pub struct DirItemInfo {
28 pub path: String,
29 pub name: String,
30 pub is_file: bool,
31}
32
33#[derive(Serialize, Deserialize, Debug, Clone)]
34pub enum FileFormat {
35 Unknown,
36 Binary,
37 Text(String),
38}
39
40pub fn get_format_from_path(path: &str) -> FileFormat {
47 if let Some(ext) = Path::new(path).extension() {
48 match ext.to_str().unwrap() {
49 "html" => FileFormat::Text("HTML".to_string()),
51 "css" => FileFormat::Text("CSS".to_string()),
53 "rs" => FileFormat::Text("Rust".to_string()),
55 "js" => FileFormat::Text("JavaScript".to_string()),
57 "jsx" => FileFormat::Text("JavaScript".to_string()),
58 "ts" => FileFormat::Text("TypeScript".to_string()),
60 "tsx" => FileFormat::Text("TypeScript".to_string()),
61 "php" => FileFormat::Text("PHP".to_string()),
63 "py" => FileFormat::Text("Python".to_string()),
65 _ => FileFormat::Unknown,
67 }
68 } else {
69 FileFormat::Unknown
70 }
71}
72
73#[derive(Serialize, Deserialize, Debug, Clone)]
75pub struct FileInfo {
76 pub content: String,
77 pub format: FileFormat,
78 pub path: String,
79}
80
81impl FileInfo {
82 pub fn new(path: &str, content: String) -> Self {
83 Self {
84 content,
85 format: get_format_from_path(path),
86 path: path.to_owned(),
87 }
88 }
89}