-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(diagnostic_source): add protocol method for Diagnostic-aware sou…
…rce chaining (#165) Signed-off-by: Matthias Beyer <[email protected]>
- Loading branch information
1 parent
33c8903
commit bc449c8
Showing
11 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::spanned::Spanned; | ||
|
||
use crate::forward::WhichFn; | ||
use crate::{ | ||
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef}, | ||
utils::{display_pat_members, gen_all_variants_with}, | ||
}; | ||
|
||
pub struct DiagnosticSource(syn::Member); | ||
|
||
impl DiagnosticSource { | ||
pub(crate) fn from_fields(fields: &syn::Fields) -> syn::Result<Option<Self>> { | ||
match fields { | ||
syn::Fields::Named(named) => Self::from_fields_vec(named.named.iter().collect()), | ||
syn::Fields::Unnamed(unnamed) => { | ||
Self::from_fields_vec(unnamed.unnamed.iter().collect()) | ||
} | ||
syn::Fields::Unit => Ok(None), | ||
} | ||
} | ||
|
||
fn from_fields_vec(fields: Vec<&syn::Field>) -> syn::Result<Option<Self>> { | ||
for (i, field) in fields.iter().enumerate() { | ||
for attr in &field.attrs { | ||
if attr.path.is_ident("diagnostic_source") { | ||
let diagnostic_source = if let Some(ident) = field.ident.clone() { | ||
syn::Member::Named(ident) | ||
} else { | ||
syn::Member::Unnamed(syn::Index { | ||
index: i as u32, | ||
span: field.span(), | ||
}) | ||
}; | ||
return Ok(Some(DiagnosticSource(diagnostic_source))); | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
pub(crate) fn gen_enum(variants: &[DiagnosticDef]) -> Option<TokenStream> { | ||
gen_all_variants_with( | ||
variants, | ||
WhichFn::DiagnosticSource, | ||
|ident, | ||
fields, | ||
DiagnosticConcreteArgs { | ||
diagnostic_source, .. | ||
}| { | ||
let (display_pat, _display_members) = display_pat_members(fields); | ||
diagnostic_source.as_ref().map(|diagnostic_source| { | ||
let rel = match &diagnostic_source.0 { | ||
syn::Member::Named(ident) => ident.clone(), | ||
syn::Member::Unnamed(syn::Index { index, .. }) => { | ||
quote::format_ident!("_{}", index) | ||
} | ||
}; | ||
quote! { | ||
Self::#ident #display_pat => { | ||
std::option::Option::Some(#rel.as_ref()) | ||
} | ||
} | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn gen_struct(&self) -> Option<TokenStream> { | ||
let rel = &self.0; | ||
Some(quote! { | ||
fn diagnostic_source<'a>(&'a self) -> std::option::Option<&'a dyn miette::Diagnostic> { | ||
std::option::Option::Some(&self.#rel) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*! | ||
Iterate over error `.diagnostic_source()` chains. | ||
*/ | ||
|
||
use crate::protocol::Diagnostic; | ||
|
||
/// Iterator of a chain of cause errors. | ||
#[derive(Clone, Default)] | ||
#[allow(missing_debug_implementations)] | ||
pub(crate) struct DiagnosticChain<'a> { | ||
state: Option<ErrorKind<'a>>, | ||
} | ||
|
||
impl<'a> DiagnosticChain<'a> { | ||
pub(crate) fn from_diagnostic(head: &'a dyn Diagnostic) -> Self { | ||
DiagnosticChain { | ||
state: Some(ErrorKind::Diagnostic(head)), | ||
} | ||
} | ||
|
||
pub(crate) fn from_stderror(head: &'a (dyn std::error::Error + 'static)) -> Self { | ||
DiagnosticChain { | ||
state: Some(ErrorKind::StdError(head)), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Iterator for DiagnosticChain<'a> { | ||
type Item = ErrorKind<'a>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if let Some(err) = self.state.take() { | ||
self.state = err.get_nested(); | ||
Some(err) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let len = self.len(); | ||
(len, Some(len)) | ||
} | ||
} | ||
|
||
impl ExactSizeIterator for DiagnosticChain<'_> { | ||
fn len(&self) -> usize { | ||
fn depth(d: Option<&ErrorKind<'_>>) -> usize { | ||
match d { | ||
Some(d) => 1 + depth(d.get_nested().as_ref()), | ||
None => 0, | ||
} | ||
} | ||
|
||
depth(self.state.as_ref()) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) enum ErrorKind<'a> { | ||
Diagnostic(&'a dyn Diagnostic), | ||
StdError(&'a (dyn std::error::Error + 'static)), | ||
} | ||
|
||
impl<'a> ErrorKind<'a> { | ||
fn get_nested(&self) -> Option<ErrorKind<'a>> { | ||
match self { | ||
ErrorKind::Diagnostic(d) => d | ||
.diagnostic_source() | ||
.map(ErrorKind::Diagnostic) | ||
.or_else(|| d.source().map(ErrorKind::StdError)), | ||
ErrorKind::StdError(e) => e.source().map(ErrorKind::StdError), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> std::fmt::Debug for ErrorKind<'a> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
ErrorKind::Diagnostic(d) => d.fmt(f), | ||
ErrorKind::StdError(e) => e.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> std::fmt::Display for ErrorKind<'a> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
ErrorKind::Diagnostic(d) => d.fmt(f), | ||
ErrorKind::StdError(e) => e.fmt(f), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.