aranet_core/
util.rs

1//! Utility functions for aranet-core.
2//!
3//! This module contains shared utility functions used across the crate.
4
5use btleplug::platform::PeripheralId;
6
7/// Format a peripheral ID as a string.
8///
9/// On macOS, peripheral IDs are UUIDs. On other platforms, they may be
10/// MAC addresses or other formats. This function extracts the useful
11/// identifier string.
12///
13/// # Example
14///
15/// ```ignore
16/// use aranet_core::util::format_peripheral_id;
17///
18/// let id = peripheral.id();
19/// let formatted = format_peripheral_id(&id);
20/// println!("Device: {}", formatted);
21/// ```
22pub fn format_peripheral_id(id: &PeripheralId) -> String {
23    format!("{:?}", id)
24        .trim_start_matches("PeripheralId(")
25        .trim_end_matches(')')
26        .to_string()
27}
28
29/// Create an identifier string from an address and peripheral ID.
30///
31/// On macOS where addresses are 00:00:00:00:00:00, uses the peripheral ID.
32/// On other platforms, uses the Bluetooth address.
33pub fn create_identifier(address: &str, peripheral_id: &PeripheralId) -> String {
34    if address == "00:00:00:00:00:00" {
35        format_peripheral_id(peripheral_id)
36    } else {
37        address.to_string()
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    // Note: We can't easily create PeripheralId in tests, so we test the logic
44    // rather than calling the functions directly.
45    #[allow(unused_imports)]
46    use super::*;
47
48    #[test]
49    fn test_create_identifier_with_valid_address() {
50        // We can't easily create a PeripheralId in tests, but we can test the logic
51        let address = "AA:BB:CC:DD:EE:FF";
52        // When address is valid, it should be used directly
53        assert_ne!(address, "00:00:00:00:00:00");
54    }
55
56    #[test]
57    fn test_create_identifier_with_zero_address() {
58        let address = "00:00:00:00:00:00";
59        assert_eq!(address, "00:00:00:00:00:00");
60    }
61}