From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/miette-derive/src/related.rs | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 vendor/miette-derive/src/related.rs (limited to 'vendor/miette-derive/src/related.rs') diff --git a/vendor/miette-derive/src/related.rs b/vendor/miette-derive/src/related.rs new file mode 100644 index 0000000..9b7f9e1 --- /dev/null +++ b/vendor/miette-derive/src/related.rs @@ -0,0 +1,79 @@ +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::spanned::Spanned; + +use crate::{ + diagnostic::{DiagnosticConcreteArgs, DiagnosticDef}, + forward::WhichFn, + utils::{display_pat_members, gen_all_variants_with}, +}; + +pub struct Related(syn::Member); + +impl Related { + pub(crate) fn from_fields(fields: &syn::Fields) -> syn::Result> { + 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> { + for (i, field) in fields.iter().enumerate() { + for attr in &field.attrs { + if attr.path().is_ident("related") { + let related = 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(Related(related))); + } + } + } + Ok(None) + } + + pub(crate) fn gen_enum(variants: &[DiagnosticDef]) -> Option { + gen_all_variants_with( + variants, + WhichFn::Related, + |ident, fields, DiagnosticConcreteArgs { related, .. }| { + let (display_pat, _display_members) = display_pat_members(fields); + related.as_ref().map(|related| { + let rel = match &related.0 { + syn::Member::Named(ident) => ident.clone(), + syn::Member::Unnamed(syn::Index { index, .. }) => { + format_ident!("_{}", index) + } + }; + quote! { + Self::#ident #display_pat => { + std::option::Option::Some(std::boxed::Box::new( + #rel.iter().map(|x| -> &(dyn miette::Diagnostic) { &*x }) + )) + } + } + }) + }, + ) + } + + pub(crate) fn gen_struct(&self) -> Option { + let rel = &self.0; + Some(quote! { + fn related<'a>(&'a self) -> std::option::Option + 'a>> { + use ::core::borrow::Borrow; + std::option::Option::Some(std::boxed::Box::new( + self.#rel.iter().map(|x| -> &(dyn miette::Diagnostic) { &*x.borrow() }) + )) + } + }) + } +} -- cgit v1.2.3