use std::error::Error;
use std::fmt;
use qubit_budget::ResourceQuantity;
use qubit_budget::json::JsonResource;
use super::JsonDecodeError;
#[must_use]
#[derive(Debug, Clone)]
pub enum JcsDecodeError<R = JsonResource, Q = usize>
where
Q: Copy + fmt::Debug,
{
Decode(
JsonDecodeError<R, Q>,
),
DuplicateKey {
line: usize,
column: usize,
},
MaterializationDepthLimit {
limit: usize,
line: usize,
column: usize,
},
}
impl<R, Q> fmt::Display for JcsDecodeError<R, Q>
where
R: fmt::Debug,
Q: ResourceQuantity,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decode(error) => error.fmt(formatter),
Self::DuplicateKey { line, column } => {
write!(formatter, "duplicate JSON object key at line {line}, column {column}")
}
Self::MaterializationDepthLimit { limit, line, column } => write!(
formatter,
"JCS value construction depth exceeds {limit} at line {line}, column {column}"
),
}
}
}
impl<R, Q> Error for JcsDecodeError<R, Q>
where
R: fmt::Debug + 'static,
Q: ResourceQuantity + 'static,
{
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Decode(error) => Some(error),
Self::DuplicateKey { .. } | Self::MaterializationDepthLimit { .. } => None,
}
}
}
impl<R, Q> From<JsonDecodeError<R, Q>> for JcsDecodeError<R, Q>
where
Q: Copy + fmt::Debug,
{
#[inline]
fn from(error: JsonDecodeError<R, Q>) -> Self {
Self::Decode(error)
}
}