extern crate phf;
#[cfg(feature = "flate2")]
extern crate flate2;
use std::borrow::{Borrow, Cow};
use std::io::{self, BufReader, Cursor, Error, ErrorKind, Read};
use std::fs::File;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(feature = "flate2")]
use flate2::bufread::GzDecoder;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Compression {
None,
#[cfg(feature = "flate2")]
Gzip
}
pub struct Files {
#[doc(hidden)]
pub files: phf::Map<&'static str, (Compression, &'static [u8])>,
#[doc(hidden)]
pub passthrough: AtomicBool
}
#[cfg(windows)]
fn as_key(path: &str) -> Cow<str> {
Cow::Owned(path.replace("\\", "/"))
}
#[cfg(not(windows))]
fn as_key(path: &str) -> Cow<str> {
Cow::Borrowed(path)
}
impl Files {
pub fn set_passthrough(&self, enabled: bool) {
self.passthrough.store(enabled, Ordering::Relaxed);
}
pub fn is_available(&self, path: &str) -> bool {
self.files.contains_key(path)
}
pub fn file_names(&'static self) -> FileNames {
FileNames { iter: self.files.keys() }
}
pub fn get(&self, path: &str) -> io::Result<Cow<'static, [u8]>> {
match self.get_raw(path) {
Ok((Compression::None, data)) => Ok(data),
#[cfg(feature = "flate2")]
Ok((Compression::Gzip, compressed)) => {
let mut r = GzDecoder::new(Cursor::new(compressed));
let mut v = Vec::new();
r.read_to_end(&mut v)?;
Ok(Cow::Owned(v))
},
Err(e) => Err(e)
}
}
pub fn get_raw(&self, path: &str) -> io::Result<(Compression, Cow<'static, [u8]>)> {
if self.passthrough.load(Ordering::Relaxed) {
let mut r = BufReader::new(File::open(path)?);
let mut v = Vec::new();
r.read_to_end(&mut v)?;
return Ok((Compression::None, Cow::Owned(v)))
}
let key = as_key(path);
self.files.get(&*key)
.map(|&(c,d)| (c, Cow::Owned(d.to_owned())))
.ok_or_else(|| Error::new(ErrorKind::NotFound, "Key not found"))
}
pub fn read(&self, path: &str) -> io::Result<Box<dyn Read>> {
if self.passthrough.load(Ordering::Relaxed) {
return Ok(Box::new(BufReader::new(File::open(path)?)))
}
let key = as_key(path);
match self.files.get(key.borrow() as &str) {
Some(b) => {
match b.0 {
Compression::None => Ok(Box::new(Cursor::new(b.1))),
#[cfg(feature = "flate2")]
Compression::Gzip => Ok(Box::new(GzDecoder::new(Cursor::new(b.1)))),
}
}
None => Err(Error::new(ErrorKind::NotFound, "Key not found")),
}
}
}
pub struct FileNames {
iter: phf::map::Keys<'static, &'static str, (Compression, &'static [u8])>,
}
impl Iterator for FileNames {
type Item = &'static str;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().cloned()
}
}