error span/source getters

This commit is contained in:
Robin Appelman 2023-12-18 17:20:22 +01:00
commit 47e94e6703

View file

@ -42,8 +42,9 @@ pub enum VdfError {
#[diagnostic(transparent)] #[diagnostic(transparent)]
/// Failed to parse serde string /// Failed to parse serde string
SerdeParse(#[from] SerdeParseError), SerdeParse(#[from] SerdeParseError),
#[error("{0}")] #[error(transparent)]
Other(String), #[diagnostic(transparent)]
Other(#[from] UnknownError),
} }
impl From<WrongEventTypeError> for VdfError { impl From<WrongEventTypeError> for VdfError {
@ -53,12 +54,58 @@ impl From<WrongEventTypeError> for VdfError {
} }
impl VdfError { impl VdfError {
pub fn source(&self) -> Option<&str> {
let src = match self {
VdfError::Other(e) => e.src.as_str(),
VdfError::UnexpectedToken(e) => e.src.as_str(),
VdfError::NoValidToken(e) => e.src.as_str(),
VdfError::WrongEntryType(e) => e.src.as_str(),
VdfError::SerdeParse(e) => e.src.as_str(),
VdfError::UnknownVariant(e) => e.src.as_str(),
_ => {
return None;
}
};
(!src.is_empty()).then_some(src)
}
pub fn span(&self) -> Option<SourceSpan> {
let span = match self {
VdfError::Other(e) => e.err_span,
VdfError::UnexpectedToken(e) => e.err_span,
VdfError::NoValidToken(e) => e.err_span,
VdfError::WrongEntryType(e) => e.err_span,
VdfError::SerdeParse(e) => e.err_span,
VdfError::UnknownVariant(e) => e.err_span,
_ => {
return None;
}
};
(!span.is_empty()).then_some(span)
}
pub(crate) fn with_source_span_if_none<Sp: Into<SourceSpan>, Sr: Into<String>>(
self,
span: Sp,
source: Sr,
) -> VdfError {
if self.source().is_none() {
self.with_source_span(span, source)
} else {
self
}
}
pub(crate) fn with_source_span<Sp: Into<SourceSpan>, Sr: Into<String>>( pub(crate) fn with_source_span<Sp: Into<SourceSpan>, Sr: Into<String>>(
self, self,
span: Sp, span: Sp,
source: Sr, source: Sr,
) -> VdfError { ) -> VdfError {
match self { match self {
VdfError::Other(e) => UnknownError {
src: source.into(),
err_span: span.into(),
..e
}
.into(),
VdfError::UnexpectedToken(e) => UnexpectedTokenError { VdfError::UnexpectedToken(e) => UnexpectedTokenError {
src: source.into(), src: source.into(),
err_span: span.into(), err_span: span.into(),
@ -113,6 +160,17 @@ impl<T: Display> Display for CommaSeperated<'_, T> {
} }
} }
#[derive(Debug, Clone, Diagnostic, Error)]
#[diagnostic(code(vmt_reader::unexpected_token))]
#[error("{error}")]
pub struct UnknownError {
pub error: String,
#[label("{error}")]
err_span: SourceSpan,
#[source_code]
src: String,
}
/// A token that wasn't expected was found while parsing /// A token that wasn't expected was found while parsing
#[derive(Debug, Clone, Diagnostic)] #[derive(Debug, Clone, Diagnostic)]
#[diagnostic(code(vmt_reader::unexpected_token))] #[diagnostic(code(vmt_reader::unexpected_token))]
@ -391,7 +449,11 @@ impl serde::de::Error for VdfError {
where where
T: Display, T: Display,
{ {
VdfError::Other(msg.to_string()) VdfError::Other(UnknownError {
err_span: (0..0).into(),
src: String::new(),
error: msg.to_string(),
})
} }
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self { fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {