use super::options::{DuplicateKeyPolicy, MergeKeyPolicy, Options};
use super::{Error, Location};
use crate::budget::BudgetBreach;
#[derive(Copy, Clone)]
pub(crate) struct Cfg {
pub(crate) dup_policy: DuplicateKeyPolicy,
pub(crate) merge_keys: MergeKeyPolicy,
pub(crate) legacy_octal_numbers: bool,
pub(crate) strict_booleans: bool,
pub(crate) angle_conversions: bool,
pub(crate) ignore_binary_tag_for_string: bool,
pub(crate) no_schema: bool,
pub(crate) max_depth: Option<usize>,
pub(crate) depth: usize,
}
impl Cfg {
#[inline]
#[allow(deprecated)]
pub(crate) fn from_options(options: &Options) -> Self {
Self {
dup_policy: options.duplicate_keys,
merge_keys: options.merge_keys,
legacy_octal_numbers: options.legacy_octal_numbers,
strict_booleans: options.strict_booleans,
angle_conversions: options.angle_conversions,
ignore_binary_tag_for_string: options.ignore_binary_tag_for_string,
no_schema: options.no_schema,
max_depth: options.budget.as_ref().map(|budget| budget.max_depth),
depth: 0,
}
}
pub(crate) fn enter_container(self, location: Location) -> Result<Self, Error> {
let depth = self.depth.saturating_add(1);
if let Some(max_depth) = self.max_depth
&& depth > max_depth
{
return Err(crate::de_error::budget_error(BudgetBreach::Depth { depth })
.with_location(location));
}
Ok(Self { depth, ..self })
}
}