#[derive(Debug, Clone)]
pub struct Options {
pub indent: String,
pub trailing_commas: bool,
pub quote_style: QuoteStyle,
pub binary_encoding: BinaryEncoding,
pub unquoted_keys: bool,
pub leading_plus: bool,
pub sort_keys: bool,
pub escape_unicode: bool,
pub use_zulu: bool,
pub timestamp_precision: TimestampPrecision,
}
impl Default for Options {
fn default() -> Self {
Self::pretty()
}
}
impl Options {
pub fn compact() -> Self {
Self {
indent: String::new(),
trailing_commas: false,
quote_style: QuoteStyle::Double,
binary_encoding: BinaryEncoding::Base64,
unquoted_keys: true,
leading_plus: false,
sort_keys: false,
escape_unicode: true,
use_zulu: true,
timestamp_precision: TimestampPrecision::Auto,
}
}
pub fn pretty() -> Self {
Self {
indent: " ".to_string(),
trailing_commas: true,
quote_style: QuoteStyle::Double,
binary_encoding: BinaryEncoding::Base64,
unquoted_keys: true,
leading_plus: false,
sort_keys: true,
escape_unicode: false,
use_zulu: true,
timestamp_precision: TimestampPrecision::Auto,
}
}
pub fn with_indent(mut self, indent: impl Into<String>) -> Self {
self.indent = indent.into();
self
}
pub fn with_trailing_commas(mut self, enable: bool) -> Self {
self.trailing_commas = enable;
self
}
pub fn with_quote_style(mut self, style: QuoteStyle) -> Self {
self.quote_style = style;
self
}
pub fn with_binary_encoding(mut self, encoding: BinaryEncoding) -> Self {
self.binary_encoding = encoding;
self
}
pub fn with_unquoted_keys(mut self, enable: bool) -> Self {
self.unquoted_keys = enable;
self
}
pub fn with_leading_plus(mut self, enable: bool) -> Self {
self.leading_plus = enable;
self
}
pub fn with_sort_keys(mut self, enable: bool) -> Self {
self.sort_keys = enable;
self
}
pub fn with_escape_unicode(mut self, enable: bool) -> Self {
self.escape_unicode = enable;
self
}
pub fn with_use_zulu(mut self, enable: bool) -> Self {
self.use_zulu = enable;
self
}
pub fn with_timestamp_precision(mut self, precision: TimestampPrecision) -> Self {
self.timestamp_precision = precision;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuoteStyle {
Double,
Single,
PreferDouble,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryEncoding {
Base64,
Hex,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimestampPrecision {
Auto,
Seconds,
Milliseconds,
Microseconds,
Nanoseconds,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compact_options() {
let opts = Options::compact();
assert!(opts.indent.is_empty());
assert!(!opts.trailing_commas);
assert!(opts.unquoted_keys);
}
#[test]
fn test_pretty_options() {
let opts = Options::pretty();
assert_eq!(opts.indent, " ");
assert!(opts.trailing_commas);
assert!(opts.unquoted_keys);
}
#[test]
fn test_builder_pattern() {
let opts = Options::compact()
.with_indent("\t")
.with_trailing_commas(true)
.with_quote_style(QuoteStyle::Single);
assert_eq!(opts.indent, "\t");
assert!(opts.trailing_commas);
assert_eq!(opts.quote_style, QuoteStyle::Single);
}
}