@@ -7,6 +7,7 @@ use futures::stream::StreamExt;
77use futures_util:: stream:: BoxStream ;
88use log:: error;
99use serde:: { Deserialize , Serialize } ;
10+ use std:: borrow:: Cow ;
1011use std:: fmt:: Display ;
1112use std:: future;
1213use std:: str:: FromStr ;
@@ -41,7 +42,7 @@ pub enum RadioMessage {
4142}
4243
4344/// Bluetooth Low Energy ID, used to filter available devices.
44- #[ derive( Debug , Clone ) ]
45+ #[ derive( Debug , Clone , PartialEq ) ]
4546pub enum BleId {
4647 /// A Meshtastic device identified by its broadcast name.
4748 Name ( String ) ,
@@ -72,6 +73,15 @@ impl BleId {
7273 }
7374}
7475
76+ impl TryFrom < u64 > for BleId {
77+ type Error = Error ;
78+
79+ fn try_from ( value : u64 ) -> Result < Self , Self :: Error > {
80+ let mac_address = BDAddr :: try_from ( value) ?;
81+ Ok ( Self :: MacAddress ( mac_address) )
82+ }
83+ }
84+
7585impl From < BDAddr > for BleId {
7686 fn from ( mac : BDAddr ) -> Self {
7787 BleId :: MacAddress ( mac)
@@ -96,6 +106,30 @@ pub struct BleDevice {
96106 pub mac_address : BDAddr ,
97107}
98108
109+ impl < ' a > From < BleId > for Cow < ' a , BleId > {
110+ fn from ( ble_id : BleId ) -> Self {
111+ Cow :: Owned ( ble_id)
112+ }
113+ }
114+
115+ impl < ' a > From < & ' a BleId > for Cow < ' a , BleId > {
116+ fn from ( ble_id : & ' a BleId ) -> Self {
117+ Cow :: Borrowed ( ble_id)
118+ }
119+ }
120+
121+ impl < ' a > From < & BleDevice > for Cow < ' a , BleId > {
122+ fn from ( device : & BleDevice ) -> Self {
123+ Cow :: Owned ( device. mac_address . into ( ) )
124+ }
125+ }
126+
127+ impl < ' a > From < BleDevice > for Cow < ' a , BleId > {
128+ fn from ( device : BleDevice ) -> Self {
129+ Cow :: Owned ( device. mac_address . into ( ) )
130+ }
131+ }
132+
99133impl BleHandler {
100134 pub async fn new ( ble_id : & BleId , scan_duration : Duration ) -> Result < Self , Error > {
101135 let ( radio, adapter) = Self :: find_ble_radio ( ble_id, scan_duration) . await ?;
@@ -350,3 +384,37 @@ impl BleHandler {
350384 } ) ) )
351385 }
352386}
387+
388+ #[ cfg( test) ]
389+ mod tests {
390+ use super :: * ;
391+
392+ #[ test]
393+ fn test_ble_id_to_cow ( ) {
394+ let ble_id_name = BleId :: from_name ( "TestDevice" ) ;
395+ let cow_from_ref: Cow < ' _ , BleId > = ( & ble_id_name) . into ( ) ;
396+ assert_eq ! ( * cow_from_ref, ble_id_name) ;
397+
398+ let cow_from_owned: Cow < ' _ , BleId > = ble_id_name. into ( ) ;
399+ assert ! ( matches!( cow_from_owned, Cow :: Owned ( _) ) ) ;
400+
401+ let ble_id_mac = BleId :: try_from ( 0x001122334455 ) . unwrap ( ) ;
402+ let cow_from_owned_mac: Cow < ' _ , BleId > = ble_id_mac. clone ( ) . into ( ) ;
403+ assert_eq ! ( * cow_from_owned_mac, ble_id_mac) ;
404+ }
405+
406+ #[ test]
407+ fn test_ble_device_to_cow ( ) {
408+ let ble_device = BleDevice {
409+ name : None ,
410+ mac_address : BDAddr :: try_from ( 0x001122334455 ) . unwrap ( ) ,
411+ } ;
412+
413+ let cow_from_ble_device_ref: Cow < ' _ , BleId > = ( & ble_device) . into ( ) ;
414+ let ble_id_mac = BleId :: try_from ( 0x001122334455 ) . unwrap ( ) ;
415+ assert_eq ! ( * cow_from_ble_device_ref, ble_id_mac) ;
416+
417+ let cow_from_ble_device_owned: Cow < ' _ , BleId > = ble_device. into ( ) ;
418+ assert_eq ! ( * cow_from_ble_device_owned, ble_id_mac) ;
419+ }
420+ }
0 commit comments