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

Add Display impl for State #205

Merged
merged 4 commits into from
Jun 29, 2024
Merged
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
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 @@
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 @@
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 @@
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 FromStr for State {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}

Check warning on line 286 in atspi-common/src/state.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/state.rs#L284-L286

Added lines #L284 - L286 were not covered by tests
}

impl From<&str> for State {
fn from(string: &str) -> State {
match string {
Expand Down Expand Up @@ -338,20 +345,21 @@
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 @@
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 @@
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
Loading