use std::cell::RefCell;
use std::fmt;
use qubit_budget::ResourceQuantity;
use super::json_encode_context::JsonEncodeContext;
pub(super) struct BudgetedDisplayCollector<'context, 'transaction, 'budget, R, Q>
where
Q: ResourceQuantity,
{
pub(super) text: String,
pub(super) context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
}
impl<'context, 'transaction, 'budget, R, Q> BudgetedDisplayCollector<'context, 'transaction, 'budget, R, Q>
where
Q: ResourceQuantity,
{
#[inline]
pub(super) fn new(context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>) -> Self {
Self {
text: String::new(),
context,
}
}
}
impl<R, Q> fmt::Write for BudgetedDisplayCollector<'_, '_, '_, R, Q>
where
R: Clone,
Q: ResourceQuantity,
{
fn write_str(&mut self, value: &str) -> fmt::Result {
let next = self.text.len().checked_add(value.len()).ok_or(fmt::Error)?;
let output_result = self.context.borrow().output.borrow().check_available(next);
self.context.borrow_mut().record::<fmt::Error>(output_result)?;
self.text.push_str(value);
Ok(())
}
}