use crate::emitter::{EmitterOptions, EmitterStyles};
use crate::event::ScalarStyle;
use crate::limits::Limits;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseOptions {
pub limits: Limits,
pub merge_keys: bool,
}
impl Default for ParseOptions {
fn default() -> Self {
Self::new()
}
}
impl From<Limits> for ParseOptions {
fn from(limits: Limits) -> Self {
Self::new().with_limits(limits)
}
}
impl ParseOptions {
#[must_use]
pub const fn new() -> Self {
Self {
limits: Limits::default_profile(),
merge_keys: false,
}
}
#[must_use]
pub const fn for_max_input() -> Self {
Self {
limits: Limits::for_max_input(),
merge_keys: false,
}
}
#[must_use]
pub const fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn with_merge_keys(mut self, merge_keys: bool) -> Self {
self.merge_keys = merge_keys;
self
}
}
#[allow(
clippy::struct_excessive_bools,
reason = "the flags are independent emitter options, not a state enum"
)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct EmitOptions {
pub limits: Limits,
pub flow: bool,
pub explicit_start: bool,
pub explicit_end: bool,
pub scalar_style: Option<ScalarStyle>,
pub indent: usize,
pub indent_sequences: bool,
pub styles: Option<EmitterStyles>,
}
impl Default for EmitOptions {
fn default() -> Self {
Self::new()
}
}
impl From<Limits> for EmitOptions {
fn from(limits: Limits) -> Self {
Self::new().with_limits(limits)
}
}
impl From<EmitterOptions> for EmitOptions {
fn from(options: EmitterOptions) -> Self {
Self {
flow: options.flow,
explicit_start: options.explicit_start,
explicit_end: options.explicit_end,
scalar_style: options.scalar_style,
indent: options.indent,
indent_sequences: options.indent_sequences,
..Self::new()
}
}
}
impl From<EmitterStyles> for EmitOptions {
fn from(styles: EmitterStyles) -> Self {
Self::new().with_styles(styles)
}
}
impl EmitOptions {
#[must_use]
pub const fn new() -> Self {
Self {
limits: Limits::default_profile(),
flow: false,
explicit_start: false,
explicit_end: false,
scalar_style: None,
indent: 2,
indent_sequences: false,
styles: None,
}
}
#[must_use]
pub const fn flow() -> Self {
Self::new().with_flow(true)
}
#[must_use]
pub const fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn with_flow(mut self, value: bool) -> Self {
self.flow = value;
self
}
#[must_use]
pub const fn with_explicit_start(mut self, value: bool) -> Self {
self.explicit_start = value;
self
}
#[must_use]
pub const fn with_explicit_end(mut self, value: bool) -> Self {
self.explicit_end = value;
self
}
#[must_use]
pub const fn with_scalar_style(mut self, value: Option<ScalarStyle>) -> Self {
self.scalar_style = value;
self
}
#[must_use]
pub const fn with_indent(mut self, value: usize) -> Self {
self.indent = value;
self
}
#[must_use]
pub const fn with_indent_sequences(mut self, value: bool) -> Self {
self.indent_sequences = value;
self
}
#[must_use]
pub fn with_styles(mut self, styles: EmitterStyles) -> Self {
self.styles = Some(styles);
self
}
pub(crate) const fn emitter_options(&self) -> EmitterOptions {
EmitterOptions::new()
.with_flow(self.flow)
.with_explicit_start(self.explicit_start)
.with_explicit_end(self.explicit_end)
.with_scalar_style(self.scalar_style)
.with_indent(self.indent)
.with_indent_sequences(self.indent_sequences)
}
}
#[cfg(test)]
mod tests {
use super::{EmitOptions, ParseOptions};
use crate::emitter::{EmitterOptions, EmitterStyles, NodeStyle, PathSegment};
use crate::event::ScalarStyle;
use crate::limits::Limits;
const PARSE_DEFAULT: ParseOptions = ParseOptions::new();
const PARSE_MAX_INPUT: ParseOptions = ParseOptions::for_max_input();
const PARSE_CUSTOM: ParseOptions =
ParseOptions::new().with_limits(Limits::new().with_max_depth(16));
const EMIT_DEFAULT: EmitOptions = EmitOptions::new();
const EMIT_FLOW: EmitOptions = EmitOptions::flow();
const EMIT_CUSTOM: EmitOptions = EmitOptions::new()
.with_limits(Limits::new().with_max_output_bytes(1024))
.with_flow(true)
.with_explicit_start(true)
.with_explicit_end(true)
.with_scalar_style(Some(ScalarStyle::DoubleQuoted))
.with_indent(4)
.with_indent_sequences(true);
#[test]
fn const_presets_and_defaults_match() {
let flow = EMIT_FLOW;
let custom = EMIT_CUSTOM;
assert_eq!(PARSE_DEFAULT, ParseOptions::default());
assert_eq!(PARSE_MAX_INPUT.limits, Limits::for_max_input());
assert_eq!(PARSE_CUSTOM.limits.max_depth, 16);
assert_eq!(
ParseOptions::from(Limits::new().with_max_depth(8))
.limits
.max_depth,
8
);
assert_eq!(EMIT_DEFAULT, EmitOptions::default());
assert!(flow.flow);
assert_eq!(flow.indent, 2);
assert_eq!(custom.limits.max_output_bytes, 1024);
assert!(custom.flow);
assert!(custom.explicit_start);
assert!(custom.explicit_end);
assert_eq!(custom.scalar_style, Some(ScalarStyle::DoubleQuoted));
assert_eq!(custom.indent, 4);
assert!(custom.indent_sequences);
assert!(custom.styles.is_none());
}
#[test]
fn emit_options_conversions_and_styles() {
let emitter_opts = EmitterOptions::new()
.with_flow(true)
.with_explicit_start(true)
.with_explicit_end(true)
.with_scalar_style(Some(ScalarStyle::SingleQuoted))
.with_indent(3)
.with_indent_sequences(true);
let from_emitter = EmitOptions::from(emitter_opts);
assert_eq!(from_emitter.emitter_options(), emitter_opts);
assert_eq!(from_emitter.limits, Limits::default());
let mut styles = EmitterStyles::new();
styles.set(
vec![PathSegment::key("a")],
NodeStyle::new().with_flow(Some(true)),
);
let from_styles = EmitOptions::from(styles.clone());
assert_eq!(from_styles.styles, Some(styles));
assert_eq!(
EmitOptions::from(Limits::new().with_max_output_bytes(42))
.limits
.max_output_bytes,
42
);
}
}