Skip to main content

swls_core/util/
fs.rs

1use bevy_ecs::prelude::Resource;
2use derive_more::derive::AsRef;
3use std::sync::Arc;
4use tower_lsp::lsp_types::Url;
5
6#[derive(Resource, Clone, AsRef, Debug)]
7pub struct Fs(pub Arc<dyn FsTrait + Sync + Send>);
8
9#[derive(Debug)]
10pub struct File {
11    pub name: String,
12    pub content: String,
13}
14
15#[derive(Debug, Clone)]
16pub struct FsDirEntry {
17    pub name: String,
18    pub path: Url,
19    pub is_dir: bool,
20}
21
22#[tower_lsp::async_trait]
23pub trait FsTrait: Send + Sync + 'static + std::fmt::Debug {
24    fn virtual_url(&self, url: &str) -> Option<crate::lsp_types::Url>;
25    fn lov_url(&self, url: &str, prefix: &str) -> Option<crate::lsp_types::Url> {
26        if !url.starts_with("http") {
27            return None;
28        }
29
30        // let prefix_url = crate::lsp_types::Url::parse(url).ok();
31        // let prefix_origin = prefix_url.as_ref().map(|x| x.path()).unwrap_or("none");
32        let url = self.virtual_url(&format!("{}.ttl", prefix))?;
33        tracing::debug!("lov url {} {} -> {}", url, prefix, url);
34        Some(url)
35    }
36    async fn read_file(&self, url: &crate::lsp_types::Url) -> Option<String>;
37    async fn glob_read(&self, url: &str) -> Option<Vec<File>>;
38    async fn write_file(&self, url: &crate::lsp_types::Url, content: &str) -> Option<()>;
39
40    async fn read_dir(&self, path: &crate::lsp_types::Url) -> Option<Vec<FsDirEntry>>;
41    async fn is_file(&self, path: &crate::lsp_types::Url) -> bool;
42    async fn is_dir(&self, path: &crate::lsp_types::Url) -> bool;
43
44    async fn glob(&self, base: &Url, pattern: &str) -> Option<Vec<FsDirEntry>>;
45}