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