A better Rust ATProto crate
at main 34 lines 1.2 kB view raw
1//! Shared helper functions for derive macros 2 3use syn::{Attribute, Ident}; 4 5/// Helper function to check if a struct derives bon::Builder or Builder 6pub fn has_derive_builder(attrs: &[Attribute]) -> bool { 7 attrs.iter().any(|attr| { 8 if !attr.path().is_ident("derive") { 9 return false; 10 } 11 12 // Parse the derive attribute to check its contents 13 if let Ok(list) = attr.parse_args_with( 14 syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated, 15 ) { 16 list.iter().any(|path| { 17 // Check for "Builder" or "bon::Builder" 18 path.segments 19 .last() 20 .map(|seg| seg.ident == "Builder") 21 .unwrap_or(false) 22 }) 23 } else { 24 false 25 } 26 }) 27} 28 29/// Check if struct name conflicts with types referenced by bon::Builder macro. 30/// bon::Builder generates code that uses unqualified `Option` and `Result`, 31/// so structs with these names cause compilation errors. 32pub fn conflicts_with_builder_macro(ident: &Ident) -> bool { 33 matches!(ident.to_string().as_str(), "Option" | "Result") 34}