use mini_fs::prelude::*;
use mini_fs::{MiniFs, Store, UserFile};
use std::io::{Cursor, Error, ErrorKind, Read, Result, Seek, SeekFrom};
use std::path::Path;
struct HelloWorldFs;
struct File(Cursor<&'static str>);
impl Store for HelloWorldFs {
type File = File;
fn open_path(&self, path: &Path) -> Result<Self::File> {
if path == Path::new("hello.txt") {
Ok(File(Cursor::new("hello world!")))
} else {
Err(Error::from(ErrorKind::NotFound))
}
}
}
impl UserFile for File {}
impl Read for File {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.0.read(buf)
}
}
impl Seek for File {
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
self.0.seek(pos)
}
}
fn main() {
let fs = MiniFs::new().mount("/files", HelloWorldFs);
let mut s = String::new();
fs.open("/files/hello.txt")
.and_then(|mut file| file.read_to_string(&mut s))
.unwrap();
println!("{}", s); }