Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_message_parts into from_message #183

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions atspi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
/// No Signature.
MissingSignature,

/// When matching on a signal with an event, but the event does not match the signal.
EventMismatch,

/// When matching on an unknown role
UnknownRole(u32),

Expand Down Expand Up @@ -86,6 +89,7 @@
Self::MissingInterface => f.write_str("Missing interface."),
Self::MissingMember => f.write_str("Missing member."),
Self::MissingSignature => f.write_str("Missing signature."),
Self::EventMismatch => f.write_str("Event <--> Message mismatch"),

Check warning on line 92 in atspi-common/src/error.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/error.rs#L92

Added line #L92 was not covered by tests
Self::UnknownRole(e) => f.write_str(&format!("atspi: Unknown role: {e}")),
Self::UnknownSignal => f.write_str("atspi: Unknown signal"),
Self::CacheVariantMismatch => f.write_str("atspi: Cache variant mismatch"),
Expand Down
51 changes: 31 additions & 20 deletions atspi-common/src/events/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::AtspiError,
events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef},
events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString},
Event, EventProperties, EventTypeProperties,
};
use zbus_names::BusName;
Expand Down Expand Up @@ -102,41 +102,41 @@ impl HasMatchRule for DocumentEvents {
/// `LibreOffice` has loaded a document from disk.
#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct LoadCompleteEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::object_ref::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

/// An event triggered by a reloading of a document.
/// For example: pressing F5, or `Control + r` will reload a page in a web browser.
#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct ReloadEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

/// An event triggered by the cancelling of a document load.
/// For example: during the loading of a large web page, a user may press `Escape` to stop loading the page.
#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct LoadStoppedEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct ContentChangedEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct AttributesChangedEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct PageChangedEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

Expand All @@ -149,9 +149,11 @@ impl BusProperties for LoadCompleteEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand All @@ -167,15 +169,16 @@ impl BusProperties for ReloadEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
}
}

impl BusProperties for LoadStoppedEvent {
const DBUS_MEMBER: &'static str = "LoadStopped";
const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document";
Expand All @@ -185,9 +188,11 @@ impl BusProperties for LoadStoppedEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand All @@ -203,9 +208,11 @@ impl BusProperties for ContentChangedEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand All @@ -221,9 +228,11 @@ impl BusProperties for AttributesChangedEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand All @@ -239,9 +248,11 @@ impl BusProperties for PageChangedEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand Down
10 changes: 6 additions & 4 deletions atspi-common/src/events/focus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::AtspiError,
events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef},
events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString},
Event, EventProperties, EventTypeProperties,
};
use zbus_names::BusName;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl HasMatchRule for FocusEvents {

#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)]
pub struct FocusEvent {
/// The [`ObjectRef`] which the event applies to.
/// The [`ObjectRef`][crate::events::ObjectRef] which the event applies to.
pub item: crate::events::ObjectRef,
}

Expand All @@ -72,9 +72,11 @@ impl BusProperties for FocusEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
Ok(Self { item: msg.try_into()? })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand Down
13 changes: 10 additions & 3 deletions atspi-common/src/events/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
error::AtspiError,
events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef},
Event, EventProperties, EventTypeProperties,
events::{
BusProperties, EventBodyOwned, EventProperties, HasMatchRule, HasRegistryEventString,
ObjectRef,
},
Event, EventTypeProperties,
};
use zbus_names::BusName;
use zvariant::{ObjectPath, OwnedValue};
Expand Down Expand Up @@ -75,9 +78,13 @@ impl BusProperties for ModifiersEvent {

type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let item = ObjectRef::try_from(msg)?;
let body: EventBodyOwned = msg.try_into()?;
Ok(Self { item, previous_modifiers: body.detail1, current_modifiers: body.detail2 })
}

fn body(&self) -> Self::Body {
let copy = self.clone();
copy.into()
Expand Down
74 changes: 59 additions & 15 deletions atspi-common/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,13 @@ impl BusProperties for LegacyAddAccessibleEvent {

type Body = LegacyCacheItem;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item, node_added: body })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let body = msg.body();
let node_added: Self::Body = body.deserialize::<Self::Body>()?;
let item = msg.try_into()?;

Ok(Self { item, node_added })
}

fn body(&self) -> Self::Body {
Expand Down Expand Up @@ -468,8 +473,13 @@ impl BusProperties for AddAccessibleEvent {

type Body = CacheItem;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item, node_added: body })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let body = msg.body();
let node_added: Self::Body = body.deserialize::<Self::Body>()?;
let item = msg.try_into()?;

Ok(Self { item, node_added })
}

fn body(&self) -> Self::Body {
Expand Down Expand Up @@ -504,6 +514,7 @@ impl_from_user_facing_event_for_interface_event_enum!(
impl_from_user_facing_type_for_event_enum!(RemoveAccessibleEvent, Event::Cache);
impl_try_from_event_for_user_facing_type!(RemoveAccessibleEvent, CacheEvents::Remove, Event::Cache);
event_test_cases!(RemoveAccessibleEvent);

impl BusProperties for RemoveAccessibleEvent {
const REGISTRY_EVENT_STRING: &'static str = "Cache:Remove";
const MATCH_RULE_STRING: &'static str =
Expand All @@ -513,9 +524,15 @@ impl BusProperties for RemoveAccessibleEvent {

type Body = ObjectRef;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item, node_removed: body })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let body = msg.body();
let node_removed: Self::Body = body.deserialize::<Self::Body>()?;
let item = msg.try_into()?;

Ok(Self { item, node_removed })
}

fn body(&self) -> Self::Body {
self.node_removed.clone()
}
Expand Down Expand Up @@ -569,11 +586,11 @@ impl TryFrom<&zbus::Message> for ObjectRef {
fn try_from(message: &zbus::Message) -> Result<Self, Self::Error> {
let header = message.header();
let path = header.path().expect("returned path is either `Some` or panics");
let owned_path: OwnedObjectPath = path.clone().into();
let owned_path: OwnedObjectPath = path.as_ref().into();

let sender: UniqueName<'_> = header.sender().expect("No sender in header").into();
let sender: UniqueName<'_> = header.sender().expect("No sender in header").as_ref();
let bus_name: BusName<'_> = sender.into();
let name: OwnedBusName = bus_name.to_owned().into();
let name = OwnedBusName::from(bus_name);

Ok(ObjectRef { name, path: owned_path })
}
Expand Down Expand Up @@ -710,9 +727,13 @@ impl BusProperties for EventListenerDeregisteredEvent {

type Body = EventListeners;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let item = msg.try_into()?;
let body: Self::Body = msg.body().deserialize::<Self::Body>()?;
Ok(Self { item, deregistered_event: body })
}

fn body(&self) -> Self::Body {
self.deregistered_event.clone()
}
Expand Down Expand Up @@ -743,6 +764,7 @@ impl_try_from_event_for_user_facing_type!(
Event::Listener
);
event_test_cases!(EventListenerRegisteredEvent);

impl BusProperties for EventListenerRegisteredEvent {
const REGISTRY_EVENT_STRING: &'static str = "Registry:EventListenerRegistered";
const MATCH_RULE_STRING: &'static str =
Expand All @@ -752,9 +774,13 @@ impl BusProperties for EventListenerRegisteredEvent {

type Body = EventListeners;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let item = msg.try_into()?;
let body: Self::Body = msg.body().deserialize::<Self::Body>()?;
Ok(Self { item, registered_event: body })
}

fn body(&self) -> Self::Body {
self.registered_event.clone()
}
Expand Down Expand Up @@ -786,6 +812,7 @@ impl TryFrom<Event> for AvailableEvent {
}
}
event_test_cases!(AvailableEvent);

impl BusProperties for AvailableEvent {
const REGISTRY_EVENT_STRING: &'static str = "Socket:Available";
const MATCH_RULE_STRING: &'static str =
Expand All @@ -795,8 +822,12 @@ impl BusProperties for AvailableEvent {

type Body = ObjectRef;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item, socket: body })
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> Result<Self, AtspiError> {
let item = msg.try_into()?;
let body = msg.body();
let socket: Self::Body = body.deserialize::<Self::Body>()?;
Ok(Self { item, socket })
}
fn body(&self) -> Self::Body {
self.socket.clone()
Expand Down Expand Up @@ -957,13 +988,26 @@ pub trait BusProperties {
/// What is the body type of this event.
type Body: Type + Serialize + for<'a> Deserialize<'a>;

/// Build the event from the object pair (`ObjectRef` and the Body).
/// Build the event from the `zbus::Message`.
///
/// When called on a `&zbus::Message` from a message stream, you may want to make
/// sure event and message match. There is a helper to check for the match:
///
/// ```ignore
/// if !msg.matches_event::<EventType>()? {
/// return Err(AtspiError::EventMismatch);
/// }
/// let event = EventType::try_from_message(msg)?;
/// ```
///
/// These checks are already performed in the `Event::try_from` implementation, therefore the check is omited in the implementations of `BusProperties`.
///
/// # Errors
///
/// When the body type, which is what the raw message looks like over `DBus`, does not match the type that is expected for the given event.
/// It is not possible for this to error on most events, but on events whose raw message [`Self::Body`] type contains a [`enum@zvariant::Value`], you may get errors when constructing the structure.
fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError>
#[cfg(feature = "zbus")]
fn try_from_message(msg: &zbus::Message) -> std::result::Result<Self, AtspiError>
where
Self: Sized;

Expand Down
Loading
Loading