#[derive(Debug, Clone)]
pub struct Options {
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 {
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,
}
}
}
impl Options {
pub fn new() -> Self {
Self::default()
}
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_default_options() {
let opts = Options::default();
assert_eq!(opts.quote_style, QuoteStyle::Double);
assert_eq!(opts.binary_encoding, BinaryEncoding::Base64);
assert!(opts.unquoted_keys);
assert!(!opts.leading_plus);
assert!(opts.sort_keys);
assert!(!opts.escape_unicode);
assert!(opts.use_zulu);
assert_eq!(opts.timestamp_precision, TimestampPrecision::Auto);
}
#[test]
fn test_builder_pattern() {
let opts = Options::new()
.with_quote_style(QuoteStyle::Single)
.with_binary_encoding(BinaryEncoding::Hex)
.with_unquoted_keys(false)
.with_sort_keys(false);
assert_eq!(opts.quote_style, QuoteStyle::Single);
assert_eq!(opts.binary_encoding, BinaryEncoding::Hex);
assert!(!opts.unquoted_keys);
assert!(!opts.sort_keys);
}
}