use std::path::PathBuf;
use crate::{decoder::Decoder, error::BuilderError};
enum Source {
None,
File(PathBuf),
Memory(Vec<u8>),
}
pub struct DecoderBuilder {
source: Source,
}
impl Default for DecoderBuilder {
fn default() -> Self {
Self::new()
}
}
impl DecoderBuilder {
pub fn new() -> Self {
Self {
source: Source::None,
}
}
pub fn use_file(mut self, path: impl Into<PathBuf>) -> Self {
self.source = Source::File(path.into());
self
}
pub fn use_bytes(mut self, bytes: &[u8]) -> Self {
self.source = Source::Memory(bytes.to_vec());
self
}
pub fn build(self) -> Result<Decoder, BuilderError> {
let bytes = match self.source {
Source::File(path) => std::fs::read(path)?,
Source::Memory(data) => data,
Source::None => return Err(BuilderError::NoSource),
};
Ok(Decoder::new(bytes)?)
}
}