atspi_common/
object_ref.rs1use serde::{Deserialize, Serialize};
2use zbus_lockstep_macros::validate;
3use zbus_names::{OwnedUniqueName, UniqueName};
4use zvariant::{ObjectPath, OwnedObjectPath, Type};
5
6#[validate(signal: "Available")]
15#[derive(Debug, Clone, Serialize, Deserialize, Type, PartialEq, Eq, Hash)]
16pub struct ObjectRef {
17 pub name: OwnedUniqueName,
18 pub path: OwnedObjectPath,
19}
20
21impl Default for ObjectRef {
22 fn default() -> Self {
23 ObjectRef {
24 name: UniqueName::from_static_str(":0.0").unwrap().into(),
25 path: ObjectPath::from_static_str("/org/a11y/atspi/accessible/null")
26 .unwrap()
27 .into(),
28 }
29 }
30}
31
32#[cfg(test)]
33#[test]
34fn test_accessible_from_dbus_ctxt_to_accessible() {
35 use zvariant::serialized::Context;
36 use zvariant::{to_bytes, Value, LE};
37
38 let acc = ObjectRef::default();
39 let ctxt = Context::new_dbus(LE, 0);
40 let acc_value: Value<'_> = acc.into();
41 let data = to_bytes(ctxt, &acc_value).unwrap();
42 let (value, _) = data.deserialize::<Value>().unwrap();
43 let accessible: ObjectRef = value.try_into().unwrap();
44
45 assert_eq!(accessible.name.as_str(), ":0.0");
46 assert_eq!(accessible.path.as_str(), "/org/a11y/atspi/accessible/null");
47}
48
49#[cfg(test)]
50#[test]
51fn test_accessible_value_wrapped_from_dbus_ctxt_to_accessible() {
52 use zvariant::serialized::Context;
53 use zvariant::{to_bytes, Value, LE};
54
55 let acc = ObjectRef::default();
56 let value: zvariant::Value = acc.into();
57 let ctxt = Context::new_dbus(LE, 0);
58 let encoded = to_bytes(ctxt, &value).unwrap();
59 let (value, _) = encoded.deserialize::<Value>().unwrap();
60 let accessible: ObjectRef = value.try_into().unwrap();
61
62 assert_eq!(accessible.name.as_str(), ":0.0");
63 assert_eq!(accessible.path.as_str(), "/org/a11y/atspi/accessible/null");
64}
65
66impl<'a> TryFrom<zvariant::Value<'a>> for ObjectRef {
67 type Error = zvariant::Error;
68 fn try_from(value: zvariant::Value<'a>) -> Result<Self, Self::Error> {
69 value.try_to_owned()?.try_into()
70 }
71}
72
73impl TryFrom<zvariant::OwnedValue> for ObjectRef {
74 type Error = zvariant::Error;
75 fn try_from<'a>(value: zvariant::OwnedValue) -> Result<Self, Self::Error> {
76 match &*value {
77 zvariant::Value::Structure(s) => {
78 if s.signature() != ObjectRef::SIGNATURE {
79 return Err(zvariant::Error::SignatureMismatch(s.signature().clone(), format!("To turn a zvariant::Value into an atspi::ObjectRef, it must be of type {:?}", ObjectRef::SIGNATURE)));
80 }
81 let fields = s.fields();
82 let name: String =
83 fields.first().ok_or(zvariant::Error::IncorrectType)?.try_into()?;
84 let path_value: ObjectPath<'_> =
85 fields.last().ok_or(zvariant::Error::IncorrectType)?.try_into()?;
86 Ok(ObjectRef {
87 name: name.try_into().map_err(|_| zvariant::Error::IncorrectType)?,
88 path: path_value.into(),
89 })
90 }
91 _ => Err(zvariant::Error::IncorrectType),
92 }
93 }
94}
95
96impl From<ObjectRef> for zvariant::Structure<'_> {
97 fn from(obj: ObjectRef) -> Self {
98 (obj.name, obj.path).into()
99 }
100}