diff options
author | Zakor Gyula <zakorgy@inf.u-szeged.hu> | 2016-11-21 16:07:59 +0100 |
---|---|---|
committer | Zakor Gyula <zakorgy@inf.u-szeged.hu> | 2016-12-01 11:15:52 +0100 |
commit | 7e2a01210ff00c538927aae9218566f48e768993 (patch) | |
tree | d09a23560f0d367e5d061f534d155cfa80ff7fe5 /components/script/dom/bluetoothuuid.rs | |
parent | 61a225bab0d82dd9a4e3b1cec910e78a02cf875a (diff) | |
download | servo-7e2a01210ff00c538927aae9218566f48e768993.tar.gz servo-7e2a01210ff00c538927aae9218566f48e768993.zip |
BluetoothUUID refactor
Diffstat (limited to 'components/script/dom/bluetoothuuid.rs')
-rw-r--r-- | components/script/dom/bluetoothuuid.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/components/script/dom/bluetoothuuid.rs b/components/script/dom/bluetoothuuid.rs index 11fb7af94cb..d3c2a983aa4 100644 --- a/components/script/dom/bluetoothuuid.rs +++ b/components/script/dom/bluetoothuuid.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; -use dom::bindings::error::Error::Syntax; +use dom::bindings::error::Error::Type; use dom::bindings::error::Fallible; use dom::bindings::reflector::Reflector; use dom::bindings::str::DOMString; @@ -268,6 +268,18 @@ const SERVICE_PREFIX: &'static str = "org.bluetooth.service"; const CHARACTERISTIC_PREFIX: &'static str = "org.bluetooth.characteristic"; const DESCRIPTOR_PREFIX: &'static str = "org.bluetooth.descriptor"; const VALID_UUID_REGEX: &'static str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; +// https://cs.chromium.org/chromium/src/third_party/WebKit/Source/modules/bluetooth/BluetoothUUID.cpp?l=314 +const UUID_ERROR_MESSAGE: &'static str = "It must be a valid UUID alias (e.g. 0x1234), \ + UUID (lowercase hex characters e.g. '00001234-0000-1000-8000-00805f9b34fb'),\nor recognized standard name from"; +// https://cs.chromium.org/chromium/src/third_party/WebKit/Source/modules/bluetooth/BluetoothUUID.cpp?l=321 +const SERVICES_ERROR_MESSAGE: &'static str = "https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx\ + \ne.g. 'alert_notification'."; +// https://cs.chromium.org/chromium/src/third_party/WebKit/Source/modules/bluetooth/BluetoothUUID.cpp?l=327 +const CHARACTERISTIC_ERROR_MESSAGE: &'static str = "https://developer.bluetooth.org/gatt/characteristics/Pages/\ + CharacteristicsHome.aspx\ne.g. 'aerobic_heart_rate_lower_limit'."; +// https://cs.chromium.org/chromium/src/third_party/WebKit/Source/modules/bluetooth/BluetoothUUID.cpp?l=333 +const DESCRIPTOR_ERROR_MESSAGE: &'static str = "https://developer.bluetooth.org/gatt/descriptors/Pages/\ + DescriptorsHomePage.aspx\ne.g. 'gatt.characteristic_presentation_format'."; impl BluetoothUUID { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-canonicaluuid @@ -325,22 +337,35 @@ fn resolve_uuid_name( prefix: &str) -> Fallible<DOMString> { match name { - // Step 1 + // Step 1. StringOrUnsignedLong::UnsignedLong(unsigned32) => { Ok(canonical_uuid(unsigned32)) }, StringOrUnsignedLong::String(dstring) => { - // Step 2 + // Step 2. let regex = Regex::new(VALID_UUID_REGEX).unwrap(); if regex.is_match(&*dstring) { Ok(dstring) } else { - // Step 3 + // Step 3. let concatenated = format!("{}.{}", prefix, dstring); let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated); match is_in_table { Some(&(_, alias)) => Ok(canonical_uuid(alias)), - None => Err(Syntax), + None => { + let (attribute_type, error_url_message) = match prefix { + SERVICE_PREFIX => ("Service", SERVICES_ERROR_MESSAGE), + CHARACTERISTIC_PREFIX => ("Characteristic", CHARACTERISTIC_ERROR_MESSAGE), + DESCRIPTOR_PREFIX => ("Descriptor", DESCRIPTOR_ERROR_MESSAGE), + _ => unreachable!(), + }; + // Step 4. + return Err(Type(format!("Invalid {} name : '{}'.\n{} {}", + attribute_type, + dstring, + UUID_ERROR_MESSAGE, + error_url_message))); + }, } } }, |