Skip to main content

turbo_tasks_fs/
virtual_fs.rs

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