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
use weedle::interface::OperationInterfaceMember;
use weedle::mixin::OperationMixinMember;
use weedle::namespace::OperationNamespaceMember;

/// A WebIDL symbol that may have an identifier
pub trait SymbolWithOptionalIdentifier<'a> {
	fn identifier(&self) -> Option<weedle::common::Identifier<'a>>;
	fn has_identifier(&self) -> bool;
}

macro_rules! impl_symbol_with_optional_identifier {
	($($sym:ident),+ $(,)?) => {
		$(
			impl<'a> SymbolWithOptionalIdentifier<'a> for $sym<'a> {
				fn identifier(&self) -> Option<weedle::common::Identifier<'a>> {
					self.identifier
				}

				fn has_identifier(&self) -> bool {
					self.identifier.is_some()
				}
			}
		)+
	};
}

impl_symbol_with_optional_identifier!(
	OperationInterfaceMember,
	OperationMixinMember,
	OperationNamespaceMember,
);

#[cfg(test)]
mod tests {
	use crate::symbol::SymbolWithOptionalIdentifier;
	use weedle::common::Identifier;
	use weedle::interface::OperationInterfaceMember;
	use weedle::Parse;

	#[test]
	fn test_operation_interface_member() {
		let (_, output) = OperationInterfaceMember::parse(
			"static Promise<boolean> isTypeSupported(DOMString type);",
		)
		.expect("OperationInterfaceMember parsed with an error");

		assert_eq!(output.identifier(), Some(Identifier("isTypeSupported")));
		assert!(output.has_identifier());
	}
}