moqtap_client/draft14/session/
setup.rs1use moqtap_codec::draft14::message::{ClientSetup, ServerSetup};
2use moqtap_codec::varint::VarInt;
3
4#[derive(Debug, thiserror::Error, PartialEq, Eq)]
6pub enum SetupError {
7 #[error("no common version between client and server")]
9 NoCommonVersion,
10 #[error("missing required parameter: {0}")]
12 MissingParameter(
13 &'static str,
15 ),
16 #[error("client sent SERVER_SETUP-only parameter")]
18 WrongParameterRole,
19 #[error("no versions offered")]
21 EmptyVersionList,
22}
23
24pub fn validate_client_setup(msg: &ClientSetup) -> Result<(), SetupError> {
26 if msg.supported_versions.is_empty() {
27 return Err(SetupError::EmptyVersionList);
28 }
29 for param in &msg.parameters {
31 if param.key == VarInt::from_u64(0x02).unwrap() {
32 return Err(SetupError::WrongParameterRole);
33 }
34 }
35 Ok(())
36}
37
38pub fn validate_server_setup(_msg: &ServerSetup) -> Result<(), SetupError> {
40 Ok(())
41}
42
43pub fn negotiate_version(
45 client_versions: &[VarInt],
46 server_version: VarInt,
47) -> Result<VarInt, SetupError> {
48 if client_versions.contains(&server_version) {
49 Ok(server_version)
50 } else {
51 Err(SetupError::NoCommonVersion)
52 }
53}