Skip to main content

vvv_engine/
workspace.rs

1//! A root directory viewed through a [`Vfs`].
2
3use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6use crate::{DiskVfs, Overlay, Vfs, VfsError};
7use vvv_core::SourceText;
8
9#[derive(Clone)]
10pub struct Workspace {
11    root: PathBuf,
12    vfs: Arc<dyn Vfs>,
13}
14
15impl Workspace {
16    /// The tree under `root` on disk, `root` made absolute.
17    pub fn disk(root: impl AsRef<Path>) -> Result<Self, VfsError> {
18        let root = root.as_ref();
19        let root = root.canonicalize().map_err(|source| VfsError::Io {
20            path: root.to_path_buf(),
21            source,
22        })?;
23        Ok(Self::new(root, Arc::new(DiskVfs::new())))
24    }
25
26    pub fn new(root: impl Into<PathBuf>, vfs: Arc<dyn Vfs>) -> Self {
27        Self {
28            root: root.into(),
29            vfs,
30        }
31    }
32
33    /// This workspace as a plan would leave it: the same root over an
34    /// overlay of its file system. Writes land in the overlay; the real
35    /// files are untouched until a plan is applied here.
36    pub fn staged(&self) -> Workspace {
37        Workspace::new(self.root.clone(), Arc::new(Overlay::over(self.vfs.clone())))
38    }
39
40    pub fn root(&self) -> &Path {
41        &self.root
42    }
43
44    pub fn vfs(&self) -> &dyn Vfs {
45        self.vfs.as_ref()
46    }
47
48    pub fn files(&self) -> Result<Vec<PathBuf>, VfsError> {
49        self.vfs.walk(&self.root)
50    }
51
52    /// Load a file given either an absolute path or one relative to the root.
53    pub fn load(&self, path: &Path) -> Result<SourceFile, VfsError> {
54        let text = self.vfs.read(&self.absolute(path))?;
55        Ok(SourceFile::new(self.relative(path), text))
56    }
57
58    /// A user-supplied path (absolute, `./x`, `a/../b`) as a clean root-relative path.
59    pub fn normalize(&self, path: &Path) -> PathBuf {
60        let mut out = PathBuf::new();
61        for component in self.absolute(path).components() {
62            match component {
63                std::path::Component::CurDir => {}
64                std::path::Component::ParentDir => {
65                    out.pop();
66                }
67                other => out.push(other),
68            }
69        }
70        self.relative(&out)
71    }
72
73    /// Path as handed to the [`Vfs`].
74    pub fn absolute(&self, path: &Path) -> PathBuf {
75        if path.is_absolute() {
76            path.to_path_buf()
77        } else {
78            self.root.join(path)
79        }
80    }
81
82    /// Path as shown to users: relative to the root when possible.
83    pub fn relative(&self, path: &Path) -> PathBuf {
84        path.strip_prefix(&self.root)
85            .map(Path::to_path_buf)
86            .unwrap_or_else(|_| path.to_path_buf())
87    }
88}
89
90impl std::fmt::Debug for Workspace {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        f.debug_struct("Workspace")
93            .field("root", &self.root)
94            .finish_non_exhaustive()
95    }
96}
97
98/// A loaded file, addressed by its workspace-relative path.
99#[derive(Debug, Clone)]
100pub struct SourceFile {
101    path: PathBuf,
102    source: SourceText,
103}
104
105impl SourceFile {
106    pub fn new(path: impl Into<PathBuf>, text: impl Into<SourceText>) -> Self {
107        Self {
108            path: path.into(),
109            source: text.into(),
110        }
111    }
112
113    pub fn path(&self) -> &Path {
114        &self.path
115    }
116
117    pub fn source(&self) -> &SourceText {
118        &self.source
119    }
120
121    pub fn text(&self) -> &str {
122        self.source.as_str()
123    }
124}