use crate::Result;
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub enum Asset {
Disk(PathBuf),
Internal(InternalAsset),
}
#[derive(Debug)]
pub struct InternalAsset {
contents: &'static str,
name: &'static str,
}
impl Asset {
pub fn internal(name: &'static str, contents: &'static str) -> Self {
Asset::Internal(InternalAsset { name, contents })
}
pub fn path(path: PathBuf) -> Self {
Asset::Disk(path)
}
pub fn copy_to(&self, output: &Path) -> Result<()> {
match self {
Asset::Internal(int) => {
let path = output.join(int.name);
let mut file = File::create(&path)?;
write!(file, "{}", int.contents)?;
Ok(())
}
Asset::Disk(path) => {
if let Some(name) = path.file_name() {
fs::copy(path, output.join(name))?;
}
Ok(())
}
}
}
}