use std::fmt;
use qubit_budget::MeasuredBudgetError;
use qubit_budget::ResourceQuantity;
use crate::encode::JsonCollectionKind;
use crate::encode::JsonIntegerSignedness;
use crate::encode::JsonMapKeyKind;
use crate::encode::JsonSerializationError;
use crate::encode::JsonSerializationErrorCategory;
use crate::encode::JsonSerializationErrorKind;
use crate::encode::JsonSerializerStateError;
#[must_use]
#[non_exhaustive]
#[derive(Debug)]
pub enum JsonValueEncodeError<R, Q>
where
Q: ResourceQuantity,
{
Budget(
MeasuredBudgetError<R, Q>,
),
Serialization(
JsonSerializationError,
),
PrivateTextLimitExceeded {
maximum: usize,
},
}
impl<R, Q> JsonValueEncodeError<R, Q>
where
Q: ResourceQuantity,
{
#[inline]
pub const fn serialization_error(&self) -> Option<&JsonSerializationError> {
match self {
Self::Serialization(error) => Some(error),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[inline]
pub const fn kind(&self) -> Option<JsonSerializationErrorKind> {
match self {
Self::Serialization(error) => Some(error.kind()),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[inline]
pub const fn category(&self) -> Option<JsonSerializationErrorCategory> {
match self {
Self::Serialization(error) => Some(error.category()),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[must_use]
#[inline]
pub const fn is_number_error(&self) -> bool {
matches!(self, Self::Serialization(error) if error.is_number_error())
}
#[must_use]
#[inline]
pub const fn is_map_key_error(&self) -> bool {
matches!(self, Self::Serialization(error) if error.is_map_key_error())
}
#[must_use]
#[inline]
pub const fn is_raw_value_error(&self) -> bool {
matches!(self, Self::Serialization(error) if error.is_raw_value_error())
}
#[must_use]
#[inline]
pub const fn is_serializer_contract_error(&self) -> bool {
matches!(self, Self::Serialization(error) if error.is_serializer_contract_error())
}
#[inline]
pub const fn integer_signedness(&self) -> Option<JsonIntegerSignedness> {
match self {
Self::Serialization(error) => error.integer_signedness(),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[inline]
pub const fn map_key_kind(&self) -> Option<JsonMapKeyKind> {
match self {
Self::Serialization(error) => error.map_key_kind(),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[inline]
pub const fn collection_kind(&self) -> Option<JsonCollectionKind> {
match self {
Self::Serialization(error) => error.collection_kind(),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
#[inline]
pub const fn serializer_state_error(&self) -> Option<JsonSerializerStateError> {
match self {
Self::Serialization(error) => error.serializer_state_error(),
Self::Budget(_) => None,
Self::PrivateTextLimitExceeded { .. } => None,
}
}
}
impl<R, Q> fmt::Display for JsonValueEncodeError<R, Q>
where
Q: ResourceQuantity,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Budget(_) => f.write_str("JSON value budget rejected serialization"),
Self::Serialization(error) => write!(f, "{error}"),
Self::PrivateTextLimitExceeded { maximum } => {
write!(f, "private JSON text exceeded the {maximum}-byte limit")
}
}
}
}
impl<R, Q> std::error::Error for JsonValueEncodeError<R, Q>
where
R: fmt::Debug + 'static,
Q: ResourceQuantity + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Budget(error) => Some(error),
Self::Serialization(error) => Some(error),
Self::PrivateTextLimitExceeded { .. } => None,
}
}
}