Skip to main content

oak_vfs/
lib.rs

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