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
//! Conversion functions and types representing a set of [`Interface`]s.
//!
//! Each `AccessibleProxy` will implement some set of these interfaces,
//! represented by a [`InterfaceSet`].

use enumflags2::{bitflags, BitFlag, BitFlags};
use serde::{
	de::{self, Deserializer, Visitor},
	ser::{self, Serializer},
	Deserialize, Serialize,
};
use std::fmt;
use zvariant::{Signature, Type};

/// AT-SPI interfaces an accessible object can implement.
#[bitflags]
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Interface {
	/// Interface to indicate implementation of `AccessibleProxy`.
	#[serde(rename = "org.a11y.atspi.Accessible")]
	Accessible,
	/// Interface to indicate implementation of `ActionProxy`.
	#[serde(rename = "org.a11y.atspi.Action")]
	Action,
	/// Interface to indicate implementation of `ApplicationProxy`.
	#[serde(rename = "org.a11y.atspi.Application")]
	Application,
	/// Interface to indicate implementation of `CacheProxy`.
	#[serde(rename = "org.a11y.atspi.Cache")]
	Cache,
	/// Interface to indicate implementation of `CollectionProxy`.
	#[serde(rename = "org.a11y.atspi.Collection")]
	Collection,
	/// Interface to indicate implementation of `ComponentProxy`.
	#[serde(rename = "org.a11y.atspi.Component")]
	Component,
	/// Interface to indicate implementation of `DocumentProxy`.
	#[serde(rename = "org.a11y.atspi.Document")]
	Document,
	/// Interface to indicate implementation of `DeviceEventControllerProxy`.
	#[serde(rename = "org.a11y.atspi.DeviceEventController")]
	DeviceEventController,
	/// Interface to indicate implementation of `DeviceEventListenerProxy`.
	#[serde(rename = "org.a11y.atspi.DeviceEventListener")]
	DeviceEventListener,
	/// Interface to indicate implementation of `EditableTextProxy`.
	#[serde(rename = "org.a11y.atspi.EditableText")]
	EditableText,
	/// Interface to indicate implementation of `HyperlinkProxy`.
	#[serde(rename = "org.a11y.atspi.Hyperlink")]
	Hyperlink,
	/// Interface to indicate implementation of `HypertextProxy`.
	#[serde(rename = "org.a11y.atspi.Hypertext")]
	Hypertext,
	/// Interface to indicate implementation of `ImageProxy`.
	#[serde(rename = "org.a11y.atspi.Image")]
	Image,
	/// Interface to indicate implementation of `RegistryProxy`.
	#[serde(rename = "org.a11y.atspi.Registry")]
	Registry,
	/// Interface to indicate implementation of `SelectionProxy`.
	#[serde(rename = "org.a11y.atspi.Selection")]
	Selection,
	/// Interface to indicate implementation of `SocketProxy`.
	#[serde(rename = "org.a11y.atspi.Socket")]
	Socket,
	/// Interface to indicate implementation of `TableProxy`.
	#[serde(rename = "org.a11y.atspi.Table")]
	Table,
	/// Interface to indicate implementation of `TableCellProxy`.
	#[serde(rename = "org.a11y.atspi.TableCell")]
	TableCell,
	/// Interface to indicate implementation of `TextProxy`.
	#[serde(rename = "org.a11y.atspi.Text")]
	Text,
	/// Interface to indicate implementation of `ValueProxy`.
	#[serde(rename = "org.a11y.atspi.Value")]
	Value,
}

/// A collection type which encodes the AT-SPI interfaces an accessible object has implemented.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct InterfaceSet(BitFlags<Interface>);

impl InterfaceSet {
	pub fn new<B: Into<BitFlags<Interface>>>(value: B) -> Self {
		Self(value.into())
	}

	#[must_use]
	pub fn empty() -> InterfaceSet {
		InterfaceSet(Interface::empty())
	}

	#[must_use]
	pub fn bits(&self) -> u32 {
		self.0.bits()
	}

	#[must_use]
	pub fn all() -> InterfaceSet {
		InterfaceSet(Interface::all())
	}

	pub fn contains<B: Into<BitFlags<Interface>>>(self, other: B) -> bool {
		self.0.contains(other)
	}

	pub fn insert<B: Into<BitFlags<Interface>>>(&mut self, other: B) {
		self.0.insert(other);
	}

	pub fn iter(self) -> impl Iterator<Item = Interface> {
		self.0.iter()
	}
}

impl<'de> de::Deserialize<'de> for InterfaceSet {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>,
	{
		struct InterfaceSetVisitor;

		impl<'de> Visitor<'de> for InterfaceSetVisitor {
			type Value = InterfaceSet;

			fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
				formatter.write_str("a sequence comprised of valid AT-SPI interface names")
			}

			fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
			where
				D: Deserializer<'de>,
			{
				match <Vec<Interface> as Deserialize>::deserialize(deserializer) {
					Ok(interfaces) => Ok(InterfaceSet(BitFlags::from_iter(interfaces))),
					Err(e) => Err(e),
				}
			}
		}

		deserializer.deserialize_newtype_struct("InterfaceSet", InterfaceSetVisitor)
	}
}

impl ser::Serialize for InterfaceSet {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		serializer
			.serialize_newtype_struct("InterfaceSet", &self.0.iter().collect::<Vec<Interface>>())
	}
}

impl Type for InterfaceSet {
	fn signature() -> Signature<'static> {
		<Vec<String> as Type>::signature()
	}
}

impl From<Interface> for InterfaceSet {
	fn from(value: Interface) -> Self {
		Self(value.into())
	}
}

impl std::ops::BitAnd for InterfaceSet {
	type Output = InterfaceSet;

	fn bitand(self, other: Self) -> Self::Output {
		InterfaceSet(self.0 & other.0)
	}
}

impl std::ops::BitXor for InterfaceSet {
	type Output = InterfaceSet;

	fn bitxor(self, other: Self) -> Self::Output {
		InterfaceSet(self.0 ^ other.0)
	}
}

impl std::ops::BitOr for InterfaceSet {
	type Output = InterfaceSet;

	fn bitor(self, other: Self) -> Self::Output {
		InterfaceSet(self.0 | other.0)
	}
}

#[cfg(test)]
mod tests {
	use super::{Interface, InterfaceSet};
	use zvariant::serialized::Data;
	use zvariant::{serialized::Context, to_bytes, LE};

	#[test]
	fn serialize_empty_interface_set() {
		let ctxt = Context::new_dbus(LE, 0);
		let encoded = to_bytes(ctxt, &InterfaceSet::empty()).unwrap();
		assert_eq!(encoded.bytes(), &[0, 0, 0, 0]);
	}

	#[test]
	fn deserialize_empty_interface_set() {
		let ctxt = Context::new_dbus(LE, 0);
		let encoded = to_bytes(ctxt, &InterfaceSet::empty()).unwrap();
		let (decoded, _) = encoded.deserialize::<InterfaceSet>().unwrap();
		assert_eq!(decoded, InterfaceSet::empty());
	}

	#[test]
	fn serialize_interface_set_accessible() {
		let ctxt = Context::new_dbus(LE, 0);
		let encoded = to_bytes(ctxt, &InterfaceSet::new(Interface::Accessible)).unwrap();
		assert_eq!(
			encoded.bytes(),
			&[
				30, 0, 0, 0, 25, 0, 0, 0, 111, 114, 103, 46, 97, 49, 49, 121, 46, 97, 116, 115,
				112, 105, 46, 65, 99, 99, 101, 115, 115, 105, 98, 108, 101, 0
			]
		);
	}

	#[test]
	fn deserialize_interface_set_accessible() {
		let ctxt = Context::new_dbus(LE, 0);
		let data = Data::new::<&[u8]>(
			&[
				30, 0, 0, 0, 25, 0, 0, 0, 111, 114, 103, 46, 97, 49, 49, 121, 46, 97, 116, 115,
				112, 105, 46, 65, 99, 99, 101, 115, 115, 105, 98, 108, 101, 0,
			],
			ctxt,
		);

		let (ifaceset, _) = data.deserialize::<InterfaceSet>().unwrap();
		assert_eq!(ifaceset, InterfaceSet::new(Interface::Accessible));
	}

	#[test]
	fn can_handle_multiple_interfaces() {
		let ctxt = Context::new_dbus(LE, 0);
		let object =
			InterfaceSet::new(Interface::Accessible | Interface::Action | Interface::Component);
		let encoded = to_bytes(ctxt, &object).unwrap();
		let (decoded, _) = encoded.deserialize::<InterfaceSet>().unwrap();
		assert!(object == decoded);
	}
}