Skip to main content

rspack_fs/
intermediate.rs

1use std::fmt::Debug;
2
3use rspack_paths::Utf8Path;
4
5use super::Result;
6use crate::{Error, WritableFileSystem};
7
8pub trait IntermediateFileSystem:
9  WritableFileSystem + IntermediateFileSystemExtras + Debug + Send + Sync
10{
11}
12
13#[async_trait::async_trait]
14pub trait IntermediateFileSystemExtras: Debug + Send + Sync {
15  async fn rename(&self, from: &Utf8Path, to: &Utf8Path) -> Result<()>;
16  async fn create_read_stream(&self, file: &Utf8Path) -> Result<Box<dyn ReadStream>>;
17  async fn create_write_stream(&self, file: &Utf8Path) -> Result<Box<dyn WriteStream>>;
18}
19
20#[async_trait::async_trait]
21pub trait ReadStream: Debug + Sync + Send {
22  async fn read_line(&mut self) -> Result<String> {
23    match String::from_utf8(self.read_until(b'\n').await?) {
24      Ok(s) => Ok(s),
25      Err(_) => Err(Error::Io(std::io::Error::other("invalid utf8 line"))),
26    }
27  }
28  async fn read(&mut self, length: usize) -> Result<Vec<u8>>;
29  async fn read_until(&mut self, byte: u8) -> Result<Vec<u8>>;
30  async fn read_to_end(&mut self) -> Result<Vec<u8>>;
31  async fn skip(&mut self, offset: usize) -> Result<()>;
32  async fn close(&mut self) -> Result<()>;
33}
34
35#[async_trait::async_trait]
36pub trait WriteStream: Debug + Sync + Send {
37  async fn write_line(&mut self, line: &str) -> Result<()> {
38    self.write(line.as_bytes()).await?;
39    self.write(b"\n").await?;
40    Ok(())
41  }
42  async fn write(&mut self, buf: &[u8]) -> Result<usize>;
43  async fn write_all(&mut self, buf: &[u8]) -> Result<()>;
44  async fn flush(&mut self) -> Result<()>;
45  async fn close(&mut self) -> Result<()>;
46}