Skip to main content

kcl_lib/fs/
mod.rs

1//! Functions for interacting with files on a machine.
2
3use std::sync::Arc;
4
5use anyhow::Result;
6
7use crate::SourceRange;
8use crate::execution::typed_path::TypedPath;
9
10pub mod in_memory;
11#[cfg(not(target_arch = "wasm32"))]
12pub mod local;
13#[cfg(not(target_arch = "wasm32"))]
14pub use local::FileManager;
15
16#[cfg(target_arch = "wasm32")]
17#[cfg(not(test))]
18pub mod wasm;
19
20#[cfg(target_arch = "wasm32")]
21#[cfg(not(test))]
22pub use wasm::FileManager;
23
24pub type FileSystemHandle = Arc<Box<dyn FileSystem>>;
25
26pub fn new_file_system_handle<T>(fs: T) -> FileSystemHandle
27where
28    T: FileSystem + 'static,
29{
30    Arc::new(Box::new(fs) as Box<dyn FileSystem>)
31}
32
33#[async_trait::async_trait]
34pub trait FileSystem: Send + Sync {
35    /// Read a file from the local file system.
36    async fn read(&self, path: &TypedPath, source_range: SourceRange) -> Result<Vec<u8>, crate::errors::KclError>;
37
38    /// Read a file from the local file system.
39    async fn read_to_string(
40        &self,
41        path: &TypedPath,
42        source_range: SourceRange,
43    ) -> Result<String, crate::errors::KclError>;
44
45    /// Check if a file exists on the local file system.
46    async fn exists(&self, path: &TypedPath, source_range: SourceRange) -> Result<bool, crate::errors::KclError>;
47
48    /// Get all the files in a directory recursively.
49    async fn get_all_files(
50        &self,
51        path: &TypedPath,
52        source_range: SourceRange,
53    ) -> Result<Vec<TypedPath>, crate::errors::KclError>;
54}