1use std::fmt;
2use std::str::FromStr;
3
4use crate::{coding, ietf, lite};
5
6#[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))]
13pub(crate) const NEGOTIATED: [Version; 7] = [
14 Version::lite(lite::Version::Lite07),
15 Version::lite(lite::Version::Lite06),
16 Version::lite(lite::Version::Lite05),
17 Version::lite(lite::Version::Lite04),
18 Version::lite(lite::Version::Lite03),
19 Version::lite(lite::Version::Lite02),
20 Version::lite(lite::Version::Lite01),
21];
22
23pub const ALPNS: &[&str] = &[
32 ALPN_LITE_06,
33 ALPN_LITE_05,
34 ALPN_LITE_04,
35 ALPN_LITE_03,
36 ALPN_LITE,
37 ALPN_22,
38 ALPN_20,
39 ALPN_18,
40];
41
42pub(crate) const ALPN_LITE: &str = "moql";
44pub(crate) const ALPN_LITE_03: &str = "moq-lite-03";
45pub(crate) const ALPN_LITE_04: &str = "moq-lite-04";
46pub(crate) const ALPN_LITE_05: &str = "moq-lite-05";
47pub(crate) const ALPN_LITE_06: &str = "moq-lite-06";
48pub(crate) const ALPN_LITE_07_WIP: &str = "moq-lite-07-wip";
49pub(crate) const ALPN_18: &str = "moqt-18";
50pub(crate) const ALPN_20: &str = "moqt-20";
51pub(crate) const ALPN_22: &str = "moqt-22";
52pub(crate) const ALPN_23: &str = "moqt-23";
53
54const ALL: [Version; 11] = [
55 Version::lite(lite::Version::Lite07),
56 Version::lite(lite::Version::Lite06),
57 Version::lite(lite::Version::Lite05),
58 Version::lite(lite::Version::Lite04),
59 Version::lite(lite::Version::Lite03),
60 Version::lite(lite::Version::Lite02),
61 Version::lite(lite::Version::Lite01),
62 Version::ietf(ietf::Version::Draft23),
63 Version::ietf(ietf::Version::Draft22),
64 Version::ietf(ietf::Version::Draft20),
65 Version::ietf(ietf::Version::Draft18),
66];
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70pub(crate) enum Draft {
71 Lite(lite::Version),
73 Ietf(ietf::Version),
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub struct Version(pub(crate) Draft);
83
84impl Version {
85 pub(crate) const fn lite(version: lite::Version) -> Self {
86 Self(Draft::Lite(version))
87 }
88
89 pub(crate) const fn ietf(version: ietf::Version) -> Self {
90 Self(Draft::Ietf(version))
91 }
92
93 pub const ALL: &'static [Version] = &ALL;
96
97 #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))]
98 pub(crate) fn session_code(self, error: &crate::SessionError) -> u32 {
99 match self.0 {
100 Draft::Lite(_) => crate::lite::error::to_session_code(error),
101 Draft::Ietf(_) => crate::ietf::error::to_session_code(error),
102 }
103 }
104
105 pub(crate) fn has_setup_token(self) -> bool {
109 match self.0 {
110 Draft::Lite(version) => version.has_auth(),
111 Draft::Ietf(_) => true,
112 }
113 }
114
115 pub fn has_request_path(&self) -> bool {
119 match self.0 {
120 Draft::Lite(version) => version.has_setup_stream(),
121 Draft::Ietf(_) => true,
122 }
123 }
124
125 pub fn as_str(&self) -> &'static str {
127 match self.0 {
128 Draft::Lite(lite::Version::Lite01) => "moq-lite-01",
129 Draft::Lite(lite::Version::Lite02) => "moq-lite-02",
130 Draft::Lite(lite::Version::Lite03) => "moq-lite-03",
131 Draft::Lite(lite::Version::Lite04) => "moq-lite-04",
132 Draft::Lite(lite::Version::Lite05) => "moq-lite-05",
133 Draft::Lite(lite::Version::Lite06) => "moq-lite-06",
134 Draft::Lite(lite::Version::Lite07) => "moq-lite-07-wip",
135 Draft::Ietf(ietf::Version::Draft18) => "moq-transport-18",
136 Draft::Ietf(ietf::Version::Draft20) => "moq-transport-20",
137 Draft::Ietf(ietf::Version::Draft22) => "moq-transport-22",
138 Draft::Ietf(ietf::Version::Draft23) => "moq-transport-23",
139 }
140 }
141
142 pub(crate) fn from_code(code: u64) -> Option<Self> {
144 match code {
145 0xff0dad01 => Some(Self::lite(lite::Version::Lite01)),
146 0xff0dad02 => Some(Self::lite(lite::Version::Lite02)),
147 0xff0dad03 => Some(Self::lite(lite::Version::Lite03)),
148 0xff0dad04 => Some(Self::lite(lite::Version::Lite04)),
149 0xff0dad05 => Some(Self::lite(lite::Version::Lite05)),
150 0xff0dad06 => Some(Self::lite(lite::Version::Lite06)),
151 0xff0dad07 => Some(Self::lite(lite::Version::Lite07)),
152 0xff000012 => Some(Self::ietf(ietf::Version::Draft18)),
153 0xff000014 => Some(Self::ietf(ietf::Version::Draft20)),
154 0xff000016 => Some(Self::ietf(ietf::Version::Draft22)),
155 0xff000017 => Some(Self::ietf(ietf::Version::Draft23)),
156 _ => None,
157 }
158 }
159
160 pub(crate) fn code(&self) -> u64 {
162 match self.0 {
163 Draft::Lite(lite::Version::Lite01) => 0xff0dad01,
164 Draft::Lite(lite::Version::Lite02) => 0xff0dad02,
165 Draft::Lite(lite::Version::Lite03) => 0xff0dad03,
166 Draft::Lite(lite::Version::Lite04) => 0xff0dad04,
167 Draft::Lite(lite::Version::Lite05) => 0xff0dad05,
168 Draft::Lite(lite::Version::Lite06) => 0xff0dad06,
169 Draft::Lite(lite::Version::Lite07) => 0xff0dad07,
170 Draft::Ietf(ietf::Version::Draft18) => 0xff000012,
171 Draft::Ietf(ietf::Version::Draft20) => 0xff000014,
172 Draft::Ietf(ietf::Version::Draft22) => 0xff000016,
173 Draft::Ietf(ietf::Version::Draft23) => 0xff000017,
174 }
175 }
176
177 pub fn alpn(&self) -> &'static str {
179 match self.0 {
180 Draft::Lite(lite::Version::Lite07) => ALPN_LITE_07_WIP,
181 Draft::Lite(lite::Version::Lite06) => ALPN_LITE_06,
182 Draft::Lite(lite::Version::Lite05) => ALPN_LITE_05,
183 Draft::Lite(lite::Version::Lite04) => ALPN_LITE_04,
184 Draft::Lite(lite::Version::Lite03) => ALPN_LITE_03,
185 Draft::Lite(lite::Version::Lite01 | lite::Version::Lite02) => ALPN_LITE,
186 Draft::Ietf(ietf::Version::Draft18) => ALPN_18,
187 Draft::Ietf(ietf::Version::Draft20) => ALPN_20,
188 Draft::Ietf(ietf::Version::Draft22) => ALPN_22,
189 Draft::Ietf(ietf::Version::Draft23) => ALPN_23,
190 }
191 }
192
193 #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))]
196 pub(crate) fn hands_off(self) -> bool {
197 matches!(self.0, Draft::Lite(v) if !matches!(v, lite::Version::Lite01 | lite::Version::Lite02))
198 }
199
200 pub fn is_lite(&self) -> bool {
202 match self.0 {
203 Draft::Lite(_) => true,
204 Draft::Ietf(_) => false,
205 }
206 }
207}
208
209impl fmt::Display for Version {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211 f.write_str(self.as_str())
212 }
213}
214
215impl FromStr for Version {
216 type Err = String;
217
218 fn from_str(s: &str) -> Result<Self, Self::Err> {
219 ALL.iter()
220 .find(|version| version.as_str() == s)
221 .copied()
222 .ok_or_else(|| format!("unknown version: {s}"))
223 }
224}
225
226impl serde::Serialize for Version {
227 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
228 serializer.serialize_str(&self.to_string())
229 }
230}
231
232impl<'de> serde::Deserialize<'de> for Version {
233 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
234 let s = String::deserialize(deserializer)?;
235 s.parse().map_err(serde::de::Error::custom)
236 }
237}
238
239impl TryFrom<coding::Version> for Version {
240 type Error = ();
241
242 fn try_from(value: coding::Version) -> Result<Self, Self::Error> {
243 Self::from_code(value.0).ok_or(())
244 }
245}
246
247impl<'a> coding::Decode<'a, Version> for Version {
248 fn decode(r: &mut coding::Decoder<'_>, version: Version) -> Result<Self, coding::DecodeError> {
249 coding::Version::decode(r, version).and_then(|v| v.try_into().map_err(|_| coding::DecodeError::InvalidValue))
250 }
251}
252
253impl coding::Encode<Version> for Version {
254 fn encode(&self, w: &mut coding::Encoder<'_>, v: Version) -> Result<(), coding::EncodeError> {
255 coding::Version::from(*self).encode(w, v)
256 }
257}
258
259impl From<Version> for coding::Version {
260 fn from(value: Version) -> Self {
261 Self(value.code())
262 }
263}
264
265impl From<Vec<Version>> for coding::Versions {
266 fn from(value: Vec<Version>) -> Self {
267 let inner: Vec<coding::Version> = value.into_iter().map(|v| v.into()).collect();
268 coding::Versions::from(inner)
269 }
270}
271
272#[derive(Debug, Clone)]
274pub struct Versions(Vec<Version>);
275
276impl Versions {
277 pub fn all() -> Self {
284 Self(
285 ALL.iter()
286 .filter(|version| {
287 !matches!(
288 version.0,
289 Draft::Lite(lite::Version::Lite07) | Draft::Ietf(ietf::Version::Draft23)
290 )
291 })
292 .copied()
293 .collect(),
294 )
295 }
296
297 pub fn alpns(&self) -> Vec<&'static str> {
299 let mut alpns = Vec::new();
300 for v in &self.0 {
301 let alpn = v.alpn();
302 if !alpns.contains(&alpn) {
303 alpns.push(alpn);
304 }
305 }
306 alpns
307 }
308
309 pub fn filter(&self, other: &Versions) -> Option<Versions> {
311 let filtered: Vec<Version> = self.0.iter().filter(|v| other.0.contains(v)).copied().collect();
312 if filtered.is_empty() {
313 None
314 } else {
315 Some(Versions(filtered))
316 }
317 }
318
319 pub fn select(&self, version: Version) -> Option<Version> {
321 self.0.contains(&version).then_some(version)
322 }
323
324 pub fn contains(&self, version: &Version) -> bool {
326 self.0.contains(version)
327 }
328
329 pub fn iter(&self) -> impl Iterator<Item = &Version> {
331 self.0.iter()
332 }
333}
334
335impl Default for Versions {
336 fn default() -> Self {
337 Self::all()
338 }
339}
340
341impl From<Version> for Versions {
342 fn from(value: Version) -> Self {
343 Self(vec![value])
344 }
345}
346
347impl From<Vec<Version>> for Versions {
348 fn from(value: Vec<Version>) -> Self {
349 Self(value)
350 }
351}
352
353impl<const N: usize> From<[Version; N]> for Versions {
354 fn from(value: [Version; N]) -> Self {
355 Self(value.to_vec())
356 }
357}
358
359impl From<Versions> for coding::Versions {
360 fn from(value: Versions) -> Self {
361 let inner: Vec<coding::Version> = value.0.into_iter().map(|v| v.into()).collect();
362 coding::Versions::from(inner)
363 }
364}
365
366#[cfg(test)]
367mod tests {
368 use super::*;
369
370 #[test]
371 fn default_versions_prefer_lite_06() {
372 let newest = Version::lite(lite::Version::Lite06);
373 assert_eq!(newest.alpn(), "moq-lite-06");
374 assert!("moq-lite-06-wip".parse::<Version>().is_err());
375 assert_eq!(Versions::all().iter().next(), Some(&newest));
376 assert_eq!(Versions::all().alpns().first(), Some(&newest.alpn()));
377 assert_eq!(ALPNS.first(), Some(&newest.alpn()));
378 }
379
380 #[test]
381 fn lite_07_wip_is_opt_in_only() {
382 let wip = Version::lite(lite::Version::Lite07);
383 assert_eq!(wip.alpn(), "moq-lite-07-wip");
384 assert_eq!(wip.to_string(), "moq-lite-07-wip");
385 assert_eq!("moq-lite-07-wip".parse::<Version>(), Ok(wip));
386 assert!("moq-lite-07".parse::<Version>().is_err());
387
388 assert!(!Versions::all().contains(&wip));
389 assert!(!ALPNS.contains(&wip.alpn()));
390 assert_eq!(Versions::from(wip).alpns(), [wip.alpn()]);
391 }
392}