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