Skip to content

Commit

Permalink
Merge pull request #205 from odilia-app/all-into-string-becomes-display
Browse files Browse the repository at this point in the history
Add Display impl for State
  • Loading branch information
TTWNO committed Jun 29, 2024
2 parents 34cc6bd + ee2a44a commit f4ca3cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion atspi-common/src/events/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ impl From<StateChangedEvent> for EventBodyOwned {
fn from(event: StateChangedEvent) -> Self {
EventBodyOwned {
properties: std::collections::HashMap::new(),
kind: event.state.into(),
kind: event.state.to_string(),
detail1: event.enabled.into(),
detail2: i32::default(),
any_data: u8::default().into(),
Expand Down
38 changes: 23 additions & 15 deletions atspi-common/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{
ser::{SerializeSeq, Serializer},
Deserialize, Serialize,
};
use std::fmt;
use std::{convert::Infallible, fmt, str::FromStr};
use zvariant::{Signature, Type};

/// Used by various interfaces indicating every possible state
Expand Down Expand Up @@ -221,9 +221,9 @@ pub enum State {
ReadOnly,
}

impl From<State> for String {
fn from(state: State) -> String {
match state {
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let state_str = match self {
State::Invalid => "invalid",
State::Active => "active",
State::Armed => "armed",
Expand Down Expand Up @@ -268,8 +268,8 @@ impl From<State> for String {
State::Checkable => "checkable",
State::HasPopup => "has-popup",
State::ReadOnly => "read-only",
}
.to_string()
};
f.write_str(state_str)
}
}

Expand All @@ -279,6 +279,13 @@ impl From<String> for State {
}
}

impl FromStr for State {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}

impl From<&str> for State {
fn from(string: &str) -> State {
match string {
Expand Down Expand Up @@ -338,20 +345,21 @@ pub struct StateSet(BitFlags<State>);
impl StateSet {
/// Create a new `StateSet`.
///
///## Example
///```Rust
/// let states = State::Focusable | State::Sensitive | State::Active;
/// let set = StateSet::new(states);
/// ## Example
/// ```rust
/// # use atspi_common::{State, StateSet};
/// let states = State::Focusable | State::Sensitive | State::Active;
/// let set = StateSet::new(states);
///
/// assert!(set.contains(State::Active));
/// assert!(!set.contains(State::Busy));
/// assert!(set.contains(State::Active));
/// assert!(!set.contains(State::Busy));
/// ```
pub fn new<B: Into<BitFlags<State>>>(value: B) -> Self {
Self(value.into())
}

/// Returns the `StateSet` that corresponds to the provided `u64`s bit pattern.
///# Errors
/// # Errors
/// When the argument encodes an undefined [`State`].
pub fn from_bits(bits: u64) -> Result<StateSet, FromBitsError<State>> {
Ok(StateSet(BitFlags::from_bits(bits)?))
Expand Down Expand Up @@ -640,7 +648,7 @@ mod tests {
for state in
StateSet::from_bits(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111).unwrap()
{
let state_str: String = state.into();
let state_str: String = state.to_string();
let state_two: State = state_str.clone().into();
assert_eq!(
state, state_two,
Expand All @@ -654,7 +662,7 @@ mod tests {
StateSet::from_bits(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111).unwrap()
{
let serde_state_str: String = serde_plain::to_string(&state).unwrap();
let state_str: String = state.into();
let state_str: String = state.to_string();
assert_eq!(serde_state_str, state_str);
let state_two: State = serde_plain::from_str(&state_str).unwrap();
assert_eq!(state, state_two, "The {state:?} was serialized as {state_str}, which deserializes to {state_two:?} (serde)");
Expand Down

0 comments on commit f4ca3cc

Please sign in to comment.