moq_lite/message/
versions.rs

1use crate::coding::*;
2
3use std::{fmt, ops::Deref};
4
5/// A version number negotiated during the setup.
6#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct Version(pub u64);
8
9impl Version {
10	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-00.html>
11	pub const DRAFT_00: Version = Version(0xff000000);
12
13	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-01.html>
14	pub const DRAFT_01: Version = Version(0xff000001);
15
16	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-02.html>
17	pub const DRAFT_02: Version = Version(0xff000002);
18
19	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-03.html>
20	pub const DRAFT_03: Version = Version(0xff000003);
21
22	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-04.html>
23	pub const DRAFT_04: Version = Version(0xff000004);
24
25	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-00.html>
26	pub const FORK_00: Version = Version(0xff0bad00);
27
28	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-01.html>
29	pub const FORK_01: Version = Version(0xff0bad01);
30
31	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-02.html>
32	pub const FORK_02: Version = Version(0xff0bad02);
33
34	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-03.html>
35	pub const FORK_03: Version = Version(0xff0bad03);
36
37	/// Unpublished: <https://kixelated.github.io/moq-drafts/draft-lcurley-moq-transfork.html>
38	pub const FORK_04: Version = Version(0xff0bad04);
39
40	pub const LITE_00: Version = Version(0xff0dad00);
41
42	pub const CURRENT: Version = Version::LITE_00;
43}
44
45/// A version number negotiated during the setup.
46#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
47pub struct Alpn(pub &'static str);
48
49impl Alpn {
50	pub const LITE_00: Alpn = Alpn("moql-00");
51	pub const CURRENT: Alpn = Alpn::LITE_00;
52}
53
54impl From<u64> for Version {
55	fn from(v: u64) -> Self {
56		Self(v)
57	}
58}
59
60impl From<Version> for u64 {
61	fn from(v: Version) -> Self {
62		v.0
63	}
64}
65
66impl Decode for Version {
67	/// Decode the version number.
68	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
69		let v = u64::decode(r)?;
70		Ok(Self(v))
71	}
72}
73
74impl Encode for Version {
75	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
76		self.0.encode(w);
77	}
78}
79
80impl fmt::Debug for Version {
81	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82		self.0.fmt(f)
83	}
84}
85
86/// A list of versions in arbitrary order.
87#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
88pub struct Versions(Vec<Version>);
89
90impl Decode for Versions {
91	/// Decode the version list.
92	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
93		let count = u64::decode(r)?;
94		let mut vs = Vec::new();
95
96		for _ in 0..count {
97			let v = Version::decode(r)?;
98			vs.push(v);
99		}
100
101		Ok(Self(vs))
102	}
103}
104
105impl Encode for Versions {
106	/// Encode the version list.
107	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
108		self.0.len().encode(w);
109
110		for v in &self.0 {
111			v.encode(w);
112		}
113	}
114}
115
116impl Deref for Versions {
117	type Target = Vec<Version>;
118
119	fn deref(&self) -> &Self::Target {
120		&self.0
121	}
122}
123
124impl From<Vec<Version>> for Versions {
125	fn from(vs: Vec<Version>) -> Self {
126		Self(vs)
127	}
128}
129
130impl<const N: usize> From<[Version; N]> for Versions {
131	fn from(vs: [Version; N]) -> Self {
132		Self(vs.to_vec())
133	}
134}
135
136impl fmt::Debug for Versions {
137	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138		f.debug_list().entries(self.0.iter()).finish()
139	}
140}