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	pub const LITE_01: Version = Version(0xff0dad01);
42
43	pub const CURRENT: Version = Version::LITE_01;
44}
45
46/// A version number negotiated during the setup.
47#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
48pub struct Alpn(pub &'static str);
49
50impl Alpn {
51	pub const LITE_00: Alpn = Alpn("moql-00");
52	pub const LITE_01: Alpn = Alpn("moql-01");
53	pub const CURRENT: Alpn = Alpn::LITE_01;
54}
55
56impl From<u64> for Version {
57	fn from(v: u64) -> Self {
58		Self(v)
59	}
60}
61
62impl From<Version> for u64 {
63	fn from(v: Version) -> Self {
64		v.0
65	}
66}
67
68impl Decode for Version {
69	/// Decode the version number.
70	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
71		let v = u64::decode(r)?;
72		Ok(Self(v))
73	}
74}
75
76impl Encode for Version {
77	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
78		self.0.encode(w);
79	}
80}
81
82impl fmt::Debug for Version {
83	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84		self.0.fmt(f)
85	}
86}
87
88/// A list of versions in arbitrary order.
89#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
90pub struct Versions(Vec<Version>);
91
92impl Decode for Versions {
93	/// Decode the version list.
94	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
95		let count = u64::decode(r)?;
96		let mut vs = Vec::new();
97
98		for _ in 0..count {
99			let v = Version::decode(r)?;
100			vs.push(v);
101		}
102
103		Ok(Self(vs))
104	}
105}
106
107impl Encode for Versions {
108	/// Encode the version list.
109	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
110		self.0.len().encode(w);
111
112		for v in &self.0 {
113			v.encode(w);
114		}
115	}
116}
117
118impl Deref for Versions {
119	type Target = Vec<Version>;
120
121	fn deref(&self) -> &Self::Target {
122		&self.0
123	}
124}
125
126impl From<Vec<Version>> for Versions {
127	fn from(vs: Vec<Version>) -> Self {
128		Self(vs)
129	}
130}
131
132impl<const N: usize> From<[Version; N]> for Versions {
133	fn from(vs: [Version; N]) -> Self {
134		Self(vs.to_vec())
135	}
136}
137
138impl fmt::Debug for Versions {
139	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140		f.debug_list().entries(self.0.iter()).finish()
141	}
142}