moq_transport/setup/
version.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
use crate::coding::{Decode, DecodeError, Encode, EncodeError};

use std::ops::Deref;

/// A version number negotiated during the setup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version(pub 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-ietf-moq-transport-05.html
	pub const DRAFT_05: Version = Version(0xff000005);
}

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) -> Result<(), EncodeError> {
		self.0.encode(w)?;
		Ok(())
	}
}

/// A list of versions in arbitrary order.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
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) -> Result<(), EncodeError> {
		self.0.len().encode(w)?;

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

		Ok(())
	}
}

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())
	}
}