use super::types::{CaptureError, EmissionError, Emittable};
use crate::closure::PartitionCargo;
use crate::expansion::Expansion;
use crate::kind::Kind;
use crate::token::CaptureBound;
use core::fmt;
impl CaptureError {
#[must_use]
pub const fn slot(&self) -> u8 {
match self {
Self::Unbounded { .. } => 0,
Self::Unread { .. } => 1,
}
}
}
impl From<CaptureBound> for CaptureError {
fn from(bound: CaptureBound) -> Self {
Self::Unbounded { bound }
}
}
impl fmt::Display for CaptureError {
fn fmt(&self, into: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unbounded { bound } => write!(into, "{bound}"),
Self::Unread { cause, .. } => write!(into, "{cause}"),
}
}
}
impl core::error::Error for CaptureError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Unbounded { bound } => Some(bound),
Self::Unread { cause, .. } => Some(cause),
}
}
}
impl fmt::Display for EmissionError {
fn fmt(&self, into: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NumberRejected { spelling } => write!(
into,
"the compiler host rejected the admitted numeric literal `{spelling}`"
),
Self::NulTerminatedTextRejected => into
.write_str("the compiler host rejected admitted NUL-terminated literal material"),
Self::SourceSpanRosterContradiction => into.write_str(
"the generated tree's preserved source roster does not match its token denominator",
),
Self::SourceSpanUnresolved(refusal) => write!(
into,
"the compiler host could not resolve one preserved source span: {refusal}"
),
}
}
}
impl core::error::Error for EmissionError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::SourceSpanUnresolved(refusal) => Some(refusal),
Self::NumberRejected { .. }
| Self::NulTerminatedTextRejected
| Self::SourceSpanRosterContradiction => None,
}
}
}
impl<K: Kind> Emittable for Expansion<K> {
fn cargos(&self) -> impl Iterator<Item = &PartitionCargo> {
core::iter::once(self.emit())
}
}