use std::{fs, io, path::Path};
use crate::{spectrum::State, Spectrum};
use super::{text, Format};
#[derive(Debug)]
pub struct Builder {
format: Format,
precision: usize,
}
impl Builder {
pub fn set_format(mut self, format: Format) -> Self {
self.format = format;
self
}
pub fn set_precision(mut self, precision: usize) -> Self {
self.precision = precision;
self
}
pub fn write<W, S: State>(self, writer: &mut W, spectrum: &Spectrum<S>) -> io::Result<()>
where
W: io::Write,
{
match self.format {
Format::Text => text::write_spectrum(writer, spectrum, self.precision),
Format::Npy => spectrum.array.write_npy(writer),
}
}
pub fn write_to_stdout<S: State>(self, spectrum: &Spectrum<S>) -> io::Result<()> {
self.write(&mut io::stdout().lock(), spectrum)
}
pub fn write_to_path<P, S: State>(self, path: P, spectrum: &Spectrum<S>) -> io::Result<()>
where
P: AsRef<Path>,
{
self.write(&mut fs::File::create(path)?, spectrum)
}
pub fn write_to_path_or_stdout<P, S: State>(
self,
path: Option<P>,
spectrum: &Spectrum<S>,
) -> io::Result<()>
where
P: AsRef<Path>,
{
match path {
Some(path) => self.write_to_path(path, spectrum),
None => self.write_to_stdout(spectrum),
}
}
}
impl Default for Builder {
fn default() -> Self {
Builder {
format: Format::Text,
precision: 6,
}
}
}