use objc2::rc::Id;
use objc2::runtime::AnyObject;
use objc2_foundation::NSString;
use uuid::Uuid;
use super::super::framework::cb;
use super::{id, nil};
pub fn cbuuid_to_uuid(cbuuid: id) -> Uuid {
let uuid = cb::uuid_uuidstring(cbuuid).to_string();
let long = if uuid.len() == 4 {
format!("0000{}-0000-1000-8000-00805f9b34fb", uuid)
} else if uuid.len() == 8 {
format!("{}-0000-1000-8000-00805f9b34fb", uuid)
} else {
uuid
};
let uuid_string = long.to_lowercase();
uuid_string.parse().unwrap()
}
pub fn uuid_to_cbuuid(uuid: Uuid) -> Id<AnyObject> {
cb::uuid_uuidwithstring(&NSString::from_str(&uuid.to_string()))
}
pub fn peripheral_debug(peripheral: id) -> String {
if peripheral == nil {
return String::from("nil");
}
let uuid = cb::peer_identifier(peripheral).UUIDString();
if let Some(name) = cb::peripheral_name(peripheral) {
format!("CBPeripheral({}, {})", name, uuid)
} else {
format!("CBPeripheral({})", uuid)
}
}
pub fn service_debug(service: id) -> String {
if service == nil {
return String::from("nil");
}
let uuid = cb::uuid_uuidstring(cb::attribute_uuid(service));
format!("CBService({})", uuid)
}
pub fn characteristic_debug(characteristic: id) -> String {
if characteristic == nil {
return String::from("nil");
}
let uuid = cb::uuid_uuidstring(cb::attribute_uuid(characteristic));
format!("CBCharacteristic({})", uuid)
}
pub fn descriptor_debug(descriptor: id) -> String {
if descriptor == nil {
return String::from("nil");
}
let uuid = cb::uuid_uuidstring(cb::attribute_uuid(descriptor));
format!("CBDescriptor({})", uuid)
}
#[cfg(test)]
mod tests {
use objc2_foundation::ns_string;
use super::super::super::framework::cb::uuid_uuidwithstring;
use super::*;
#[test]
fn parse_uuid_short() {
let uuid_string = "1234";
let uuid_nsstring = NSString::from_str(uuid_string);
let cbuuid = uuid_uuidwithstring(&uuid_nsstring);
let uuid = cbuuid_to_uuid(&*cbuuid);
assert_eq!(
uuid,
Uuid::from_u128(0x00001234_0000_1000_8000_00805f9b34fb)
);
}
#[test]
fn parse_uuid_long() {
let uuid_nsstring = ns_string!("12345678-0000-1111-2222-333344445555");
let cbuuid = uuid_uuidwithstring(uuid_nsstring);
let uuid = cbuuid_to_uuid(&*cbuuid);
assert_eq!(
uuid,
Uuid::from_u128(0x12345678_0000_1111_2222_333344445555)
);
}
#[test]
fn cbuuid_roundtrip() {
for uuid in [
Uuid::from_u128(0x00001234_0000_1000_8000_00805f9b34fb),
Uuid::from_u128(0xabcd1234_0000_1000_8000_00805f9b34fb),
Uuid::from_u128(0x12345678_0000_1111_2222_333344445555),
] {
assert_eq!(cbuuid_to_uuid(&*uuid_to_cbuuid(uuid)), uuid);
}
}
}