turbo_tasks_fs/
virtual_fs.rs

1use anyhow::{Result, bail};
2use turbo_rcstr::{RcStr, rcstr};
3use turbo_tasks::{ValueDefault, ValueToString, Vc};
4
5use crate::{FileContent, FileMeta, FileSystem, FileSystemPath, LinkContent, RawDirectoryContent};
6
7#[turbo_tasks::value]
8pub struct VirtualFileSystem {
9    pub name: RcStr,
10}
11
12impl VirtualFileSystem {
13    /// Creates a new [`Vc<VirtualFileSystem>`].
14    ///
15    /// NOTE: This function is not a `turbo_tasks::function` to avoid instances
16    /// being equivalent identity-wise. This ensures that a
17    /// [`FileSystemPath`] created from this [`Vc<VirtualFileSystem>`]
18    /// will never be equivalent, nor be interoperable, with a
19    /// [`FileSystemPath`] created from another
20    /// [`Vc<VirtualFileSystem>`].
21    pub fn new() -> Vc<Self> {
22        Self::cell(VirtualFileSystem {
23            name: rcstr!("virtual file system"),
24        })
25    }
26
27    /// Creates a new [`Vc<VirtualFileSystem>`] with a name.
28    ///
29    /// NOTE: This function is not a `turbo_tasks::function` to avoid instances
30    /// being equivalent identity-wise. This ensures that a
31    /// [`FileSystemPath`] created from this [`Vc<VirtualFileSystem>`]
32    /// will never be equivalent, nor be interoperable, with a
33    /// [`FileSystemPath`] created from another
34    /// [`Vc<VirtualFileSystem>`].
35    pub fn new_with_name(name: RcStr) -> Vc<Self> {
36        Self::cell(VirtualFileSystem { name })
37    }
38}
39
40impl ValueDefault for VirtualFileSystem {
41    fn value_default() -> Vc<Self> {
42        Self::new()
43    }
44}
45
46#[turbo_tasks::value_impl]
47impl FileSystem for VirtualFileSystem {
48    #[turbo_tasks::function]
49    fn read(&self, _fs_path: FileSystemPath) -> Result<Vc<FileContent>> {
50        bail!("Reading is not possible on the virtual file system")
51    }
52
53    #[turbo_tasks::function]
54    fn read_link(&self, _fs_path: FileSystemPath) -> Result<Vc<LinkContent>> {
55        bail!("Reading is not possible on the virtual file system")
56    }
57
58    #[turbo_tasks::function]
59    fn raw_read_dir(&self, _fs_path: FileSystemPath) -> Result<Vc<RawDirectoryContent>> {
60        bail!("Reading is not possible on the virtual file system")
61    }
62
63    #[turbo_tasks::function]
64    fn write(&self, _fs_path: FileSystemPath, _content: Vc<FileContent>) -> Result<Vc<()>> {
65        bail!("Writing is not possible on the virtual file system")
66    }
67
68    #[turbo_tasks::function]
69    fn write_link(&self, _fs_path: FileSystemPath, _target: Vc<LinkContent>) -> Result<Vc<()>> {
70        bail!("Writing is not possible on the virtual file system")
71    }
72
73    #[turbo_tasks::function]
74    fn metadata(&self, _fs_path: FileSystemPath) -> Result<Vc<FileMeta>> {
75        bail!("Reading is not possible on the virtual file system")
76    }
77}
78
79#[turbo_tasks::value_impl]
80impl ValueToString for VirtualFileSystem {
81    #[turbo_tasks::function]
82    fn to_string(&self) -> Vc<RcStr> {
83        Vc::cell(self.name.clone())
84    }
85}