oak_vfs/
lib.rs

1#![feature(new_range_api)]
2
3use oak_core::source::Source;
4use serde::{Deserialize, Serialize};
5
6mod line_map;
7pub use line_map::LineMap;
8
9/// Type of a file in the VFS.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11pub enum FileType {
12    File,
13    Directory,
14    Other,
15}
16
17/// Metadata for a file or directory in the VFS.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct FileMetadata {
20    pub file_type: FileType,
21    pub len: u64,
22    pub modified: Option<u64>, // Unix timestamp in seconds
23}
24
25pub mod vfs;
26pub use vfs::{DiskVfs, MemoryVfs};
27
28/// A trait for a Virtual File System that can provide source content and location mapping.
29pub trait Vfs: Send + Sync {
30    /// The type of source returned by this VFS.
31    type Source: Source + 'static;
32
33    /// Get the source for the given URI.
34    fn get_source(&self, uri: &str) -> Option<Self::Source>;
35
36    /// Check if a path exists at the given URI.
37    fn exists(&self, uri: &str) -> bool;
38
39    /// Read the metadata for the given URI.
40    fn metadata(&self, uri: &str) -> Option<FileMetadata>;
41
42    /// Read the contents of a directory at the given URI.
43    /// Returns a list of URIs or names.
44    fn read_dir(&self, uri: &str) -> Option<Vec<String>>;
45
46    /// Check if the given URI points to a file.
47    fn is_file(&self, uri: &str) -> bool {
48        self.metadata(uri).map(|m| m.file_type == FileType::File).unwrap_or(false)
49    }
50
51    /// Check if the given URI points to a directory.
52    fn is_dir(&self, uri: &str) -> bool {
53        self.metadata(uri).map(|m| m.file_type == FileType::Directory).unwrap_or(false)
54    }
55
56    fn line_map(&self, uri: &str) -> Option<LineMap> {
57        self.get_source(uri).map(|s| LineMap::from_source(&s))
58    }
59}
60
61/// A trait for a Virtual File System that supports writing.
62pub trait WritableVfs: Vfs {
63    /// Update or create a file with the given content.
64    fn write_file(&self, uri: &str, content: String);
65
66    /// Remove a file from the VFS.
67    fn remove_file(&self, uri: &str);
68}