Skip to content

Commit

Permalink
Merge pull request #198 from odilia-app/enabled-is-a-bool
Browse files Browse the repository at this point in the history
Enabled Is a Bool
  • Loading branch information
TTWNO committed Jun 27, 2024
2 parents e6b3aa3 + 866eb87 commit 626521d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
37 changes: 29 additions & 8 deletions atspi-common/src/events/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,33 @@ pub struct StateChangedEvent {
pub item: crate::events::ObjectRef,
/// The state to be enabled/disabled.
pub state: State,
/// Enabled or disabled the state.
///
/// 1 == enabled
///
/// 0 == disabled
pub enabled: i32,
/// Whether the state was enabled or disabled.
#[serde(with = "i32_bool_conversion")]
pub enabled: bool,
}

mod i32_bool_conversion {
use serde::{Deserialize, Deserializer, Serializer};
/// Convert an integer flag to a boolean.
/// returns true if value is more than 0, otherwise false
pub fn deserialize<'de, D>(de: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let int: i32 = Deserialize::deserialize(de)?;
Ok(int > 0)
}

/// Convert a boolean flag to an integer.
/// returns 0 if false and 1 if true
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S>(b: &bool, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let val: i32 = (*b).into();
ser.serialize_i32(val)
}
}

/// A child of an [`ObjectRef`] has been added or removed.
Expand Down Expand Up @@ -651,7 +672,7 @@ impl BusProperties for StateChangedEvent {
type Body = EventBodyOwned;

fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
Ok(Self { item, state: body.kind.into(), enabled: body.detail1 })
Ok(Self { item, state: body.kind.into(), enabled: body.detail1 > 0 })
}
fn body(&self) -> Self::Body {
let copy = self.clone();
Expand Down Expand Up @@ -1137,7 +1158,7 @@ impl From<StateChangedEvent> for EventBodyOwned {
EventBodyOwned {
properties: std::collections::HashMap::new(),
kind: event.state.into(),
detail1: event.enabled,
detail1: event.enabled.into(),
detail2: i32::default(),
any_data: u8::default().into(),
}
Expand Down
2 changes: 1 addition & 1 deletion atspi/examples/focused-async-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
while let Some(Ok(ev)) = events.next().await {
let Ok(change) = <StateChangedEvent>::try_from(ev) else { continue };

if change.state == "focused".into() && change.enabled == 1 {
if change.state == "focused".into() && change.enabled {
let bus_name = change.item.name.clone();
println!("Accessible belonging to {bus_name} focused!");
}
Expand Down
2 changes: 1 addition & 1 deletion atspi/examples/focused-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
while let Some(Ok(ev)) = events.next().await {
let Ok(change) = <StateChangedEvent>::try_from(ev) else { continue };

if change.state == "focused".into() && change.enabled == 1 {
if change.state == "focused".into() && change.enabled {
let bus_name = change.item.name.clone();
println!("Accessible belonging to {bus_name} focused!");
}
Expand Down

0 comments on commit 626521d

Please sign in to comment.