moqtap_client/draft12/session/
setup.rs1use moqtap_codec::draft12::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> {
29 if msg.supported_versions.is_empty() {
30 return Err(SetupError::EmptyVersionList);
31 }
32 for param in &msg.parameters {
33 if param.key == VarInt::from_u64(0x02).unwrap() {
34 return Err(SetupError::WrongParameterRole);
35 }
36 }
37 Ok(())
38}
39
40pub fn validate_server_setup(_msg: &ServerSetup) -> Result<(), SetupError> {
42 Ok(())
43}
44
45pub fn negotiate_version(
47 client_versions: &[VarInt],
48 server_version: VarInt,
49) -> Result<VarInt, SetupError> {
50 if client_versions.contains(&server_version) {
51 Ok(server_version)
52 } else {
53 Err(SetupError::NoCommonVersion)
54 }
55}