Skip to content

Commit c75ca79

Browse files
committed
Add DeviceId from str conversion
1 parent 23ab0f2 commit c75ca79

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

bluez-async/src/device.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use dbus::Path;
33
use dbus::arg::{PropMap, RefArg, Variant, cast};
44
use serde::{Deserialize, Serialize};
55
use std::collections::HashMap;
6+
use std::convert::TryFrom;
67
use std::fmt::{self, Display, Formatter};
78
use std::str::FromStr;
89
use uuid::Uuid;
@@ -41,6 +42,16 @@ impl From<DeviceId> for Path<'static> {
4142
}
4243
}
4344

45+
impl TryFrom<&str> for DeviceId {
46+
type Error = dbus::Error;
47+
48+
fn try_from(s: &str) -> Result<Self, Self::Error> {
49+
let path = Path::try_from(format!("/org/bluez/{}", s))
50+
.map_err(|_| dbus::Error::new_failed("Invalid D-Bus path"))?;
51+
Ok(DeviceId { object_path: path.into_static() })
52+
}
53+
}
54+
4455
impl Display for DeviceId {
4556
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
4657
write!(
@@ -296,6 +307,12 @@ mod tests {
296307
assert_eq!(device_id.to_string(), "hci0/dev_11_22_33_44_55_66");
297308
}
298309

310+
#[test]
311+
fn from_string() {
312+
let device_id = DeviceId::try_from("hci0/dev_11_22_33_44_55_66");
313+
assert_eq!(device_id.unwrap().to_string(), "hci0/dev_11_22_33_44_55_66");
314+
}
315+
299316
#[test]
300317
fn service_data() {
301318
let uuid = uuid_from_u32(0x11223344);

0 commit comments

Comments
 (0)