Skip to content
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
30 changes: 30 additions & 0 deletions bluez-async/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use dbus::Path;
use dbus::arg::{PropMap, RefArg, Variant, cast};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use uuid::Uuid;
Expand Down Expand Up @@ -41,6 +42,23 @@ impl From<DeviceId> for Path<'static> {
}
}

impl FromStr for DeviceId {
type Err = BluetoothError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with("hci") {
return Err(BluetoothError::DeviceIdParseError(s.to_string()));
}

let path = Path::new(format!("/org/bluez/{}", s))
.map_err(|_| BluetoothError::DeviceIdParseError(s.to_string()))?;

Ok(DeviceId {
object_path: path.into_static(),
})
}
}

impl Display for DeviceId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -296,6 +314,18 @@ mod tests {
assert_eq!(device_id.to_string(), "hci0/dev_11_22_33_44_55_66");
}

#[test]
fn from_string() {
let device_id = DeviceId::from_str("hci0/dev_11_22_33_44_55_66");
assert_eq!(device_id.unwrap().to_string(), "hci0/dev_11_22_33_44_55_66");

let invalid_id = DeviceId::from_str("dev_11_22_33_44_55_66");
assert_eq!(
invalid_id.unwrap_err().to_string(),
"Error parsing DeviceId string: dev_11_22_33_44_55_66"
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for converting an invalid path String to a DeviceId too, to make sure it returns an error as expected.

Copy link
Contributor Author

@szymonlesisz szymonlesisz Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i did some digging and turn out that dbus::Path returns Result<Path, Infallible> so it always succeeds and should never produce an error, so i ended up with adding simple validation (check if input starts with hci)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me that Path::try_from is an auto implementation based on Path::from, which in turn will panic if there is an invalid path. But Path::new will return an error.


#[test]
fn service_data() {
let uuid = uuid_from_u32(0x11223344);
Expand Down
3 changes: 3 additions & 0 deletions bluez-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub enum BluetoothError {
/// Error parsing a `Modalias` from a string.
#[error(transparent)]
ModaliasParseError(#[from] ParseModaliasError),
/// Error parsing DeviceId from a string.
#[error("Error parsing DeviceId string: {0}")]
DeviceIdParseError(String),
}

/// Error type for futures representing tasks spawned by this crate.
Expand Down
Loading