gveditor_core_api/filesystems/
mod.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::path::Path;
4mod local;
5pub use local::LocalFilesystem;
6
7use crate::Errors;
8
9/// Filesystem errors
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub enum FilesystemErrors {
12    FilesystemNotFound,
13    FileNotFound,
14    FileNotSupported,
15    PermissionDenied,
16}
17
18/// Filesystem interface
19#[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
40/// Returns the content format of the give file
41///
42/// # Arguments
43///
44/// * `path`   - The path of the file
45///
46pub 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
50            "html" => FileFormat::Text("HTML".to_string()),
51            // CSS
52            "css" => FileFormat::Text("CSS".to_string()),
53            // Rust
54            "rs" => FileFormat::Text("Rust".to_string()),
55            // Javascript
56            "js" => FileFormat::Text("JavaScript".to_string()),
57            "jsx" => FileFormat::Text("JavaScript".to_string()),
58            // TypeScript
59            "ts" => FileFormat::Text("TypeScript".to_string()),
60            "tsx" => FileFormat::Text("TypeScript".to_string()),
61            // PHP
62            "php" => FileFormat::Text("PHP".to_string()),
63            // Python
64            "py" => FileFormat::Text("Python".to_string()),
65            // Not identified
66            _ => FileFormat::Unknown,
67        }
68    } else {
69        FileFormat::Unknown
70    }
71}
72
73/// Contains information about a file
74#[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}