use super::{
CoordinateRole, SourceCoordinate, SpanHandle, SpanResolutionRefusal, SpanTable, TextReadRefusal,
};
impl SpanTable {
pub fn coordinate_of(
&self,
span: SpanHandle,
) -> Result<SourceCoordinate, SpanResolutionRefusal> {
match self {
Self::ByteOffsets(offsets) => {
let unreached = SpanResolutionRefusal {
handle: span,
reaches: offsets.len(),
};
let index = usize::try_from(span.index()).map_err(|_| unreached)?;
offsets
.as_slice()
.get(index)
.map(|offset| SourceCoordinate {
role: CoordinateRole::Byte,
position: *offset,
})
.ok_or(unreached)
}
Self::ProducerHeld => Ok(SourceCoordinate {
role: CoordinateRole::SemanticOrigin,
position: u64::from(span.index()),
}),
}
}
}
impl TextReadRefusal {
#[must_use]
pub const fn coordinate(self) -> SourceCoordinate {
SourceCoordinate {
role: CoordinateRole::Byte,
position: self.at,
}
}
}