Skip to content

Conversation

@szymonlesisz
Copy link
Contributor

adding missing conversion from &str

resolve: #296

use case (see btleplug feature)

  1. store DeviceId as string in database
  2. retrieve DeviceId from database and call BaluetoothSession.get_device_info(restored_id)

alternatively restore btleplug PeripheralId (wrapper for DeviceId) which i believe is referred in related issue

@szymonlesisz szymonlesisz force-pushed the feat/deviceid-from-str branch from e796108 to c75ca79 Compare October 29, 2025 14:03
@codecov
Copy link

codecov bot commented Nov 3, 2025

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 46.44%. Comparing base (23ab0f2) to head (e4200f5).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
bluez-async/src/device.rs 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #348      +/-   ##
==========================================
+ Coverage   46.14%   46.44%   +0.30%     
==========================================
  Files          28       28              
  Lines        2659     2676      +17     
  Branches     2659     2676      +17     
==========================================
+ Hits         1227     1243      +16     
  Misses       1375     1375              
- Partials       57       58       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}
}

impl TryFrom<&str> for DeviceId {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about implementing FromStr instead? That's more common for parsing things from a string.

fn from_string() {
let device_id = DeviceId::try_from("hci0/dev_11_22_33_44_55_66");
assert_eq!(device_id.unwrap().to_string(), "hci0/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.


fn try_from(s: &str) -> Result<Self, Self::Error> {
let path = Path::try_from(format!("/org/bluez/{}", s))
.map_err(|_| dbus::Error::new_failed("Invalid D-Bus path"))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need to map the error here?

@szymonlesisz szymonlesisz force-pushed the feat/deviceid-from-str branch from c75ca79 to e4200f5 Compare November 5, 2025 13:21
@qwandor qwandor merged commit df1242f into bluez-rs:main Nov 5, 2025
6 checks passed
@szymonlesisz szymonlesisz mentioned this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

consider add pub init method for DeviceId

2 participants