fp-macros 0.7.1

Procedural macros for generating and working with Higher-Kinded Type (HKT) traits in the fp-library crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! Attribute parsing and filtering utilities.
//!
//! This module provides utilities for parsing, filtering, and working with attributes,
//! including documentation-specific attributes like `document_default` and `document_use`.

use {
	crate::core::{
		Result,
		constants::attributes,
	},
	proc_macro2::TokenStream,
	syn::{
		Attribute,
		Expr,
		ExprLit,
		Lit,
		Meta,
		parse::Parse,
		spanned::Spanned,
	},
};

/// Check if an attribute path matches a given name.
///
/// Matches both simple idents (`#[document_signature]`) and qualified paths
/// (`#[fp_macros::document_signature]`) by comparing the last path segment.
pub(crate) fn attr_matches(
	attr: &Attribute,
	name: &str,
) -> bool {
	let path = attr.path();
	path.is_ident(name) || path.segments.last().is_some_and(|seg| seg.ident == name)
}

/// Finds the index of the first attribute with the given name.
pub fn find_attribute(
	attrs: &[Attribute],
	name: &str,
) -> Option<usize> {
	attrs.iter().position(|attr| attr_matches(attr, name))
}

/// Checks if an attribute with the given name exists.
pub fn has_attribute(
	attrs: &[Attribute],
	name: &str,
) -> bool {
	attrs.iter().any(|attr| attr_matches(attr, name))
}

/// Counts the number of attributes with the given name.
pub fn count_attributes(
	attrs: &[Attribute],
	name: &str,
) -> usize {
	attrs.iter().filter(|attr| attr_matches(attr, name)).count()
}

/// Returns an error if the attribute list contains a duplicate of the given attribute.
///
/// Used by standalone attribute macros: the current attribute is consumed by rustc,
/// so any remaining occurrence in the parsed item is a duplicate.
pub fn reject_duplicate_attribute(
	attrs: &[Attribute],
	name: &str,
) -> crate::core::Result<()> {
	if has_attribute(attrs, name) {
		Err(crate::core::Error::validation(
			proc_macro2::Span::call_site(),
			format!("#[{name}] can only be used once per item. Remove the duplicate attribute"),
		))
	} else {
		Ok(())
	}
}

/// Returns true if the attribute should be kept in generated code.
///
/// This filters out documentation-specific attributes like `document_default`
/// and `document_use` which are processed by the macro system but should not
/// appear in the final generated code.
///
/// # Examples
///
/// ```ignore
/// use syn::parse_quote;
///
/// let attr: Attribute = parse_quote!(#[document_default]);
/// assert!(!should_keep_attribute(&attr));
///
/// let attr: Attribute = parse_quote!(#[derive(Debug)]);
/// assert!(should_keep_attribute(&attr));
/// ```
pub fn should_keep_attribute(attr: &Attribute) -> bool {
	!is_doc_attribute(attr)
}

/// Returns true if the attribute is documentation-specific.
///
/// Documentation-specific attributes include:
/// - `document_default`: Marks an associated type as the default for resolution
/// - `document_use`: Specifies which associated type to use for documentation
///
/// # Examples
///
/// ```ignore
/// use syn::parse_quote;
///
/// let attr: Attribute = parse_quote!(#[document_default]);
/// assert!(is_doc_attribute(&attr));
///
/// let attr: Attribute = parse_quote!(#[document_use = "Of"]);
/// assert!(is_doc_attribute(&attr));
/// ```
pub fn is_doc_attribute(attr: &Attribute) -> bool {
	attributes::DOCUMENT_SPECIFIC_ATTRS.iter().any(|name| attr.path().is_ident(name))
}

/// Filters out documentation-specific attributes from a slice.
///
/// This returns an iterator over attributes that should be kept
/// (i.e., are not documentation-specific).
///
/// # Examples
///
/// ```ignore
/// use syn::{Attribute, parse_quote};
///
/// let attrs: Vec<Attribute> = vec![
///     parse_quote!(#[document_default]),
///     parse_quote!(#[derive(Debug)]),
///     parse_quote!(#[document_use = "Of"]),
/// ];
///
/// let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();
/// assert_eq!(filtered.len(), 1); // Only #[derive(Debug)] remains
/// ```
pub fn filter_doc_attributes(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
	attrs.iter().filter(|attr| should_keep_attribute(attr))
}

/// Remove an attribute at the given index and extract its tokens.
///
/// This function removes the attribute and returns its tokens for parsing.
/// If the attribute is not in list form (i.e., has no arguments), returns empty tokens.
///
/// # Returns
///
/// The tokens from the attribute's meta list, or empty tokens if not a list.
///
/// # Examples
///
/// ```ignore
/// let idx = find_attribute(&item.attrs, "document_signature").unwrap();
/// let tokens = remove_attribute_tokens(&mut item.attrs, idx)?;
/// ```
pub fn remove_attribute_tokens(
	attrs: &mut Vec<Attribute>,
	index: usize,
) -> syn::Result<TokenStream> {
	let attr = attrs.remove(index);

	// Try to get tokens from list form: #[attr(...)]
	if let Ok(meta_list) = attr.meta.require_list() {
		Ok(meta_list.tokens.clone())
	} else {
		// Attribute has no arguments: #[attr]
		Ok(TokenStream::new())
	}
}

/// Extracts a single string value from a name-value attribute, with duplicate checking.
///
/// For example, this extracts `"SomeType"` from `#[document_use = "SomeType"]`.
///
/// Returns `Ok(Some(value))` if a unique name-value attribute is found,
/// `Ok(None)` if the attribute doesn't exist or isn't in name-value form,
/// or `Err(...)` if duplicate attributes are found.
pub fn parse_unique_attribute_value(
	attrs: &[syn::Attribute],
	name: &str,
) -> Result<Option<String>> {
	let mut found = None;
	for attr in attrs {
		if attr.path().is_ident(name) {
			if found.is_some() {
				return Err(crate::core::Error::validation(
					attr.span(),
					format!("Multiple `#[{name}]` attributes found on same item"),
				));
			}
			if let syn::Meta::NameValue(nv) = &attr.meta
				&& let syn::Expr::Lit(syn::ExprLit {
					lit: syn::Lit::Str(s), ..
				}) = &nv.value
			{
				found = Some(s.value());
			}
		}
	}
	Ok(found)
}

/// Extension trait for ergonomic attribute manipulation on `Vec<Attribute>`.
///
/// This trait provides convenient methods for common attribute operations,
/// combining finding, removing, and parsing into single operations.
///
/// # Examples
///
/// ```ignore
/// use crate::support::attributes::AttributeExt;
///
/// // Find and remove a parsed attribute
/// if let Some(args) = item.attrs.find_and_remove::<MyParsedArgs>("my_attribute")? {
///     // Process args
/// }
///
/// // Find and extract a name-value attribute's string value
/// let document_use = method.attrs.find_value("document_use")?;
///
/// // Check for attribute existence
/// if item.attrs.has_attribute("inline") {
///     // Handle inline attribute
/// }
/// ```
#[allow(dead_code, reason = "API kept for completeness")]
pub trait AttributeExt {
	/// Find, remove, and parse an attribute in one operation.
	///
	/// Returns `Ok(Some(parsed))` if the attribute was found and parsed successfully,
	/// `Ok(None)` if the attribute was not found, or `Err(...)` if parsing failed.
	///
	/// # Examples
	///
	/// ```ignore
	/// use crate::support::attributes::AttributeExt;
	///
	/// if let Some(args) = item.attrs.find_and_remove::<MyParsedArgs>("my_attribute")? {
	///     // The attribute has been removed and args are parsed
	/// }
	/// ```
	fn find_and_remove<T: Parse>(
		&mut self,
		name: &str,
	) -> Result<Option<T>>;

	/// Find and extract a name-value attribute's string value without removing it.
	///
	/// For example, extracts `"SomeType"` from `#[document_use = "SomeType"]`.
	///
	/// Returns `Ok(Some(value))` if the attribute was found with a string value,
	/// `Ok(None)` if the attribute was not found or not in name-value form,
	/// or `Err(...)` if duplicate attributes exist.
	///
	/// # Examples
	///
	/// ```ignore
	/// use crate::support::attributes::AttributeExt;
	///
	/// let document_use = method.attrs.find_value("document_use")?;
	/// ```
	fn find_value(
		&self,
		name: &str,
	) -> Result<Option<String>>;

	/// Find, remove, and extract a name-value attribute's string value.
	///
	/// This is like `find_value` but also removes the attribute from the list.
	///
	/// Returns `Ok(Some(value))` if the attribute was found and removed,
	/// or `Ok(None)` if the attribute was not found or not in name-value form.
	///
	/// # Examples
	///
	/// ```ignore
	/// use crate::support::attributes::AttributeExt;
	///
	/// if let Some(value) = item.attrs.find_and_remove_value("document_use")? {
	///     // Use value, attribute has been removed
	/// }
	/// ```
	fn find_and_remove_value(
		&mut self,
		name: &str,
	) -> Result<Option<String>>;

	/// Check if an attribute with the given name exists.
	///
	/// # Examples
	///
	/// ```ignore
	/// use crate::support::attributes::AttributeExt;
	///
	/// if item.attrs.has_attribute("inline") {
	///     // Handle inline attribute
	/// }
	/// ```
	fn has_attribute(
		&self,
		name: &str,
	) -> bool;

	/// Find and extract a name-value attribute's string value, collecting errors instead of returning them.
	///
	/// This is a convenience method that combines `find_value` with error collection,
	/// useful when processing multiple attributes where you want to collect all errors
	/// rather than short-circuit on the first failure.
	///
	/// For example, extracts `"SomeType"` from `#[document_use = "SomeType"]`.
	///
	/// Returns `Some(value)` if the attribute was found with a string value,
	/// or `None` if the attribute was not found or not in name-value form.
	/// Errors (such as duplicate attributes) are pushed to the error collector.
	///
	/// # Examples
	///
	/// ```ignore
	/// use crate::support::attributes::AttributeExt;
	/// use crate::core::error_handling::ErrorCollector;
	///
	/// let mut errors = ErrorCollector::new();
	/// let document_use = method.attrs.find_value_or_collect("document_use", &mut errors);
	/// ```
	fn find_value_or_collect(
		&self,
		name: &str,
		errors: &mut crate::core::error_handling::ErrorCollector,
	) -> Option<String>;
}

impl AttributeExt for Vec<Attribute> {
	fn find_and_remove<T: Parse>(
		&mut self,
		name: &str,
	) -> Result<Option<T>> {
		let Some(index) = find_attribute(self, name) else {
			return Ok(None);
		};

		let tokens = remove_attribute_tokens(self, index).map_err(crate::core::Error::Parse)?;

		if tokens.is_empty() {
			return Ok(None);
		}

		let parsed = syn::parse2::<T>(tokens).map_err(crate::core::Error::Parse)?;
		Ok(Some(parsed))
	}

	fn find_value(
		&self,
		name: &str,
	) -> Result<Option<String>> {
		parse_unique_attribute_value(self, name)
	}

	fn find_and_remove_value(
		&mut self,
		name: &str,
	) -> Result<Option<String>> {
		let Some(index) = find_attribute(self, name) else {
			return Ok(None);
		};

		let attr = self.remove(index);
		if let Meta::NameValue(nv) = &attr.meta
			&& let Expr::Lit(ExprLit {
				lit: Lit::Str(s), ..
			}) = &nv.value
		{
			return Ok(Some(s.value()));
		}
		Ok(None)
	}

	fn has_attribute(
		&self,
		name: &str,
	) -> bool {
		has_attribute(self, name)
	}

	fn find_value_or_collect(
		&self,
		name: &str,
		errors: &mut crate::core::error_handling::ErrorCollector,
	) -> Option<String> {
		self.find_value(name).unwrap_or_else(|e| {
			errors.push(e.into());
			None
		})
	}
}

#[cfg(test)]
#[expect(
	clippy::unwrap_used,
	clippy::indexing_slicing,
	reason = "Tests use panicking operations for brevity and clarity"
)]
mod tests {
	use super::*;

	#[test]
	fn test_has_attr() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> = vec![parse_quote!(#[doc = "test"])];
		assert!(has_attribute(&attrs, "doc"));
		assert!(!has_attribute(&attrs, "test"));
	}

	#[test]
	fn test_filter_document_default() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> =
			vec![parse_quote!(#[document_default]), parse_quote!(#[derive(Debug)])];

		let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();

		assert_eq!(filtered.len(), 1);
		assert!(filtered[0].path().is_ident("derive"));
	}

	#[test]
	fn test_filter_document_use() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> =
			vec![parse_quote!(#[document_use = "Of"]), parse_quote!(#[inline])];

		let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();

		assert_eq!(filtered.len(), 1);
		assert!(filtered[0].path().is_ident("inline"));
	}

	#[test]
	fn test_is_doc_attribute() {
		use syn::parse_quote;
		let document_default: Attribute = parse_quote!(#[document_default]);
		let document_use: Attribute = parse_quote!(#[document_use = "Of"]);
		let derive: Attribute = parse_quote!(#[derive(Debug)]);

		assert!(is_doc_attribute(&document_default));
		assert!(is_doc_attribute(&document_use));
		assert!(!is_doc_attribute(&derive));
	}

	#[test]
	fn test_should_keep_attribute() {
		use syn::parse_quote;
		let document_default: Attribute = parse_quote!(#[document_default]);
		let derive: Attribute = parse_quote!(#[derive(Debug)]);

		assert!(!should_keep_attribute(&document_default));
		assert!(should_keep_attribute(&derive));
	}

	#[test]
	fn test_filter_empty() {
		let attrs: Vec<Attribute> = vec![];
		let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();
		assert_eq!(filtered.len(), 0);
	}

	#[test]
	fn test_filter_all_doc_attrs() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> =
			vec![parse_quote!(#[document_default]), parse_quote!(#[document_use = "Of"])];

		let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();
		assert_eq!(filtered.len(), 0);
	}

	#[test]
	fn test_filter_no_doc_attrs() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> = vec![parse_quote!(#[derive(Debug)]), parse_quote!(#[inline])];

		let filtered: Vec<_> = filter_doc_attributes(&attrs).collect();
		assert_eq!(filtered.len(), 2);
	}

	#[test]
	fn test_remove_attribute_tokens() {
		use syn::{
			ItemStruct,
			LitStr,
			parse_quote,
		};
		let mut item: ItemStruct = parse_quote! {
			#[test_attr("hello")]
			#[derive(Debug)]
			struct Foo;
		};

		let idx = find_attribute(&item.attrs, "test_attr").unwrap();
		let tokens = remove_attribute_tokens(&mut item.attrs, idx).unwrap();

		// Should have removed the attribute
		assert_eq!(item.attrs.len(), 1);
		assert!(has_attribute(&item.attrs, "derive"));
		assert!(!has_attribute(&item.attrs, "test_attr"));

		// Should have extracted the tokens
		let lit: LitStr = syn::parse2(tokens).unwrap();
		assert_eq!(lit.value(), "hello");
	}

	#[test]
	fn test_remove_attribute_tokens_empty() {
		use syn::{
			ItemStruct,
			parse_quote,
		};
		let mut item: ItemStruct = parse_quote! {
			#[test_attr]
			struct Foo;
		};

		let idx = find_attribute(&item.attrs, "test_attr").unwrap();
		let tokens = remove_attribute_tokens(&mut item.attrs, idx).unwrap();

		// Should have removed the attribute
		assert_eq!(item.attrs.len(), 0);

		// Should have empty tokens
		assert!(tokens.is_empty());
	}

	#[test]
	fn test_parse_unique_attribute_value() {
		use syn::parse_quote;
		let attrs: Vec<Attribute> = vec![parse_quote!(#[test = "value"])];

		let result = parse_unique_attribute_value(&attrs, "test");
		assert!(result.is_ok());
		assert_eq!(result.unwrap(), Some("value".to_string()));

		let multi_attrs: Vec<Attribute> =
			vec![parse_quote!(#[test = "v1"]), parse_quote!(#[test = "v2"])];
		let result = parse_unique_attribute_value(&multi_attrs, "test");
		assert!(result.is_err());

		// Test invalid format (not name-value)
		let invalid_attrs: Vec<Attribute> = vec![parse_quote!(#[test])];
		let result = parse_unique_attribute_value(&invalid_attrs, "test");
		assert!(result.is_ok());
		assert_eq!(result.unwrap(), None);
	}

	// Tests for AttributeExt trait
	#[test]
	fn test_attribute_ext_find_and_remove() {
		use syn::{
			ItemStruct,
			LitStr,
			parse::Parse,
			parse_quote,
		};

		struct TestArgs {
			value: LitStr,
		}

		impl Parse for TestArgs {
			fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
				Ok(TestArgs {
					value: input.parse()?,
				})
			}
		}

		let mut item: ItemStruct = parse_quote! {
			#[test_attr("hello")]
			#[derive(Debug)]
			struct Foo;
		};

		let result = item.attrs.find_and_remove::<TestArgs>("test_attr").unwrap();

		assert!(result.is_some());
		let args = result.unwrap();
		assert_eq!(args.value.value(), "hello");
		assert_eq!(item.attrs.len(), 1);
		assert!(!item.attrs.has_attribute("test_attr"));
		assert!(item.attrs.has_attribute("derive"));
	}

	#[test]
	fn test_attribute_ext_find_and_remove_not_found() {
		use syn::{
			ItemStruct,
			LitStr,
			parse::Parse,
			parse_quote,
		};

		struct TestArgs {
			_value: LitStr,
		}

		impl Parse for TestArgs {
			fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
				Ok(TestArgs {
					_value: input.parse()?,
				})
			}
		}

		let mut item: ItemStruct = parse_quote! {
			#[derive(Debug)]
			struct Foo;
		};

		let original_len = item.attrs.len();
		let result = item.attrs.find_and_remove::<TestArgs>("nonexistent").unwrap();

		assert!(result.is_none());
		assert_eq!(item.attrs.len(), original_len);
	}

	#[test]
	fn test_attribute_ext_find_and_remove_empty_attr() {
		use syn::{
			ItemStruct,
			LitStr,
			parse::Parse,
			parse_quote,
		};

		struct TestArgs {
			_value: LitStr,
		}

		impl Parse for TestArgs {
			fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
				Ok(TestArgs {
					_value: input.parse()?,
				})
			}
		}

		let mut item: ItemStruct = parse_quote! {
			#[test_attr]
			struct Foo;
		};

		let result = item.attrs.find_and_remove::<TestArgs>("test_attr").unwrap();

		// Empty attributes should return None after removing
		assert!(result.is_none());
		// But the attribute should still be removed
		assert_eq!(item.attrs.len(), 0);
	}

	#[test]
	fn test_attribute_ext_find_value() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let item: ItemStruct = parse_quote! {
			#[document_use = "SomeType"]
			#[derive(Debug)]
			struct Foo;
		};

		let result = item.attrs.find_value("document_use").unwrap();
		assert_eq!(result, Some("SomeType".to_string()));

		// Attribute should not be removed
		assert_eq!(item.attrs.len(), 2);
	}

	#[test]
	fn test_attribute_ext_find_value_not_found() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let item: ItemStruct = parse_quote! {
			#[derive(Debug)]
			struct Foo;
		};

		let result = item.attrs.find_value("document_use").unwrap();
		assert_eq!(result, None);
	}

	#[test]
	fn test_attribute_ext_find_value_duplicate_error() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let item: ItemStruct = parse_quote! {
			#[test = "v1"]
			#[test = "v2"]
			struct Foo;
		};

		let result = item.attrs.find_value("test");
		assert!(result.is_err());
	}

	#[test]
	fn test_attribute_ext_find_and_remove_value() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let mut item: ItemStruct = parse_quote! {
			#[document_use = "SomeType"]
			#[derive(Debug)]
			struct Foo;
		};

		let result = item.attrs.find_and_remove_value("document_use").unwrap();
		assert_eq!(result, Some("SomeType".to_string()));

		// Attribute should be removed
		assert_eq!(item.attrs.len(), 1);
		assert!(!item.attrs.has_attribute("document_use"));
		assert!(item.attrs.has_attribute("derive"));
	}

	#[test]
	fn test_attribute_ext_find_and_remove_value_not_found() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let mut item: ItemStruct = parse_quote! {
			#[derive(Debug)]
			struct Foo;
		};

		let original_len = item.attrs.len();
		let result = item.attrs.find_and_remove_value("document_use").unwrap();
		assert_eq!(result, None);
		assert_eq!(item.attrs.len(), original_len);
	}

	#[test]
	fn test_attribute_ext_find_and_remove_value_not_name_value() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let mut item: ItemStruct = parse_quote! {
			#[test_attr]
			struct Foo;
		};

		let result = item.attrs.find_and_remove_value("test_attr").unwrap();
		assert_eq!(result, None);
		// Attribute should still be removed even if not name-value form
		assert_eq!(item.attrs.len(), 0);
	}

	#[test]
	fn test_attribute_ext_has_attribute() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let item: ItemStruct = parse_quote! {
			#[derive(Debug)]
			#[inline]
			struct Foo;
		};

		assert!(item.attrs.has_attribute("derive"));
		assert!(item.attrs.has_attribute("inline"));
		assert!(!item.attrs.has_attribute("test"));
	}

	#[test]
	fn test_attribute_ext_has_attribute_empty() {
		use syn::{
			ItemStruct,
			parse_quote,
		};

		let item: ItemStruct = parse_quote! {
			struct Foo;
		};

		assert!(!item.attrs.has_attribute("derive"));
	}
}