#[doc(hidden)]
pub mod backend;
pub mod codec;
pub mod events;
pub mod exec;
pub mod host;
pub use events::{set_sink, Event};
pub use exec::{oneshot, spawn, Oneshot, Sender, TaskId};
pub use host::{call, call_empty, call_str, Answer, HostError, Op};
pub const SHIM_JS: &str = include_str!("../js/webbluetooth.js");
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_shim_is_present_and_implements_the_boundary() {
assert!(
SHIM_JS.len() > 1_000,
"the shim looks truncated: {} bytes",
SHIM_JS.len()
);
for expected in [
"wbt_call",
"wbt_settle",
"wbt_event",
"wbt_alloc",
"wbt_free",
"navigator.bluetooth",
] {
assert!(
SHIM_JS.contains(expected),
"the shim does not mention {expected:?}"
);
}
}
const OPS: &[(Op, &str)] = &[
(Op::Availability, "AVAILABILITY"),
(Op::RequestDevice, "REQUEST_DEVICE"),
(Op::GetDevices, "GET_DEVICES"),
(Op::Forget, "FORGET"),
(Op::Connect, "CONNECT"),
(Op::Disconnect, "DISCONNECT"),
(Op::DiscoverServices, "DISCOVER_SERVICES"),
(Op::DiscoverIncludedServices, "DISCOVER_INCLUDED_SERVICES"),
(Op::DiscoverCharacteristics, "DISCOVER_CHARACTERISTICS"),
(Op::DiscoverDescriptors, "DISCOVER_DESCRIPTORS"),
(Op::ReadCharacteristic, "READ_CHARACTERISTIC"),
(Op::WriteCharacteristic, "WRITE_CHARACTERISTIC"),
(Op::ReadDescriptor, "READ_DESCRIPTOR"),
(Op::WriteDescriptor, "WRITE_DESCRIPTOR"),
(Op::SetNotify, "SET_NOTIFY"),
(Op::WatchAdvertisements, "WATCH_ADVERTISEMENTS"),
(Op::UnwatchAdvertisements, "UNWATCH_ADVERTISEMENTS"),
(Op::SetTimeout, "SET_TIMEOUT"),
];
#[test]
fn the_shim_and_this_side_agree_on_every_op_code() {
for (op, name) in OPS {
let expected = format!("{name}: {},", *op as u32);
assert!(
SHIM_JS.contains(&expected),
"the shim should define `{expected}` for {op:?}, and does not"
);
}
}
#[test]
fn the_shim_handles_every_operation() {
for (op, name) in OPS {
assert!(
SHIM_JS.contains(&format!("case OPS.{name}:")),
"the shim has no case for {op:?}"
);
}
}
#[test]
fn the_shim_and_this_side_agree_on_every_event_code() {
for (kind, name) in [
(
crate::host::EventKind::CharacteristicValue,
"CHARACTERISTIC_VALUE",
),
(
crate::host::EventKind::GattServerDisconnected,
"DISCONNECTED",
),
(crate::host::EventKind::ServiceChanged, "SERVICE_CHANGED"),
(
crate::host::EventKind::AdvertisementReceived,
"ADVERTISEMENT",
),
(
crate::host::EventKind::AvailabilityChanged,
"AVAILABILITY_CHANGED",
),
] {
let expected = format!("{name}: {},", kind as u32);
assert!(
SHIM_JS.contains(&expected),
"the shim should define `{expected}` for {kind:?}, and does not"
);
}
}
#[test]
fn the_shim_and_this_side_agree_on_every_error_code() {
for (error, name) in [
(HostError::NotFound, "NotFoundError"),
(HostError::Security, "SecurityError"),
(HostError::Network, "NetworkError"),
(HostError::InvalidState, "InvalidStateError"),
(HostError::NotSupported, "NotSupportedError"),
(HostError::InvalidModification, "InvalidModificationError"),
(HostError::Abort, "AbortError"),
(HostError::NotAllowed, "NotAllowedError"),
] {
let expected = format!("{name}: {},", error as u32);
assert!(
SHIM_JS.contains(&expected),
"the shim should map `{expected}`, and does not"
);
}
}
}