use crate::error::Result;
use crate::uuid::{BluetoothUuid, IntoUuid};
impl BluetoothUuid {
pub const fn as_uuid(&self) -> ::uuid::Uuid {
::uuid::Uuid::from_u128(self.as_u128())
}
}
impl From<::uuid::Uuid> for BluetoothUuid {
fn from(uuid: ::uuid::Uuid) -> Self {
Self::from_u128(uuid.as_u128())
}
}
impl From<&::uuid::Uuid> for BluetoothUuid {
fn from(uuid: &::uuid::Uuid) -> Self {
Self::from_u128(uuid.as_u128())
}
}
impl From<BluetoothUuid> for ::uuid::Uuid {
fn from(uuid: BluetoothUuid) -> Self {
uuid.as_uuid()
}
}
impl IntoUuid for ::uuid::Uuid {
fn into_uuid(self) -> Result<BluetoothUuid> {
Ok(self.into())
}
}
impl IntoUuid for &::uuid::Uuid {
fn into_uuid(self) -> Result<BluetoothUuid> {
Ok(self.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_a_vendor_uuid() {
const MUSE_CONTROL: ::uuid::Uuid =
::uuid::Uuid::from_u128(0x273e0001_4c4d_454d_96be_f03bac821358);
let uuid: BluetoothUuid = MUSE_CONTROL.into();
assert_eq!(uuid.as_str(), "273e0001-4c4d-454d-96be-f03bac821358");
assert_eq!(uuid.as_uuid(), MUSE_CONTROL);
assert_eq!(::uuid::Uuid::from(uuid), MUSE_CONTROL);
}
#[test]
fn keeps_the_base_uuid_of_an_assigned_number() {
let battery = BluetoothUuid::from_u16(0x180f);
assert_eq!(
battery.as_uuid(),
::uuid::Uuid::from_u128(0x0000180f_0000_1000_8000_00805f9b34fb)
);
assert_eq!(BluetoothUuid::from(battery.as_uuid()), battery);
}
#[test]
fn a_uuid_names_an_attribute_directly() {
const SERVICE: ::uuid::Uuid =
::uuid::Uuid::from_u128(0x0000fe8d_0000_1000_8000_00805f9b34fb);
assert_eq!(
SERVICE.into_uuid().unwrap(),
BluetoothUuid::from_u16(0xfe8d)
);
assert_eq!(
(&SERVICE).into_uuid().unwrap(),
BluetoothUuid::from_u16(0xfe8d)
);
}
}