Function macro_tools::has_debug

source ·
pub fn has_debug<'a>(attrs: impl Iterator<Item = &'a Attribute>) -> Result<bool>
Expand description

Checks if the given iterator of attributes contains an attribute named debug.

This function iterates over an input sequence of syn::Attribute, typically associated with a struct, enum, or other item in a Rust Abstract Syntax Tree ( AST ), and determines whether any of the attributes is exactly named debug.

§Parameters

  • attrs : An iterator over syn::Attribute. This could be obtained from parsing Rust code with the syn crate, where the iterator represents attributes applied to a Rust item ( like a struct or function ).

§Returns

  • Ok( true ) if the debug attribute is present.
  • Ok( false ) if the debug attribute is not found.
  • Err( syn::Error ) if an unknown or improperly formatted attribute is encountered.

§Example

Suppose you have the following struct definition in a procedural macro input:

#[ derive( SomeDerive ) ]
#[ debug ]
struct MyStruct
{
  field : i32,
}

You can use has_debug to check for the presence of the debug attribute:

use macro_tools::exposed::*;

// Example struct attribute
let attrs : Vec< syn::Attribute > = vec![ syn::parse_quote!( #[ debug ] ) ];

// Checking for 'debug' attribute
let contains_debug = attr::has_debug( ( &attrs ).into_iter() ).unwrap();

assert!( contains_debug, "Expected to find 'debug' attribute" );