use super::AbstractSerializer;
use icu_provider::buf::BufferFormat;
use icu_provider::datagen::*;
use icu_provider::prelude::*;
use std::io;
#[non_exhaustive]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum StyleOption {
Compact,
Pretty,
}
pub struct Serializer {
style: StyleOption,
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub struct Options {
pub style: StyleOption,
}
impl Default for Options {
fn default() -> Self {
Self {
style: StyleOption::Compact,
}
}
}
impl AbstractSerializer for Serializer {
fn serialize(
&self,
obj: &DataPayload<ExportMarker>,
mut sink: &mut dyn io::Write,
) -> Result<(), DataError> {
match self.style {
StyleOption::Compact => obj.serialize(&mut serde_json::Serializer::new(&mut sink)),
StyleOption::Pretty => obj.serialize(&mut serde_json::Serializer::pretty(&mut sink)),
}
.map_err(|e| DataError::custom("JSON serialize").with_display_context(&e))?;
writeln!(sink)?;
Ok(())
}
fn get_buffer_format(&self) -> BufferFormat {
BufferFormat::Json
}
fn is_text_format(&self) -> bool {
true
}
}
impl Serializer {
pub fn new(options: Options) -> Self {
Self {
style: options.style,
}
}
pub fn pretty() -> Self {
Self::new(Options {
style: StyleOption::Pretty,
..Default::default()
})
}
}
impl Default for Serializer {
fn default() -> Self {
Self::new(Default::default())
}
}