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
use weedle::interface::{AttributeInterfaceMember, MaplikeInterfaceMember, SetlikeInterfaceMember};
use weedle::mixin::AttributeMixinMember;

/// A WebIDL symbol that may have a readonly modifier
pub trait SymbolWithReadOnly {
	fn readonly(&self) -> Option<weedle::term::ReadOnly>;
	fn is_readonly(&self) -> bool;
	fn is_not_readonly(&self) -> bool;
}

macro_rules! impl_symbol_with_readonly {
	($($sym:ident),+ $(,)?) => {
		$(
			impl<'a> SymbolWithReadOnly for $sym<'a> {
				fn readonly(&self) -> Option<weedle::term::ReadOnly> {
					self.readonly
				}

				fn is_readonly(&self) -> bool {
					self.readonly.is_some()
				}

				fn is_not_readonly(&self) -> bool {
					self.readonly.is_none()
				}
			}
		)+
	};
}

impl_symbol_with_readonly!(
	AttributeInterfaceMember,
	MaplikeInterfaceMember,
	SetlikeInterfaceMember,
	AttributeMixinMember,
);

#[cfg(test)]
mod tests {
	use crate::symbol::SymbolWithReadOnly;
	use weedle::interface::AttributeInterfaceMember;
	use weedle::Parse;

	#[test]
	fn test_attribute_interface_member() {
		let (_, member) = AttributeInterfaceMember::parse("readonly attribute float width;")
			.expect("AttributeInterfaceMember parsed with an error");

		assert!(member.readonly().is_some());
		assert!(member.is_readonly());
		assert!(!member.is_not_readonly());
	}
}