Skip to content

Commit

Permalink
Use const Signature for matching
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Nov 5, 2024
1 parent 508f0e7 commit cf3315d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 41 deletions.
35 changes: 34 additions & 1 deletion atspi-common/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,39 @@ use crate::{InterfaceSet, ObjectRef, Role, StateSet};
use serde::{Deserialize, Serialize};
use zbus_lockstep_macros::validate;
use zbus_names::UniqueName;
use zvariant::{ObjectPath, Type};
use zvariant::{
signature::{Child, Fields},
ObjectPath, Signature, Type,
};

// ((so)(so)(so)iiassusau)
pub const CACHE_ITEM_SIGNATURE: &Signature = &Signature::static_structure(&[
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::I32,
&Signature::I32,
&Signature::Array(Child::Static { child: &Signature::Str }),
&Signature::Str,
&Signature::U32,
&Signature::Str,
&Signature::Array(Child::Static { child: &Signature::U32 }),
]);
pub const LEGACY_CACHE_ITEM_SIGNATURE: &Signature = &Signature::static_structure(&[
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Array(Child::Static {
child: &Signature::Structure(Fields::Static {
fields: &[&Signature::Str, &Signature::ObjectPath],
}),
}),
&Signature::Array(Child::Static { child: &Signature::Str }),
&Signature::Str,
&Signature::U32,
&Signature::Str,
&Signature::Array(Child::Static { child: &Signature::U32 }),
]);

/// The item type provided by `Cache:Add` signals
#[allow(clippy::module_name_repetitions)]
Expand Down Expand Up @@ -128,4 +160,5 @@ fn zvariant_type_signature_of_legacy_cache_item() {
*<LegacyCacheItem as Type>::SIGNATURE,
zbus::zvariant::Signature::from_str("((so)(so)(so)a(so)assusau)").unwrap()
);
assert_eq!(<LegacyCacheItem as Type>::SIGNATURE, LEGACY_CACHE_ITEM_SIGNATURE,);
}
95 changes: 58 additions & 37 deletions atspi-common/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ pub mod window;
// EVENT_LISTENER_SIGNATURE is a type signature used to notify when events are registered or deregistered.
// CACHE_ADD_SIGNATURE and *_REMOVE have very different types
// Same as "(siiva{sv})"
pub static ATSPI_EVENT_SIGNATURE: Signature = Signature::static_structure(&[
pub const ATSPI_EVENT_SIGNATURE: &Signature = &Signature::static_structure(&[
&Signature::Str,
&Signature::I32,
&Signature::I32,
&Signature::Variant,
&Signature::Array(Child::Static {
child: &Signature::Dict {
key: Child::Static { child: &Signature::Str },
value: Child::Static { child: &Signature::Variant },
},
}),
&Signature::Dict {
key: Child::Static { child: &Signature::Str },
value: Child::Static { child: &Signature::Variant },
},
]);
pub const EVENT_NAME_SIGNATURE: &Signature =
&Signature::static_structure(&[&Signature::Str, &Signature::Str]);
// Same as "(siiv(so))"
pub static QSPI_EVENT_SIGNATURE: Signature = Signature::static_structure(&[
pub const QSPI_EVENT_SIGNATURE: &Signature = &Signature::static_structure(&[
&Signature::Str,
&Signature::I32,
&Signature::I32,
&Signature::Variant,
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
]);
// Same as "(so)"
pub static EVENT_LISTENER_SIGNATURE: Signature =
Signature::static_structure(&[&Signature::Str, &Signature::ObjectPath]);
pub const EVENT_LISTENER_SIGNATURE: &Signature =
&Signature::static_structure(&[&Signature::Str, &Signature::ObjectPath]);
// Same as "((so)(so)(so)iiassusau)"
pub static CACHE_ADD_SIGNATURE: Signature = Signature::static_structure(&[
pub const CACHE_ADD_SIGNATURE: &Signature = &Signature::static_structure(&[
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
&Signature::Structure(Fields::Static { fields: &[&Signature::Str, &Signature::ObjectPath] }),
Expand Down Expand Up @@ -594,10 +594,10 @@ impl TryFrom<&zbus::Message> for EventBodyOwned {
let body = message.body();
let signature = body.signature();

if signature == &QSPI_EVENT_SIGNATURE {
if signature == QSPI_EVENT_SIGNATURE {
let qt_body = body.deserialize::<EventBodyQT>()?;
Ok(EventBodyOwned::from(qt_body))
} else if signature == &ATSPI_EVENT_SIGNATURE {
} else if signature == ATSPI_EVENT_SIGNATURE {
Ok(body.deserialize::<EventBodyOwned>()?)
} else {
Err(AtspiError::Conversion(
Expand Down Expand Up @@ -822,7 +822,6 @@ impl TryFrom<&zbus::Message> for Event {
let header = msg.header();

let body_signature = body.signature();
let body_signature_str = body_signature.to_string_no_parens();

let member = header.member().ok_or(AtspiError::MissingMember)?;
let member_str = member.as_str();
Expand All @@ -834,48 +833,70 @@ impl TryFrom<&zbus::Message> for Event {
// parentheses are not included in the signature.
// However, `Cache` signals are often emitted with outer parentheses, so we also need to
// match against the same signature, but with outer parentheses.
match (interface_str, member_str, body_signature_str.as_str()) {
("org.a11y.atspi.Socket", "Available", "so") => {
match (interface_str, member_str, body_signature) {
("org.a11y.atspi.Socket", "Available", sig)
if sig == crate::object_ref::OBJECT_REF_SIGNATURE =>
{
Ok(AvailableEvent::try_from(msg)?.into())
}
("org.a11y.atspi.Event.Object", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Object", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Object(ObjectEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Document", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Document", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Document(DocumentEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Window", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Window", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Window(WindowEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Terminal", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Terminal", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Terminal(TerminalEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Mouse", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Mouse", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Mouse(MouseEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Focus", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Focus", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Focus(FocusEvents::try_from(msg)?))
}
("org.a11y.atspi.Event.Keyboard", _, "siiva{sv}" | "siiv(so)") => {
("org.a11y.atspi.Event.Keyboard", _, sig)
if sig == ATSPI_EVENT_SIGNATURE || sig == QSPI_EVENT_SIGNATURE =>
{
Ok(Event::Keyboard(KeyboardEvents::try_from(msg)?))
}
("org.a11y.atspi.Registry", "EventListenerRegistered", "ss") => {
("org.a11y.atspi.Registry", "EventListenerRegistered", sig)
if sig == EVENT_NAME_SIGNATURE =>
{
Ok(EventListenerRegisteredEvent::try_from(msg)?.into())
}
("org.a11y.atspi.Registry", "EventListenerDeregistered", "ss") => {
("org.a11y.atspi.Registry", "EventListenerDeregistered", sig)
if sig == EVENT_NAME_SIGNATURE =>
{
Ok(EventListenerDeregisteredEvent::try_from(msg)?.into())
}
(
"org.a11y.atspi.Cache",
"AddAccessible",
"(so)(so)(so)iiassusau" | "((so)(so)(so)iiassusau)",
) => Ok(AddAccessibleEvent::try_from(msg)?.into()),
(
"org.a11y.atspi.Cache",
"AddAccessible",
"(so)(so)(so)a(so)assusau" | "((so)(so)(so)a(so)assusau)",
) => Ok(LegacyAddAccessibleEvent::try_from(msg)?.into()),
("org.a11y.atspi.Cache", "RemoveAccessible", "so" | "(so)") => {
("org.a11y.atspi.Cache", "AddAccessible", sig)
if sig == crate::cache::CACHE_ITEM_SIGNATURE =>
{
Ok(AddAccessibleEvent::try_from(msg)?.into())
}
("org.a11y.atspi.Cache", "AddAccessible", sig)
if sig == crate::cache::LEGACY_CACHE_ITEM_SIGNATURE =>
{
Ok(LegacyAddAccessibleEvent::try_from(msg)?.into())
}
("org.a11y.atspi.Cache", "RemoveAccessible", sig)
if sig == crate::object_ref::OBJECT_REF_SIGNATURE =>
{
Ok(RemoveAccessibleEvent::try_from(msg)?.into())
}
(_iface, _method, sig) => Err(AtspiError::UnknownBusSignature(sig.to_string())),
Expand Down Expand Up @@ -1013,7 +1034,7 @@ mod tests {

#[test]
fn check_event_body_qt_signature() {
assert_eq!(<EventBodyQT as Type>::SIGNATURE, &QSPI_EVENT_SIGNATURE);
assert_eq!(<EventBodyQT as Type>::SIGNATURE, QSPI_EVENT_SIGNATURE);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions atspi-common/src/object_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use zbus_names::{OwnedUniqueName, UniqueName};
use zvariant::{ObjectPath, OwnedObjectPath, Signature, Type};

// Equiv to "(so)"
pub static OBJECT_REF_SIGNATURE: Signature =
Signature::static_structure(&[&Signature::Str, &Signature::ObjectPath]);
pub const OBJECT_REF_SIGNATURE: &Signature =
&Signature::static_structure(&[&Signature::Str, &Signature::ObjectPath]);

/// `ObjectRef` type
///
Expand Down Expand Up @@ -79,7 +79,7 @@ impl TryFrom<zvariant::OwnedValue> for ObjectRef {
fn try_from<'a>(value: zvariant::OwnedValue) -> Result<Self, Self::Error> {
match &*value {
zvariant::Value::Structure(s) => {
if *s.signature() != OBJECT_REF_SIGNATURE {
if s.signature() != OBJECT_REF_SIGNATURE {
return Err(zvariant::Error::SignatureMismatch(s.signature().clone(), format!("To turn a zvariant::Value into an atspi::ObjectRef, it must be of type {OBJECT_REF_SIGNATURE}")));
}
let fields = s.fields();
Expand Down

0 comments on commit cf3315d

Please sign in to comment.