use crate::resource::ResourceQuantity;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct JsonValueState<Q>
where
Q: ResourceQuantity,
{
remaining_nodes: Option<Q>,
remaining_payload_bytes: Option<Q>,
}
impl<Q> JsonValueState<Q>
where
Q: ResourceQuantity,
{
#[inline(always)]
#[must_use]
pub(crate) const fn new(remaining_nodes: Option<Q>, remaining_payload_bytes: Option<Q>) -> Self {
Self {
remaining_nodes,
remaining_payload_bytes,
}
}
#[must_use]
#[inline(always)]
pub(crate) const fn remaining_nodes(&self) -> Option<Q> {
self.remaining_nodes
}
#[must_use]
#[inline(always)]
pub(crate) const fn remaining_payload_bytes(&self) -> Option<Q> {
self.remaining_payload_bytes
}
pub(crate) fn apply(&mut self, node: bool, payload_bytes: Q) {
if node && let Some(remaining) = &mut self.remaining_nodes {
*remaining = *remaining - Q::ONE;
}
if let Some(remaining) = &mut self.remaining_payload_bytes {
*remaining = *remaining - payload_bytes;
}
}
}