use super::{
super::{annotate::*, normal::*, *},
errors::*,
serializer::*,
};
use {
depiction::*,
std::{fs::*, io, path},
};
pub struct RepresentationWriter {
format: Option<Format>,
pretty: bool,
base64: bool,
}
impl RepresentationWriter {
pub fn new(format: Option<Format>, pretty: bool, base64: bool) -> Self {
Self { format, pretty, base64 }
}
pub fn write_to_file_or_stdout<PathT, AnnotatedT>(
&self,
variant: &Variant<AnnotatedT>,
quiet: bool,
quiet_empty: bool,
trace: bool,
path: Option<PathT>,
) -> Result<(), SerializeError>
where
PathT: AsRef<path::Path>,
AnnotatedT: Annotated + Clone + Default,
{
let format = if trace && let Some(format) = self.format { format.as_str() } else { "debug depiction" };
if let Some(path) = path {
let path = path.as_ref();
if trace {
tracing::info!("writing {} to file: {}", format, path.display());
}
self.write(variant, &mut io::BufWriter::new(File::create(path)?))?;
} else if quiet {
if quiet_empty {
if trace {
tracing::info!("writing {} to empty", format);
}
self.write(variant, &mut io::empty())?;
}
} else if self.is_binary() {
if trace {
tracing::info!("writing {} to stdout (raw)", format);
}
self.write(variant, &mut io::stdout())?;
} else {
if trace {
tracing::info!("writing {} to stdout", format);
}
self.write(variant, &mut anstream::stdout())?;
}
Ok(())
}
pub fn write<AnnotatedT, WriteT>(
&self,
variant: &Variant<AnnotatedT>,
mut writer: WriteT,
) -> Result<(), SerializeError>
where
AnnotatedT: Annotated + Clone + Default,
WriteT: io::Write,
{
match self.format {
Some(format) => Serializer::new(format)
.with_pretty(self.pretty)
.with_base64(self.base64)
.write_modal(variant, &mut writer),
None => Ok(if self.pretty {
variant.write_default_depiction(&mut writer)
} else {
variant.write_plain_depiction(&mut writer)
}?),
}
}
fn is_binary(&self) -> bool {
if let Some(format) = self.format
&& format.is_binary()
&& !self.base64
{
true
} else {
false
}
}
}