use crate::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum YamlVersion {
#[default]
V1_2,
V1_1,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ParserConfig {
pub yaml_version: YamlVersion,
pub max_depth: usize,
pub max_document_length: usize,
pub max_alias_expansions: usize,
pub max_mapping_keys: usize,
pub max_sequence_length: usize,
pub max_events: usize,
pub max_nodes: usize,
pub max_total_scalar_bytes: usize,
pub max_documents: usize,
pub max_merge_keys: usize,
pub alias_anchor_ratio: Option<f64>,
pub duplicate_key_policy: DuplicateKeyPolicy,
pub strict_booleans: bool,
pub legacy_booleans: bool,
pub tag_registry: Option<Arc<crate::TagRegistry>>,
pub merge_key_policy: MergeKeyPolicy,
pub no_schema: bool,
pub legacy_octal_numbers: bool,
pub ignore_binary_tag_for_string: bool,
pub legacy_sexagesimal: bool,
pub require_indent: RequireIndent,
pub policies: Vec<Arc<dyn crate::policy::Policy>>,
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub properties: Option<Arc<std::collections::HashMap<String, String>>>,
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub strict_properties: bool,
#[cfg(feature = "include")]
#[cfg_attr(docsrs, doc(cfg(feature = "include")))]
pub include_resolver: Option<crate::include::IncludeResolver>,
#[cfg(feature = "include")]
#[cfg_attr(docsrs, doc(cfg(feature = "include")))]
pub max_include_depth: usize,
}
impl Default for ParserConfig {
fn default() -> Self {
ParserConfig {
yaml_version: YamlVersion::V1_2,
max_depth: 128,
max_document_length: 1024 * 1024 * 64, max_alias_expansions: 1024,
max_mapping_keys: 1024 * 64,
max_sequence_length: 1024 * 64,
max_events: 1_000_000,
max_nodes: 250_000,
max_total_scalar_bytes: 1024 * 1024 * 64, max_documents: 1_000,
max_merge_keys: 10_000,
alias_anchor_ratio: Some(10.0),
duplicate_key_policy: DuplicateKeyPolicy::default(),
strict_booleans: false,
legacy_booleans: false,
tag_registry: None,
merge_key_policy: MergeKeyPolicy::default(),
no_schema: false,
legacy_octal_numbers: false,
ignore_binary_tag_for_string: false,
legacy_sexagesimal: false,
require_indent: RequireIndent::Unchecked,
policies: Vec::new(),
#[cfg(feature = "std")]
properties: None,
#[cfg(feature = "std")]
strict_properties: false,
#[cfg(feature = "include")]
include_resolver: None,
#[cfg(feature = "include")]
max_include_depth: 24,
}
}
}
impl ParserConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn strict() -> Self {
ParserConfig {
yaml_version: YamlVersion::V1_2,
max_depth: 64,
max_document_length: 1024 * 1024, max_alias_expansions: 100,
max_mapping_keys: 1024,
max_sequence_length: 1024,
max_events: 100_000,
max_nodes: 25_000,
max_total_scalar_bytes: 1024 * 1024, max_documents: 100,
max_merge_keys: 1_000,
alias_anchor_ratio: Some(5.0),
strict_booleans: true,
legacy_booleans: false,
duplicate_key_policy: DuplicateKeyPolicy::Error,
tag_registry: None,
merge_key_policy: MergeKeyPolicy::default(),
no_schema: false,
legacy_octal_numbers: false,
ignore_binary_tag_for_string: false,
legacy_sexagesimal: false,
require_indent: RequireIndent::Even,
policies: Vec::new(),
#[cfg(feature = "std")]
properties: None,
#[cfg(feature = "std")]
strict_properties: true,
#[cfg(feature = "include")]
include_resolver: None,
#[cfg(feature = "include")]
max_include_depth: 8,
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
pub fn properties(
mut self,
properties: Arc<std::collections::HashMap<String, String>>,
) -> Self {
self.properties = Some(properties);
self
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[must_use]
pub fn strict_properties(mut self, strict: bool) -> Self {
self.strict_properties = strict;
self
}
#[cfg(feature = "include")]
#[cfg_attr(docsrs, doc(cfg(feature = "include")))]
#[must_use]
pub fn include_resolver(mut self, resolver: crate::include::IncludeResolver) -> Self {
self.include_resolver = Some(resolver);
self
}
#[cfg(feature = "include")]
#[cfg_attr(docsrs, doc(cfg(feature = "include")))]
#[must_use]
pub fn max_include_depth(mut self, depth: usize) -> Self {
self.max_include_depth = depth;
self
}
#[must_use]
pub fn version(mut self, version: YamlVersion) -> Self {
self.yaml_version = version;
match version {
YamlVersion::V1_1 => {
self.legacy_booleans = true;
self.legacy_octal_numbers = true;
self.legacy_sexagesimal = true;
}
YamlVersion::V1_2 => {
self.legacy_booleans = false;
self.legacy_octal_numbers = false;
self.legacy_sexagesimal = false;
}
}
self
}
#[must_use]
pub fn with_policy<P>(mut self, policy: P) -> Self
where
P: crate::policy::Policy + 'static,
{
self.policies.push(Arc::new(policy));
self
}
#[must_use]
pub fn max_depth(mut self, depth: usize) -> Self {
self.max_depth = depth;
self
}
#[must_use]
pub fn max_document_length(mut self, len: usize) -> Self {
self.max_document_length = len;
self
}
#[must_use]
pub fn max_alias_expansions(mut self, expansions: usize) -> Self {
self.max_alias_expansions = expansions;
self
}
#[must_use]
pub fn max_mapping_keys(mut self, max: usize) -> Self {
self.max_mapping_keys = max;
self
}
#[must_use]
pub fn max_sequence_length(mut self, max: usize) -> Self {
self.max_sequence_length = max;
self
}
#[must_use]
pub fn max_events(mut self, max: usize) -> Self {
self.max_events = max;
self
}
#[must_use]
pub fn max_nodes(mut self, max: usize) -> Self {
self.max_nodes = max;
self
}
#[must_use]
pub fn max_total_scalar_bytes(mut self, max: usize) -> Self {
self.max_total_scalar_bytes = max;
self
}
#[must_use]
pub fn max_documents(mut self, max: usize) -> Self {
self.max_documents = max;
self
}
#[must_use]
pub fn max_merge_keys(mut self, max: usize) -> Self {
self.max_merge_keys = max;
self
}
#[must_use]
pub fn require_indent(mut self, mode: RequireIndent) -> Self {
self.require_indent = mode;
self
}
#[must_use]
pub fn alias_anchor_ratio(mut self, ratio: Option<f64>) -> Self {
self.alias_anchor_ratio = ratio;
self
}
#[must_use]
pub fn duplicate_key_policy(mut self, policy: DuplicateKeyPolicy) -> Self {
self.duplicate_key_policy = policy;
self
}
#[must_use]
pub fn strict_booleans(mut self, strict: bool) -> Self {
self.strict_booleans = strict;
self
}
#[must_use]
pub fn legacy_booleans(mut self, legacy: bool) -> Self {
self.legacy_booleans = legacy;
self
}
#[must_use]
pub fn tag_registry(mut self, registry: Arc<crate::TagRegistry>) -> Self {
self.tag_registry = Some(registry);
self
}
#[must_use]
pub fn merge_key_policy(mut self, policy: MergeKeyPolicy) -> Self {
self.merge_key_policy = policy;
self
}
#[must_use]
pub fn no_schema(mut self, no_schema: bool) -> Self {
self.no_schema = no_schema;
self
}
#[must_use]
pub fn legacy_octal_numbers(mut self, on: bool) -> Self {
self.legacy_octal_numbers = on;
self
}
#[must_use]
pub fn ignore_binary_tag_for_string(mut self, on: bool) -> Self {
self.ignore_binary_tag_for_string = on;
self
}
#[must_use]
pub fn legacy_sexagesimal(mut self, on: bool) -> Self {
self.legacy_sexagesimal = on;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum RequireIndent {
#[default]
Unchecked,
Even,
Divisible(usize),
Uniform(Option<usize>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum MergeKeyPolicy {
#[default]
Auto,
AsOrdinary,
Error,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum DuplicateKeyPolicy {
First,
#[default]
Last,
Error,
}