Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.6 kB
130 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2021 The Gleam contributors
3
4use camino::{Utf8Path, Utf8PathBuf};
5use gleam_core::{
6 Error, Result,
7 io::{
8 BeamCompiler, Command, CommandExecutor, FileSystemReader, FileSystemWriter, ReadDir, Stdio,
9 WrappedReader, memory::InMemoryFileSystem,
10 },
11};
12use std::collections::HashSet;
13
14#[derive(Clone, Debug, Default)]
15pub struct WasmFileSystem {
16 imfs: InMemoryFileSystem,
17}
18
19impl WasmFileSystem {
20 pub fn reset(&self) {
21 self.imfs.reset();
22 }
23}
24
25impl CommandExecutor for WasmFileSystem {
26 fn exec(&self, _command: Command) -> Result<i32, Error> {
27 Ok(0) // Always succeed.
28 }
29}
30
31impl BeamCompiler for WasmFileSystem {
32 fn compile_beam(
33 &self,
34 _out: &Utf8Path,
35 _lib: &Utf8Path,
36 _modules: &HashSet<Utf8PathBuf>,
37 _stdio: Stdio,
38 ) -> Result<Vec<String>, Error> {
39 Ok(Vec::new()) // Always succeed.
40 }
41}
42
43impl FileSystemWriter for WasmFileSystem {
44 fn delete_directory(&self, path: &Utf8Path) -> Result<(), Error> {
45 tracing::trace!("delete {:?}", path);
46 self.imfs.delete_directory(path)
47 }
48
49 fn copy(&self, _from: &Utf8Path, _to: &Utf8Path) -> Result<(), Error> {
50 Ok(())
51 }
52 fn copy_dir(&self, _: &Utf8Path, _: &Utf8Path) -> Result<(), Error> {
53 Ok(())
54 }
55
56 fn mkdir(&self, path: &Utf8Path) -> Result<(), Error> {
57 tracing::trace!("mkdir {:?}", path);
58 self.imfs.mkdir(path)
59 }
60
61 fn hardlink(&self, _: &Utf8Path, _: &Utf8Path) -> Result<(), Error> {
62 Ok(())
63 }
64
65 fn symlink_dir(&self, _: &Utf8Path, _: &Utf8Path) -> Result<(), Error> {
66 Ok(())
67 }
68
69 fn delete_file(&self, path: &Utf8Path) -> Result<(), Error> {
70 tracing::trace!("delete file {:?}", path);
71 self.imfs.delete_file(path)
72 }
73
74 fn write(&self, path: &Utf8Path, content: &str) -> Result<(), Error> {
75 tracing::trace!("write {:?}", path);
76 self.imfs.write(path, content)
77 }
78
79 fn write_bytes(&self, path: &Utf8Path, content: &[u8]) -> Result<(), Error> {
80 tracing::trace!("write_bytes {:?}", path);
81 self.imfs.write_bytes(path, content)
82 }
83
84 fn exists(&self, path: &Utf8Path) -> bool {
85 self.imfs.exists(path)
86 }
87}
88
89impl FileSystemReader for WasmFileSystem {
90 fn read(&self, path: &Utf8Path) -> Result<String, Error> {
91 tracing::trace!("read {:?}", path);
92 self.imfs.read(path)
93 }
94
95 fn is_file(&self, path: &Utf8Path) -> bool {
96 tracing::info!("is_file {:?}", path);
97 self.imfs.is_file(path)
98 }
99
100 fn is_directory(&self, path: &Utf8Path) -> bool {
101 tracing::trace!("is_directory {:?}", path);
102 self.imfs.is_directory(path)
103 }
104
105 fn reader(&self, path: &Utf8Path) -> Result<WrappedReader, Error> {
106 tracing::trace!("reader {:?}", path);
107 self.imfs.reader(path)
108 }
109
110 fn read_dir(&self, path: &Utf8Path) -> Result<ReadDir> {
111 tracing::trace!("read_dir {:?}", path);
112 self.imfs.read_dir(path)
113 }
114
115 fn modification_time(&self, path: &Utf8Path) -> Result<std::time::SystemTime, Error> {
116 self.imfs.modification_time(path)
117 }
118
119 fn read_bytes(&self, path: &Utf8Path) -> Result<Vec<u8>, Error> {
120 self.imfs.read_bytes(path)
121 }
122
123 fn canonicalise(&self, path: &Utf8Path) -> Result<Utf8PathBuf, Error> {
124 self.imfs.canonicalise(path)
125 }
126
127 fn is_same_file(&self, _left: &Utf8Path, _right: &Utf8Path) -> Result<bool, Error> {
128 unreachable!("is_same_file unimplemented")
129 }
130}