File size: 3,030 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use anyhow::{Result, bail};
use auto_hash_map::AutoMap;
use include_dir::{Dir, DirEntry};
use turbo_rcstr::RcStr;
use turbo_tasks::{ValueToString, Vc};

use crate::{
    File, FileContent, FileMeta, FileSystem, FileSystemPath, LinkContent, RawDirectoryContent,
    RawDirectoryEntry,
};

#[turbo_tasks::value(serialization = "none", cell = "new", eq = "manual")]
pub struct EmbeddedFileSystem {
    name: RcStr,
    #[turbo_tasks(trace_ignore)]
    dir: &'static Dir<'static>,
}

impl EmbeddedFileSystem {
    pub(super) fn new(name: RcStr, dir: &'static Dir<'static>) -> Vc<EmbeddedFileSystem> {
        EmbeddedFileSystem { name, dir }.cell()
    }
}

#[turbo_tasks::value_impl]
impl FileSystem for EmbeddedFileSystem {
    #[turbo_tasks::function]
    async fn read(&self, path: FileSystemPath) -> Result<Vc<FileContent>> {
        let file = match self.dir.get_file(&path.path) {
            Some(file) => file,
            None => return Ok(FileContent::NotFound.cell()),
        };

        Ok(File::from(file.contents()).into())
    }

    #[turbo_tasks::function]
    fn read_link(&self, _path: FileSystemPath) -> Vc<LinkContent> {
        LinkContent::NotFound.cell()
    }

    #[turbo_tasks::function]
    async fn raw_read_dir(&self, path: FileSystemPath) -> Result<Vc<RawDirectoryContent>> {
        let path_str = &path.path;
        let dir = match (path_str.as_str(), self.dir.get_dir(path_str)) {
            ("", _) => self.dir,
            (_, Some(dir)) => dir,
            (_, None) => return Ok(RawDirectoryContent::NotFound.cell()),
        };

        let mut converted_entries = AutoMap::new();
        for e in dir.entries() {
            let entry_name: RcStr = e
                .path()
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .into();

            converted_entries.insert(
                entry_name,
                match e {
                    DirEntry::Dir(_) => RawDirectoryEntry::Directory,
                    DirEntry::File(_) => RawDirectoryEntry::File,
                },
            );
        }

        Ok(RawDirectoryContent::new(converted_entries))
    }

    #[turbo_tasks::function]
    fn write(&self, _path: FileSystemPath, _content: Vc<FileContent>) -> Result<Vc<()>> {
        bail!("Writing is not possible to the embedded filesystem")
    }

    #[turbo_tasks::function]
    fn write_link(&self, _path: FileSystemPath, _target: Vc<LinkContent>) -> Result<Vc<()>> {
        bail!("Writing is not possible to the embedded filesystem")
    }

    #[turbo_tasks::function]
    async fn metadata(&self, path: FileSystemPath) -> Result<Vc<FileMeta>> {
        if self.dir.get_entry(&path.path).is_none() {
            bail!("path not found, can't read metadata");
        }

        Ok(FileMeta::default().cell())
    }
}

#[turbo_tasks::value_impl]
impl ValueToString for EmbeddedFileSystem {
    #[turbo_tasks::function]
    fn to_string(&self) -> Vc<RcStr> {
        Vc::cell(self.name.clone())
    }
}