use std;
use std::io::ErrorKind;
use std::io::Read;
use std::str;
pub struct FileSystem {}
pub enum File {
Native(std::fs::File),
}
pub type IoError = std::io::Error;
pub type IoErrorKind = std::io::ErrorKind;
impl FileSystem {
pub fn open(s: &str) -> Result<File, IoError> {
if s.starts_with("http") {
Err(std::io::Error::new(
ErrorKind::Other,
format!(
"error : could not read {} : http feature not enabled in uni-app::FileSystem",
s
),
))
} else {
Ok(File::Native(std::fs::File::open(s)?))
}
}
}
impl File {
pub fn read_binary(&mut self) -> Result<Vec<u8>, IoError> {
let mut buf = Vec::new();
match self {
File::Native(f) => {
f.read_to_end(&mut buf)?;
}
}
Ok(buf)
}
pub fn read_text(&mut self) -> Result<String, IoError> {
let mut data = String::new();
match self {
File::Native(f) => match f.read_to_string(&mut data) {
Ok(_) => Ok(data),
Err(e) => Err(std::io::Error::new(ErrorKind::Other, e)),
},
}
}
pub fn is_ready(&mut self) -> bool {
true
}
}