use crate::{io::Cursor, DiskImage, DiskImageError, DiskImageFileFormat, ImageParser};
use std::path::PathBuf;
#[derive(Debug, Default)]
pub struct ImageWriter {
pub path: Option<PathBuf>,
pub format: Option<DiskImageFileFormat>,
}
impl ImageWriter {
pub fn new() -> Self {
Default::default()
}
pub fn with_format(self, format: DiskImageFileFormat) -> Self {
Self {
format: Some(format),
..self
}
}
pub fn with_path(self, path: PathBuf) -> Self {
Self {
path: Some(path),
..self
}
}
pub fn write(self, image: &mut DiskImage) -> Result<(), DiskImageError> {
if self.path.is_none() {
return Err(DiskImageError::ParameterError);
}
if self.format.is_none() {
return Err(DiskImageError::ParameterError);
}
let path = self.path.unwrap();
let format = self.format.unwrap();
let mut buf = Cursor::new(Vec::with_capacity(1_000_000));
format.save_image(image, &mut buf)?;
let data = buf.into_inner();
std::fs::write(path, data)?;
Ok(())
}
}