usage/
usage.rs

1use syn::{Field, parse_quote};
2use syn_cfg_attr::{AttributeHelpers, ExpandedAttr};
3
4// Mock structures to simulate usage
5struct KorumaAttr {
6    name: String,
7}
8
9impl syn::parse::Parse for KorumaAttr {
10    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
11        // Simple parser for demonstration: expects identifier
12        let ident: syn::Ident = input.parse()?;
13        Ok(KorumaAttr {
14            name: ident.to_string(),
15        })
16    }
17}
18
19fn parse_field(field: &Field) {
20    println!("Processing field attributes...");
21
22    // Automatically looks inside cfg_attr and handles nesting
23    let koruma_attrs = field.attrs.find_attribute("koruma");
24
25    if koruma_attrs.is_empty() {
26        println!("No koruma attributes found.");
27        return;
28    }
29
30    for expanded in koruma_attrs {
31        match expanded.parse_args::<KorumaAttr>() {
32            Ok(attr) => {
33                println!("Found koruma attribute: {}", attr.name);
34                if let ExpandedAttr::Nested { condition, .. } = &expanded {
35                    println!("  - Condition: {}", condition);
36                }
37            },
38            Err(e) => println!("  - Error parsing args: {}", e),
39        }
40    }
41}
42
43fn main() {
44    // Example 1: Direct attribute
45    let field1: Field = parse_quote! {
46        #[koruma(skip)]
47        field1: i32
48    };
49    parse_field(&field1);
50
51    // Example 2: Nested inside cfg_attr
52    let field2: Field = parse_quote! {
53        #[cfg_attr(feature = "validation", koruma(required), other_attr)]
54        field2: String
55    };
56    parse_field(&field2);
57
58    // Example 3: Deeply nested
59    let field3: Field = parse_quote! {
60        #[cfg_attr(all(), cfg_attr(any(), koruma(validate)))]
61        field3: u32
62    };
63    parse_field(&field3);
64}