moq_transfork/message/
versions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::coding::*;

use std::{fmt, ops::Deref};

/// A version number negotiated during the setup.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version(u64);

impl Version {
	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-00.html>
	pub const DRAFT_00: Version = Version(0xff000000);

	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-01.html>
	pub const DRAFT_01: Version = Version(0xff000001);

	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-02.html>
	pub const DRAFT_02: Version = Version(0xff000002);

	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-03.html>
	pub const DRAFT_03: Version = Version(0xff000003);

	/// <https://www.ietf.org/archive/id/draft-ietf-moq-transport-04.html>
	pub const DRAFT_04: Version = Version(0xff000004);

	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-00.html>
	pub const FORK_00: Version = Version(0xff0bad00);

	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-01.html>
	pub const FORK_01: Version = Version(0xff0bad01);

	/// <https://www.ietf.org/archive/id/draft-lcurley-moq-transfork-02.html>
	pub const FORK_02: Version = Version(0xff0bad02);

	pub const CURRENT: Version = Version::FORK_02;
}

impl From<u64> for Version {
	fn from(v: u64) -> Self {
		Self(v)
	}
}

impl From<Version> for u64 {
	fn from(v: Version) -> Self {
		v.0
	}
}

impl Decode for Version {
	/// Decode the version number.
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		let v = u64::decode(r)?;
		Ok(Self(v))
	}
}

impl Encode for Version {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
		self.0.encode(w);
	}
}

impl fmt::Debug for Version {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.0.fmt(f)
	}
}

/// A list of versions in arbitrary order.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Versions(Vec<Version>);

impl Decode for Versions {
	/// Decode the version list.
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		let count = u64::decode(r)?;
		let mut vs = Vec::new();

		for _ in 0..count {
			let v = Version::decode(r)?;
			vs.push(v);
		}

		Ok(Self(vs))
	}
}

impl Encode for Versions {
	/// Encode the version list.
	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
		self.0.len().encode(w);

		for v in &self.0 {
			v.encode(w);
		}
	}
}

impl Deref for Versions {
	type Target = Vec<Version>;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl From<Vec<Version>> for Versions {
	fn from(vs: Vec<Version>) -> Self {
		Self(vs)
	}
}

impl<const N: usize> From<[Version; N]> for Versions {
	fn from(vs: [Version; N]) -> Self {
		Self(vs.to_vec())
	}
}

impl fmt::Debug for Versions {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_list().entries(self.0.iter()).finish()
	}
}