use std::cell::RefCell;
use qubit_budget::ResourceQuantity;
use serde::Serialize;
use serde::Serializer;
use super::JsonEncodeContext;
use super::JsonPrivateTextSerializer;
use super::PrivateTextKind;
pub(in crate::encode) struct BudgetedPrivateValue<'a, 'transaction, 'budget, 'context, T, R, Q>
where
T: ?Sized,
Q: ResourceQuantity,
{
value: &'a T,
context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
kind: PrivateTextKind,
}
impl<'a, 'transaction, 'budget, 'context, T, R, Q>
BudgetedPrivateValue<'a, 'transaction, 'budget, 'context, T, R, Q>
where
T: ?Sized,
Q: ResourceQuantity,
{
#[must_use]
#[inline]
pub(super) const fn raw_value(
value: &'a T,
context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
depth: usize,
) -> Self {
Self {
value,
context,
kind: PrivateTextKind::RawValue { depth },
}
}
#[must_use]
#[inline]
pub(super) const fn number(
value: &'a T,
context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
depth: usize,
) -> Self {
Self {
value,
context,
kind: PrivateTextKind::Number { depth },
}
}
}
impl<T, R, Q> Serialize for BudgetedPrivateValue<'_, '_, '_, '_, T, R, Q>
where
T: Serialize + ?Sized,
R: Clone,
Q: ResourceQuantity,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let result = self.value.serialize(JsonPrivateTextSerializer {
inner: serializer,
context: self.context,
kind: self.kind,
});
if result.is_err() {
self.context
.borrow()
.output
.borrow_mut()
.record_custom_serialization_error();
}
result
}
}