pub mod b64;
pub mod device;
pub mod names;
pub mod reasons;
pub mod tls;
pub mod wire;
use std::borrow::Cow;
use std::fmt;
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::de::{self, Deserializer};
use serde::{Deserialize, Serialize, Serializer};
use crate::id::Id;
pub use b64::{B64, B64Error};
pub use device::*;
pub use names::{CallId, JoinIdHex, LkIdentity, NameError};
pub use wire::*;
pub const RECORDING_DECLINE_JOINS_UNRECORDED: bool = true;
pub const RECORDING_CONSENT_REQUIRED: &str = "recording_consent_required";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CallScope {
channel_id: Id,
}
pub const SCOPE_MAX_BYTES: usize = 64;
impl CallScope {
pub fn call(channel_id: Id) -> CallScope {
CallScope { channel_id }
}
pub fn channel_id(self) -> Id {
self.channel_id
}
pub fn to_bytes(self) -> Vec<u8> {
self.to_string().into_bytes()
}
pub fn parse(bytes: &[u8]) -> Option<CallScope> {
if bytes.is_empty() || bytes.len() > SCOPE_MAX_BYTES {
return None;
}
let s = std::str::from_utf8(bytes).ok()?;
let digits = s.strip_prefix("call:")?;
if digits.is_empty()
|| digits.starts_with('0')
|| !digits.bytes().all(|b| b.is_ascii_digit())
{
return None;
}
let id: Id = digits.parse().ok()?;
(id.to_string() == digits).then_some(CallScope { channel_id: id })
}
}
impl fmt::Display for CallScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "call:{}", self.channel_id)
}
}
impl Serialize for CallScope {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> Deserialize<'de> for CallScope {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<CallScope, D::Error> {
let s = String::deserialize(d)?;
CallScope::parse(s.as_bytes())
.ok_or_else(|| de::Error::custom("not a call scope (call:<decimal channel id>)"))
}
}
impl JsonSchema for CallScope {
fn inline_schema() -> bool {
true
}
fn schema_name() -> Cow<'static, str> {
"CallScope".into()
}
fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"pattern": "^call:[1-9][0-9]*$",
"description": "What a KeyPackage may join: `call:<channel_id>` (decimal)."
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scope_round_trip_and_refusals() {
let id: Id = match "1839000000000000001".parse() {
Ok(i) => i,
Err(_) => return,
};
let s = CallScope::call(id);
assert_eq!(s.to_string(), "call:1839000000000000001");
assert_eq!(CallScope::parse(&s.to_bytes()), Some(s));
for bad in [
&b""[..],
b"call:",
b"voice:1839000000000000001",
b"call:01",
b"call:-1",
b"call:1x",
b"CALL:1",
] {
assert_eq!(CallScope::parse(bad), None, "{bad:?}");
}
}
}