-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use polonius_the_crab::{polonius, polonius_return}; | ||
use std::{ | ||
collections::HashMap, | ||
fs::{self, File}, | ||
io, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
pub trait Filesystem { | ||
type File<'a>: io::Write | ||
where | ||
Self: 'a; | ||
|
||
fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()>; | ||
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>>; | ||
|
||
fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>>; | ||
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>>; | ||
} | ||
|
||
pub struct DummyFs { | ||
inner: HashMap<PathBuf, Vec<u8>>, | ||
} | ||
|
||
#[inline] | ||
fn file_not_found() -> io::Error { | ||
io::Error::new(io::ErrorKind::NotFound, "file not found") | ||
} | ||
|
||
impl Filesystem for DummyFs { | ||
type File<'a> = &'a mut Vec<u8>; | ||
|
||
#[inline] | ||
fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()> { | ||
let value = self.inner.get(src).ok_or_else(file_not_found)?; | ||
self.inner.insert(dst.to_path_buf(), value.clone()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>> { | ||
self.inner.get(path).cloned().ok_or_else(file_not_found) | ||
} | ||
|
||
#[inline] | ||
fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>> { | ||
let mut this = self; | ||
// TODO: Remove once we can FINALLY have polonius.. | ||
polonius!(|this| -> io::Result<&'polonius mut Vec<u8>> { | ||
if let Some(value) = this.inner.get_mut(path) { | ||
value.clear(); | ||
polonius_return!(Ok(value)); | ||
} | ||
}); | ||
|
||
this.inner.insert(path.to_path_buf(), Vec::new()); | ||
this.create_or_truncate(path) | ||
} | ||
|
||
#[inline] | ||
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>> { | ||
self.inner.get_mut(path).ok_or_else(file_not_found) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct NativeFs { | ||
_priv: (), | ||
} | ||
|
||
impl Filesystem for NativeFs { | ||
type File<'a> = File; | ||
|
||
#[inline] | ||
fn copy(&mut self, src: &Path, dst: &Path) -> io::Result<()> { | ||
fs::copy(src, dst)?; | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>> { | ||
fs::read(path) | ||
} | ||
|
||
#[inline] | ||
fn create_or_truncate(&mut self, path: &Path) -> io::Result<Self::File<'_>> { | ||
File::options() | ||
.create(true) | ||
.truncate(true) | ||
.write(true) | ||
.open(path) | ||
} | ||
|
||
#[inline] | ||
fn open_append(&mut self, path: &Path) -> io::Result<Self::File<'_>> { | ||
File::options().append(true).open(path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters