use super::{VALUE_WORK, WriteError, Writer};
use crate::error::{LimitError, LimitKind};
impl Writer<'_, '_> {
pub(super) fn count(&mut self, work: u64) -> Result<(), WriteError> {
if self.repeated > 0 {
self.bound()?;
}
self.open.add(work).map_err(WriteError::Limit)
}
pub(super) fn skip(&mut self) -> Result<(), WriteError> {
self.bound()?;
self.open.add(VALUE_WORK).map_err(WriteError::Limit)
}
pub(super) fn bound(&mut self) -> Result<(), WriteError> {
self.values = self.values.saturating_add(1);
let max = self.limits.max_values_per_object;
if self.values > max {
return Err(WriteError::Limit(LimitError::refused(
LimitKind::ValuesPerObject,
self.values,
max,
)));
}
Ok(())
}
#[inline]
pub(super) fn child(
&mut self,
repeated: bool,
write: impl FnOnce(&mut Self) -> Result<(), WriteError>,
) -> Result<(), WriteError> {
if !repeated {
return write(self);
}
self.repeated = self.repeated.saturating_add(1);
let r = write(self);
self.repeated = self.repeated.saturating_sub(1);
r
}
pub(super) fn frame(
&mut self,
f: impl FnOnce(&mut Self) -> Result<(), WriteError>,
) -> Result<(), WriteError> {
self.depth = self.depth.saturating_add(1);
if self.depth > self.max_depth {
let e = WriteError::Limit(LimitError::refused(
LimitKind::ValueDepth,
u64::from(self.depth),
u64::from(self.max_depth),
));
self.depth = self.depth.saturating_sub(1);
return Err(e);
}
let r = f(self);
self.depth = self.depth.saturating_sub(1);
r
}
}