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