use crate::{
constants::DEFAULT_INDENT,
types::{
Delimiter,
KeyFoldingMode,
PathExpansionMode,
},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Indent {
Spaces(usize),
}
impl Default for Indent {
fn default() -> Self {
Indent::Spaces(DEFAULT_INDENT)
}
}
impl Indent {
pub fn get_string(&self, depth: usize) -> String {
if depth == 0 {
return String::new();
}
match self {
Indent::Spaces(count) => {
if *count > 0 {
" ".repeat(*count * depth)
} else {
String::new()
}
}
}
}
pub fn get_spaces(&self) -> usize {
match self {
Indent::Spaces(count) => *count,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodeOptions {
pub delimiter: Delimiter,
pub indent: Indent,
pub key_folding: KeyFoldingMode,
pub flatten_depth: usize,
}
impl Default for EncodeOptions {
fn default() -> Self {
Self {
delimiter: Delimiter::Comma,
indent: Indent::default(),
key_folding: KeyFoldingMode::Off,
flatten_depth: usize::MAX,
}
}
}
impl EncodeOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_delimiter(mut self, delimiter: Delimiter) -> Self {
self.delimiter = delimiter;
self
}
pub fn with_indent(mut self, style: Indent) -> Self {
self.indent = style;
self
}
pub fn with_spaces(mut self, count: usize) -> Self {
self.indent = Indent::Spaces(count);
self
}
pub fn with_key_folding(mut self, mode: KeyFoldingMode) -> Self {
self.key_folding = mode;
self
}
pub fn with_flatten_depth(mut self, depth: usize) -> Self {
self.flatten_depth = depth;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodeOptions {
pub delimiter: Option<Delimiter>,
pub strict: bool,
pub coerce_types: bool,
pub indent: Indent,
pub expand_paths: PathExpansionMode,
}
impl Default for DecodeOptions {
fn default() -> Self {
Self {
delimiter: None,
strict: true,
coerce_types: true,
indent: Indent::default(),
expand_paths: PathExpansionMode::Off,
}
}
}
impl DecodeOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
pub fn with_delimiter(mut self, delimiter: Delimiter) -> Self {
self.delimiter = Some(delimiter);
self
}
pub fn with_coerce_types(mut self, coerce: bool) -> Self {
self.coerce_types = coerce;
self
}
pub fn with_indent(mut self, style: Indent) -> Self {
self.indent = style;
self
}
pub fn with_expand_paths(mut self, mode: PathExpansionMode) -> Self {
self.expand_paths = mode;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_options_indent() {
let opts = EncodeOptions::new().with_spaces(4);
assert_eq!(opts.indent, Indent::Spaces(4));
let opts = EncodeOptions::new().with_indent(Indent::Spaces(2));
assert_eq!(opts.indent, Indent::Spaces(2));
}
#[test]
fn test_decode_options_coerce_types() {
let opts = DecodeOptions::new();
assert!(opts.coerce_types);
let opts = DecodeOptions::new().with_coerce_types(false);
assert!(!opts.coerce_types);
let opts = DecodeOptions::new().with_coerce_types(true);
assert!(opts.coerce_types);
}
}