stak_file/
file_system.rs

1mod error;
2#[cfg(feature = "libc")]
3mod libc;
4mod memory;
5#[cfg(feature = "std")]
6mod os;
7mod utility;
8mod void;
9
10pub use self::error::FileError;
11use core::error::Error;
12#[cfg(feature = "libc")]
13pub use libc::LibcFileSystem;
14pub use memory::MemoryFileSystem;
15#[cfg(feature = "std")]
16pub use os::OsFileSystem;
17use stak_vm::{Memory, Value};
18pub use void::VoidFileSystem;
19
20/// A file descriptor.
21pub type FileDescriptor = usize;
22
23/// A file system.
24pub trait FileSystem {
25    /// A path.
26    type Path: ?Sized;
27
28    /// A path buffer.
29    type PathBuf: AsRef<Self::Path>;
30
31    /// An error.
32    type Error: Error;
33
34    /// Opens a file and returns its descriptor.
35    fn open(&mut self, path: &Self::Path, output: bool) -> Result<FileDescriptor, Self::Error>;
36
37    /// Closes a file.
38    fn close(&mut self, descriptor: FileDescriptor) -> Result<(), Self::Error>;
39
40    /// Reads a file.
41    fn read(&mut self, descriptor: FileDescriptor) -> Result<Option<u8>, Self::Error>;
42
43    /// Writes a file.
44    fn write(&mut self, descriptor: FileDescriptor, byte: u8) -> Result<(), Self::Error>;
45
46    /// Flushes a file.
47    fn flush(&mut self, descriptor: FileDescriptor) -> Result<(), Self::Error>;
48
49    /// Deletes a file.
50    fn delete(&mut self, path: &Self::Path) -> Result<(), Self::Error>;
51
52    /// Checks if a file exists.
53    fn exists(&self, path: &Self::Path) -> Result<bool, Self::Error>;
54
55    /// Decodes a path.
56    fn decode_path(memory: &Memory, list: Value) -> Result<Self::PathBuf, Self::Error>;
57}