2024-02-12 12:18:37 -08:00
|
|
|
//! Functions for interacting with files on a machine.
|
|
|
|
|
2024-12-03 16:39:51 +13:00
|
|
|
use anyhow::Result;
|
|
|
|
|
2025-05-13 14:06:10 -07:00
|
|
|
use crate::{execution::typed_path::TypedPath, SourceRange};
|
2024-12-03 16:39:51 +13:00
|
|
|
|
2024-02-12 12:18:37 -08:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub mod local;
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub use local::FileManager;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[cfg(not(test))]
|
|
|
|
pub mod wasm;
|
2024-04-09 18:05:36 -07:00
|
|
|
|
2024-02-12 12:18:37 -08:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[cfg(not(test))]
|
|
|
|
pub use wasm::FileManager;
|
|
|
|
|
2024-03-12 13:37:47 -07:00
|
|
|
#[async_trait::async_trait]
|
2024-02-12 12:18:37 -08:00
|
|
|
pub trait FileSystem: Clone {
|
|
|
|
/// Read a file from the local file system.
|
2025-05-06 20:04:34 -07:00
|
|
|
async fn read(&self, path: &TypedPath, source_range: SourceRange) -> Result<Vec<u8>, crate::errors::KclError>;
|
2024-02-12 12:18:37 -08:00
|
|
|
|
2024-10-17 00:48:33 -04:00
|
|
|
/// Read a file from the local file system.
|
2025-05-06 20:04:34 -07:00
|
|
|
async fn read_to_string(
|
2024-10-17 00:48:33 -04:00
|
|
|
&self,
|
2025-05-06 20:04:34 -07:00
|
|
|
path: &TypedPath,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2024-10-17 00:48:33 -04:00
|
|
|
) -> Result<String, crate::errors::KclError>;
|
|
|
|
|
2024-02-12 12:18:37 -08:00
|
|
|
/// Check if a file exists on the local file system.
|
2025-05-06 20:04:34 -07:00
|
|
|
async fn exists(&self, path: &TypedPath, source_range: SourceRange) -> Result<bool, crate::errors::KclError>;
|
2024-02-19 12:33:16 -08:00
|
|
|
|
|
|
|
/// Get all the files in a directory recursively.
|
2025-05-06 20:04:34 -07:00
|
|
|
async fn get_all_files(
|
2024-02-19 12:33:16 -08:00
|
|
|
&self,
|
2025-05-06 20:04:34 -07:00
|
|
|
path: &TypedPath,
|
2024-12-03 16:39:51 +13:00
|
|
|
source_range: SourceRange,
|
2025-05-06 20:04:34 -07:00
|
|
|
) -> Result<Vec<TypedPath>, crate::errors::KclError>;
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|