diff --git a/components/net/bluetooth_thread.rs b/components/net/bluetooth_thread.rs index b298e78acab..82fe4891ee9 100644 --- a/components/net/bluetooth_thread.rs +++ b/components/net/bluetooth_thread.rs @@ -15,6 +15,14 @@ use std::collections::HashMap; use std::string::String; use util::thread::spawn_named; +const ADAPTER_ERROR: &'static str = "No adapter found"; +const DEVICE_ERROR: &'static str = "No device found"; +const DEVICE_MATCH_ERROR: &'static str = "No device found, that matches the given options"; +const PRIMARY_SERVICE_ERROR: &'static str = "No primary service found"; +const CHARACTERISTIC_ERROR: &'static str = "No characteristic found"; +const DESCRIPTOR_ERROR: &'static str = "No descriptor found"; +const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id"; + macro_rules! send_error( ($sender:expr, $error:expr) => ( return $sender.send(BluetoothObjectMsg::Error { error: String::from($error) }).unwrap(); @@ -79,40 +87,40 @@ impl BluetoothManager { match self.receiver.recv().unwrap() { BluetoothMethodMsg::RequestDevice(options, sender) => { self.request_device(options, sender) - } + }, BluetoothMethodMsg::GATTServerConnect(device_id, sender) => { self.gatt_server_connect(device_id, sender) - } + }, BluetoothMethodMsg::GATTServerDisconnect(device_id, sender) => { self.gatt_server_disconnect(device_id, sender) - } + }, BluetoothMethodMsg::GetPrimaryService(device_id, uuid, sender) => { self.get_primary_service(device_id, uuid, sender) - } + }, BluetoothMethodMsg::GetPrimaryServices(device_id, uuid, sender) => { self.get_primary_services(device_id, uuid, sender) - } + }, BluetoothMethodMsg::GetCharacteristic(service_id, uuid, sender) => { self.get_characteristic(service_id, uuid, sender) - } + }, BluetoothMethodMsg::GetCharacteristics(service_id, uuid, sender) => { self.get_characteristics(service_id, uuid, sender) - } + }, BluetoothMethodMsg::GetDescriptor(characteristic_id, uuid, sender) => { self.get_descriptor(characteristic_id, uuid, sender) - } + }, BluetoothMethodMsg::GetDescriptors(characteristic_id, uuid, sender) => { self.get_descriptors(characteristic_id, uuid, sender) - } + }, BluetoothMethodMsg::ReadValue(id, sender) => { self.read_value(id, sender) - } + }, BluetoothMethodMsg::WriteValue(id, value, sender) => { self.write_value(id, value, sender) - } + }, BluetoothMethodMsg::Exit => { break - } + }, } } } @@ -132,7 +140,9 @@ impl BluetoothManager { fn get_devices(&mut self, adapter: &mut BluetoothAdapter) -> Vec { let devices = adapter.get_devices().unwrap_or(vec!()); for device in &devices { - self.cached_devices.insert(device.get_address().unwrap_or("".to_owned()), device.clone()); + if let Ok(address) = device.get_address() { + self.cached_devices.insert(address, device.clone()); + } } devices } @@ -159,10 +169,7 @@ impl BluetoothManager { services } - fn get_gatt_service(&mut self, - adapter: &mut BluetoothAdapter, - service_id: &str) - -> Option<&BluetoothGATTService> { + fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> { check_cache!(self.cached_services, service_id); let device_id = match self.service_to_device.get_mut(service_id) { Some(d) => d.clone(), @@ -182,8 +189,10 @@ impl BluetoothManager { let mut services_vec: Vec = vec!(); let services = self.get_gatt_services(adapter, device_id); for service in services { - if service.get_uuid().unwrap_or("".to_owned()) == service_uuid { - services_vec.push(service.clone()); + if let Ok(uuid) = service.get_uuid() { + if uuid == service_uuid { + services_vec.push(service.clone()); + } } } services_vec @@ -230,8 +239,10 @@ impl BluetoothManager { let mut characteristics_vec: Vec = vec!(); let characteristics = self.get_gatt_characteristics(adapter, service_id); for characteristic in characteristics { - if characteristic.get_uuid().unwrap_or("".to_owned()) == characteristic_uuid { - characteristics_vec.push(characteristic.clone()); + if let Ok(uuid) = characteristic.get_uuid() { + if uuid == characteristic_uuid { + characteristics_vec.push(characteristic.clone()); + } } } characteristics_vec @@ -298,8 +309,10 @@ impl BluetoothManager { let mut descriptors_vec: Vec = vec!(); let descriptors = self.get_gatt_descriptors(adapter, characteristic_id); for descriptor in descriptors { - if descriptor.get_uuid().unwrap_or("".to_owned()) == descriptor_uuid { - descriptors_vec.push(descriptor.clone()); + if let Ok(uuid) = descriptor.get_uuid() { + if uuid == descriptor_uuid { + descriptors_vec.push(descriptor.clone()); + } } } descriptors_vec @@ -307,16 +320,14 @@ impl BluetoothManager { // Methods - fn request_device(&mut self, - options: RequestDeviceoptions, - sender: IpcSender) { + fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let devices = self.get_devices(&mut adapter); if devices.is_empty() { - send_error!(sender, "No device found"); + send_error!(sender, DEVICE_ERROR); } let matched_devices: Vec = devices.into_iter() @@ -345,13 +356,13 @@ impl BluetoothManager { return sender.send(message).unwrap(); } } - send_error!(sender, "No device found, that matches the given options"); + send_error!(sender, DEVICE_MATCH_ERROR); } pub fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let connected = match self.get_device(&mut adapter, &device_id) { @@ -361,8 +372,8 @@ impl BluetoothManager { } else { !d.connect().is_err() } - } - None => send_error!(sender, "No device found"), + }, + None => send_error!(sender, DEVICE_ERROR), }; let message = BluetoothObjectMsg::BluetoothServer { @@ -374,7 +385,7 @@ impl BluetoothManager { pub fn gatt_server_disconnect(&mut self, device_id: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let connected = match self.get_device(&mut adapter, &device_id) { @@ -384,8 +395,8 @@ impl BluetoothManager { } else { false } - } - None => send_error!(sender, "No device found"), + }, + None => send_error!(sender, DEVICE_ERROR), }; let message = BluetoothObjectMsg::BluetoothServer { @@ -397,11 +408,11 @@ impl BluetoothManager { pub fn get_primary_service(&mut self, device_id: String, uuid: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid); if services.is_empty() { - send_error!(sender, "No primary service found"); + send_error!(sender, PRIMARY_SERVICE_ERROR); } for service in services { if service.is_primary().unwrap_or(false) { @@ -415,7 +426,7 @@ impl BluetoothManager { } } } - send_error!(sender, "No primary service found"); + send_error!(sender, PRIMARY_SERVICE_ERROR); } pub fn get_primary_services(&mut self, @@ -424,27 +435,29 @@ impl BluetoothManager { sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let services: Vec = match uuid { Some(id) => self.get_gatt_services_by_uuid(&mut adapter, &device_id, &id), None => self.get_gatt_services(&mut adapter, &device_id), }; if services.is_empty() { - send_error!(sender, "No service found"); + send_error!(sender, PRIMARY_SERVICE_ERROR); } let mut services_vec: Vec = vec!(); for service in services { if service.is_primary().unwrap_or(false) { - services_vec.push(BluetoothObjectMsg::BluetoothService { - uuid: service.get_uuid().unwrap_or("".to_owned()), - is_primary: true, - instance_id: service.get_object_path(), - }); + if let Ok(uuid) = service.get_uuid() { + services_vec.push(BluetoothObjectMsg::BluetoothService { + uuid: uuid, + is_primary: true, + instance_id: service.get_object_path(), + }); + } } } if services_vec.is_empty() { - send_error!(sender, "No service found"); + send_error!(sender, PRIMARY_SERVICE_ERROR); } let message = BluetoothObjectMsg::BluetoothServices { services_vec: services_vec }; sender.send(message).unwrap(); @@ -453,11 +466,11 @@ impl BluetoothManager { pub fn get_characteristic(&mut self, service_id: String, uuid: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid); if characteristics.is_empty() { - send_error!(sender, "No characteristic found"); + send_error!(sender, CHARACTERISTIC_ERROR); } for characteristic in characteristics { if let Ok(uuid) = characteristic.get_uuid() { @@ -478,7 +491,7 @@ impl BluetoothManager { return sender.send(message).unwrap(); } } - send_error!(sender, "No characteristic found"); + send_error!(sender, CHARACTERISTIC_ERROR); } pub fn get_characteristics(&mut self, @@ -487,50 +500,49 @@ impl BluetoothManager { sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let characteristics = match uuid { Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id), None => self.get_gatt_characteristics(&mut adapter, &service_id), }; if characteristics.is_empty() { - send_error!(sender, "No characteristic found"); + send_error!(sender, CHARACTERISTIC_ERROR); } let mut characteristics_vec: Vec = vec!(); for characteristic in characteristics { - let properties = self.get_characteristic_properties(&characteristic); - characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic { - uuid: characteristic.get_uuid().unwrap_or("".to_owned()), - instance_id: characteristic.get_object_path(), - broadcast: properties[0], - read: properties[1], - write_without_response: properties[2], - write: properties[3], - notify: properties[4], - indicate: properties[5], - authenticated_signed_writes: properties[6], - reliable_write: properties[7], - writable_auxiliaries: properties[8], - }); + if let Ok(uuid) = characteristic.get_uuid() { + let properties = self.get_characteristic_properties(&characteristic); + characteristics_vec.push(BluetoothObjectMsg::BluetoothCharacteristic { + uuid: uuid, + instance_id: characteristic.get_object_path(), + broadcast: properties[0], + read: properties[1], + write_without_response: properties[2], + write: properties[3], + notify: properties[4], + indicate: properties[5], + authenticated_signed_writes: properties[6], + reliable_write: properties[7], + writable_auxiliaries: properties[8], + }); + } } if characteristics_vec.is_empty() { - send_error!(sender, "No characteristic found"); + send_error!(sender, CHARACTERISTIC_ERROR); } let message = BluetoothObjectMsg::BluetoothCharacteristics { characteristics_vec: characteristics_vec }; sender.send(message).unwrap(); } - pub fn get_descriptor(&mut self, - characteristic_id: String, - uuid: String, - sender: IpcSender) { + pub fn get_descriptor(&mut self, characteristic_id: String, uuid: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid); if descriptors.is_empty() { - send_error!(sender, "No descriptor found"); + send_error!(sender, DESCRIPTOR_ERROR); } for descriptor in descriptors { if let Ok(uuid) = descriptor.get_uuid() { @@ -541,7 +553,7 @@ impl BluetoothManager { return sender.send(message).unwrap(); } } - send_error!(sender, "No descriptor found"); + send_error!(sender, DESCRIPTOR_ERROR); } pub fn get_descriptors(&mut self, @@ -550,24 +562,26 @@ impl BluetoothManager { sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let descriptors = match uuid { Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id), None => self.get_gatt_descriptors(&mut adapter, &characteristic_id), }; if descriptors.is_empty() { - send_error!(sender, "No descriptor found"); + send_error!(sender, DESCRIPTOR_ERROR); } let mut descriptors_vec: Vec = vec!(); for descriptor in descriptors { - descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor { - uuid: descriptor.get_uuid().unwrap_or("".to_owned()), - instance_id: descriptor.get_object_path(), - }); + if let Ok(uuid) = descriptor.get_uuid() { + descriptors_vec.push(BluetoothObjectMsg::BluetoothDescriptor { + uuid: uuid, + instance_id: descriptor.get_object_path(), + }); + } } if descriptors_vec.is_empty() { - send_error!(sender, "No descriptor found"); + send_error!(sender, DESCRIPTOR_ERROR); } let message = BluetoothObjectMsg::BluetoothDescriptors { descriptors_vec: descriptors_vec }; sender.send(message).unwrap(); @@ -576,7 +590,7 @@ impl BluetoothManager { pub fn read_value(&mut self, id: String, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let mut value = match self.get_gatt_characteristic(&mut adapter, &id) { Some(c) => Some(c.read_value().unwrap_or(vec!())), @@ -591,7 +605,7 @@ impl BluetoothManager { let message = match value { Some(v) => BluetoothObjectMsg::BluetoothReadValue { value: v }, - None => send_error!(sender, "No characteristic or descriptor found with that id"), + None => send_error!(sender, VALUE_ERROR), }; sender.send(message).unwrap(); @@ -600,7 +614,7 @@ impl BluetoothManager { pub fn write_value(&mut self, id: String, value: Vec, sender: IpcSender) { let mut adapter = match self.get_adapter() { Some(a) => a, - None => send_error!(sender, "No adapter found"), + None => send_error!(sender, ADAPTER_ERROR), }; let mut result = match self.get_gatt_characteristic(&mut adapter, &id) { Some(c) => Some(c.write_value(value.clone())), @@ -618,7 +632,7 @@ impl BluetoothManager { Ok(_) => BluetoothObjectMsg::BluetoothWriteValue, Err(e) => send_error!(sender, e.to_string()), }, - None => send_error!(sender, "No characteristic or descriptor found with that id"), + None => send_error!(sender, VALUE_ERROR), }; sender.send(message).unwrap(); diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index b66a427864a..d56528d845a 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -23,13 +23,13 @@ use util::str::DOMString; // A device name can never be longer than 29 bytes. An adv packet is at most // 31 bytes long. The length and identifier of the length field take 2 bytes. -// That least 29 bytes for the name. -const MAX_FILTER_NAME_LENGTH: usize = 29; -// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name. -const MAX_DEVICE_NAME_LENGTH: usize = 248; const FILTER_EMPTY_ERROR: &'static str = "'filters' member must be non - empty to find any devices."; const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way."; const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes."; +// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name. +const MAX_DEVICE_NAME_LENGTH: usize = 248; +// That least 29 bytes for the name. +const MAX_FILTER_NAME_LENGTH: usize = 29; const NAME_PREFIX_ERROR: &'static str = "'namePrefix', if present, must be non - empty."; const NAME_TOO_LONG_ERROR: &'static str = "A device name can't be longer than 248 bytes."; const SERVICE_ERROR: &'static str = "'services', if present, must contain at least one service."; @@ -79,11 +79,8 @@ impl Clone for RequestDeviceOptions { } } -fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) - -> Fallible { - if !(filter.services.is_some() || - filter.name.is_some() || - filter.namePrefix.is_some()) { +fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) -> Fallible { + if !(filter.services.is_some() || filter.name.is_some() || filter.namePrefix.is_some()) { return Err(Type(FILTER_ERROR.to_owned())); } @@ -129,8 +126,9 @@ fn canonicalize_filter(filter: &BluetoothScanFilter, global: GlobalRef) Ok(BluetoothScanfilter::new(name, name_prefix, services_vec)) } -fn convert_request_device_options(options: &RequestDeviceOptions, global: GlobalRef) - -> Fallible { +fn convert_request_device_options(options: &RequestDeviceOptions, + global: GlobalRef) + -> Fallible { if options.filters.is_empty() { return Err(Type(FILTER_EMPTY_ERROR.to_owned())); } @@ -160,15 +158,12 @@ fn convert_request_device_options(options: &RequestDeviceOptions, global: Global impl BluetoothMethods for Bluetooth { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice - fn RequestDevice(&self, - option: &RequestDeviceOptions) - -> Fallible> { + fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible> { let (sender, receiver) = ipc::channel().unwrap(); match convert_request_device_options(option, self.global().r()) { Ok(option) => { - self.get_bluetooth_thread() - .send(BluetoothMethodMsg::RequestDevice(option, sender)) - .unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::RequestDevice(option, sender)).unwrap(); let device = receiver.recv().unwrap(); match device { BluetoothObjectMsg::BluetoothDevice { @@ -211,7 +206,9 @@ impl BluetoothMethods for Bluetooth { }, BluetoothObjectMsg::Error { error - } => return Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } }, diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 6d3bc5fe4f0..19e78e09f84 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::Bluetoot use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; use dom::bindings::error::Error::{Network, Type}; -use dom::bindings::error::Fallible; +use dom::bindings::error::{Fallible, ErrorResult}; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -61,8 +61,8 @@ impl BluetoothRemoteGATTCharacteristic { uuid, properties, instanceID), - global, - BluetoothRemoteGATTCharacteristicBinding::Wrap) + global, + BluetoothRemoteGATTCharacteristicBinding::Wrap) } fn get_bluetooth_thread(&self) -> IpcSender { @@ -94,9 +94,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor - fn GetDescriptor(&self, - descriptor: StringOrUnsignedLong) - -> Fallible> { + fn GetDescriptor(&self, descriptor: StringOrUnsignedLong) -> Fallible> { let uuid: String = match BluetoothUUID::GetDescriptor(self.global().r(), descriptor.clone()) { Ok(domstring) => domstring.to_string(), Err(error) => return Err(error), @@ -117,7 +115,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } } @@ -126,12 +126,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris fn GetDescriptors(&self, descriptor: Option) -> Fallible>> { - let uuid: Option = match descriptor { - Some(d) => match BluetoothUUID::GetDescriptor(self.global().r(), d.clone()) { - Ok(domstring) => Some(domstring.to_string()), + let mut uuid: Option = None; + if let Some(d)= descriptor { + match BluetoothUUID::GetCharacteristic(self.global().r(), d.clone()) { + Ok(domstring) => uuid = Some(domstring.to_string()), Err(error) => return Err(error), - }, - None => None, + } }; let (sender, receiver) = ipc::channel().unwrap(); let mut descriptors: Vec> = vec!(); @@ -151,7 +151,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris descriptors.push(BluetoothRemoteGATTDescriptor::new(self.global().r(), &self, DOMString::from(uuid), - instance_id)) + instance_id)); }, _ => unreachable!(), } @@ -160,7 +160,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!(), } } @@ -175,8 +177,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris let (sender, receiver) = ipc::channel().unwrap(); if !self.Service().Device().Gatt().Connected() { Err(Network) - } - else { + } else { self.get_bluetooth_thread().send( BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap(); let result = receiver.recv().unwrap(); @@ -188,7 +189,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris }, BluetoothObjectMsg::Error { error - } => return Err(Type(error)), + } => { + return Err(Type(error)) + }, _ => unreachable!() }; *self.value.borrow_mut() = value; @@ -197,7 +200,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue - fn WriteValue(&self, value: Vec) -> Fallible<()> { + fn WriteValue(&self, value: Vec) -> ErrorResult { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); @@ -206,7 +209,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } } diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 9a48ee8395f..edbc61f9e3a 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::Blue use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::error::Error::{Type, Network}; -use dom::bindings::error::Fallible; +use dom::bindings::error::{Fallible, ErrorResult}; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -90,8 +90,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { let (sender, receiver) = ipc::channel().unwrap(); if !self.Characteristic().Service().Device().Gatt().Connected() { Err(Network) - } - else { + } else { self.get_bluetooth_thread().send( BluetoothMethodMsg::ReadValue(self.get_instance_id(), sender)).unwrap(); let result = receiver.recv().unwrap(); @@ -103,7 +102,9 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { }, BluetoothObjectMsg::Error { error - } => return Err(Type(error)), + } => { + return Err(Type(error)) + }, _ => unreachable!() }; *self.value.borrow_mut() = value; @@ -112,7 +113,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue - fn WriteValue(&self, value: Vec) -> Fallible<()> { + fn WriteValue(&self, value: Vec) -> ErrorResult { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); @@ -121,7 +122,9 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } } diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index a52482f2106..a6c6f42769f 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; use dom::bindings::error::Error::Type; -use dom::bindings::error::Fallible; +use dom::bindings::error::{Fallible, ErrorResult}; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -64,9 +64,8 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect fn Connect(&self) -> Fallible> { let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread() - .send(BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)) - .unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap(); let server = receiver.recv().unwrap(); match server { BluetoothObjectMsg::BluetoothServer { @@ -77,13 +76,15 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect - fn Disconnect(&self) -> Fallible<()>{ + fn Disconnect(&self) -> ErrorResult { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); @@ -97,7 +98,9 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!() } } @@ -119,33 +122,35 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { instance_id, } => { Ok(BluetoothRemoteGATTService::new(self.global().r(), - &self.device.get(), - DOMString::from(uuid), - is_primary, - instance_id)) + &self.device.get(), + DOMString::from(uuid), + is_primary, + instance_id)) }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!(), } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices - fn GetPrimaryServices(&self, service: Option) + fn GetPrimaryServices(&self, + service: Option) -> Fallible>> { - let uuid: Option = match service { - Some(s) => match BluetoothUUID::GetService(self.global().r(), s.clone()) { - Ok(domstring) => Some(domstring.to_string()), + let mut uuid: Option = None; + if let Some(s)= service { + match BluetoothUUID::GetService(self.global().r(), s.clone()) { + Ok(domstring) => uuid = Some(domstring.to_string()), Err(error) => return Err(error), - }, - None => None, + } }; let mut services: Vec> = vec!(); let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread() - .send(BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)) - .unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); let services_vec = receiver.recv().unwrap(); match services_vec { BluetoothObjectMsg::BluetoothServices { @@ -162,7 +167,7 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { &self.device.get(), DOMString::from(uuid), is_primary, - instance_id)) + instance_id)); }, _ => unreachable!(), } @@ -171,7 +176,9 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { }, BluetoothObjectMsg::Error { error - } => Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!(), } } diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index de98f887f0c..77820521552 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -85,17 +85,16 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic - fn GetCharacteristic(&self, characteristic: StringOrUnsignedLong) + fn GetCharacteristic(&self, + characteristic: StringOrUnsignedLong) -> Fallible> { - let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), - characteristic.clone()) { + let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), characteristic.clone()) { Ok(domstring) => domstring.to_string(), Err(error) => return Err(error), }; let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread() - .send(BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)) - .unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap(); let characteristic = receiver.recv().unwrap(); match characteristic { BluetoothObjectMsg::BluetoothCharacteristic { @@ -122,33 +121,35 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { reliable_write, writable_auxiliaries); Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(), - &self, - DOMString::from(uuid), - properties, - instance_id)) + &self, + DOMString::from(uuid), + properties, + instance_id)) }, BluetoothObjectMsg::Error { error - } => return Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!(), } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics - fn GetCharacteristics(&self, characteristic: Option) + fn GetCharacteristics(&self, + characteristic: Option) -> Fallible>> { - let uuid: Option = match characteristic { - Some(c) => match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { - Ok(domstring) => Some(domstring.to_string()), + let mut uuid: Option = None; + if let Some(c)= characteristic { + match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { + Ok(domstring) => uuid = Some(domstring.to_string()), Err(error) => return Err(error), - }, - None => None, + } }; let mut characteristics: Vec> = vec!(); let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread() - .send(BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)) - .unwrap(); + self.get_bluetooth_thread().send( + BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap(); let characteristics_vec = receiver.recv().unwrap(); match characteristics_vec { BluetoothObjectMsg::BluetoothCharacteristics { @@ -169,23 +170,21 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { reliable_write, writable_auxiliaries, } => { - let properties = &BluetoothCharacteristicProperties::new( - self.global().r(), - broadcast, - read, - write_without_response, - write, - notify, - indicate, - authenticated_signed_writes, - reliable_write, - writable_auxiliaries); - characteristics.push(BluetoothRemoteGATTCharacteristic::new( - self.global().r(), - &self, - DOMString::from(uuid), - properties, - instance_id)) + let properties = &BluetoothCharacteristicProperties::new(self.global().r(), + broadcast, + read, + write_without_response, + write, + notify, + indicate, + authenticated_signed_writes, + reliable_write, + writable_auxiliaries); + characteristics.push(BluetoothRemoteGATTCharacteristic::new(self.global().r(), + &self, + DOMString::from(uuid), + properties, + instance_id)); }, _ => unreachable!(), } @@ -194,7 +193,9 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { }, BluetoothObjectMsg::Error { error - } => return Err(Type(error)), + } => { + Err(Type(error)) + }, _ => unreachable!(), } } diff --git a/components/script/dom/bluetoothuuid.rs b/components/script/dom/bluetoothuuid.rs index 91c581d5d3b..e916c6ebded 100644 --- a/components/script/dom/bluetoothuuid.rs +++ b/components/script/dom/bluetoothuuid.rs @@ -274,9 +274,7 @@ impl BluetoothUUID { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice - pub fn GetService(globalref: GlobalRef, - name: StringOrUnsignedLong) - -> Fallible { + pub fn GetService(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible { BluetoothUUID::resolve_uuid_name(globalref, name, BLUETOOTH_ASSIGNED_SERVICES, @@ -284,9 +282,7 @@ impl BluetoothUUID { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic - pub fn GetCharacteristic(globalref: GlobalRef, - name: StringOrUnsignedLong) - -> Fallible { + pub fn GetCharacteristic(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible { BluetoothUUID::resolve_uuid_name(globalref, name, BLUETOOTH_ASSIGNED_CHARCTERISTICS, @@ -294,9 +290,7 @@ impl BluetoothUUID { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor - pub fn GetDescriptor(globalref: GlobalRef, - name: StringOrUnsignedLong) - -> Fallible { + pub fn GetDescriptor(globalref: GlobalRef, name: StringOrUnsignedLong) -> Fallible { BluetoothUUID::resolve_uuid_name(globalref, name, BLUETOOTH_ASSIGNED_DESCRIPTORS, @@ -311,7 +305,7 @@ impl BluetoothUUID { -> Fallible { match name { // Step 1 - StringOrUnsignedLong::UnsignedLong(unsigned32) =>{ + StringOrUnsignedLong::UnsignedLong(unsigned32) => { Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32)) }, StringOrUnsignedLong::String(dstring) => { @@ -322,8 +316,7 @@ impl BluetoothUUID { } else { // Step 3 let concatenated = format!("{}.{}", prefix, dstring); - let is_in_table = assigned_numbers_table.iter() - .find(|p| p.0 == concatenated); + let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated); match is_in_table { Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)), None => Err(Syntax),