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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use crate::varint::VarInt;
use std::fmt;
use std::str::FromStr;

/// Stream id.
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct StreamId(VarInt);

impl StreamId {
    /// The largest stream id.
    pub const MAX: StreamId = StreamId(VarInt::MAX);

    /// New stream id.
    #[inline(always)]
    pub const fn new(varint: VarInt) -> Self {
        Self(varint)
    }

    /// Checks whether a stream is bi-directional or not.
    #[inline(always)]
    pub const fn is_bidirectional(self) -> bool {
        self.0.into_inner() & 0x2 == 0
    }

    /// Checks whether a stream is client-initiated or not.
    #[inline(always)]
    pub const fn is_client_initiated(self) -> bool {
        self.0.into_inner() & 0x1 == 0
    }

    /// Checks whether a stream is locally initiated or not.
    #[inline(always)]
    pub const fn is_local(self, is_server: bool) -> bool {
        (self.0.into_inner() & 0x1) == (is_server as u64)
    }

    /// Returns the integer value as `u64`.
    #[inline(always)]
    pub const fn into_u64(self) -> u64 {
        self.0.into_inner()
    }

    /// Returns the stream id as [`VarInt`] value.
    #[inline(always)]
    pub const fn into_varint(self) -> VarInt {
        self.0
    }
}

impl From<StreamId> for VarInt {
    #[inline(always)]
    fn from(stream_id: StreamId) -> Self {
        stream_id.0
    }
}

impl fmt::Debug for StreamId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for StreamId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Error for invalid Session ID value.
#[derive(Debug, thiserror::Error)]
#[error("invalid session ID")]
pub struct InvalidSessionId;

/// A WebTransport session id.
///
/// Internally, it corresponds to a *bidirectional* *client-initiated* QUIC stream,
/// that is, a webtransport *session stream*.
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SessionId(StreamId);

impl SessionId {
    /// Returns the integer value as `u64`.
    #[inline(always)]
    pub const fn into_u64(self) -> u64 {
        self.0.into_u64()
    }

    /// Returns the session id as [`VarInt`] value.
    #[inline(always)]
    pub const fn into_varint(self) -> VarInt {
        self.0.into_varint()
    }

    /// Returns the corresponding session QUIC stream.
    #[inline(always)]
    pub const fn session_stream(self) -> StreamId {
        self.0
    }

    /// Tries to create a session id from its session stream.
    ///
    /// `stream_id` must be *bidirectional* and *client-initiated*, otherwise
    /// an [`Err`] is returned.
    pub fn try_from_session_stream(stream_id: StreamId) -> Result<Self, InvalidSessionId> {
        if stream_id.is_bidirectional() && stream_id.is_client_initiated() {
            Ok(Self(stream_id))
        } else {
            Err(InvalidSessionId)
        }
    }

    /// Creates a session id without checking session stream properties.
    ///
    /// # Safety
    ///
    /// `stream_id` must be *bidirectional* and *client-initiated*.
    #[inline(always)]
    pub const unsafe fn from_session_stream_unchecked(stream_id: StreamId) -> Self {
        debug_assert!(stream_id.is_bidirectional() && stream_id.is_client_initiated());
        Self(stream_id)
    }

    #[inline(always)]
    pub(crate) fn try_from_varint(varint: VarInt) -> Result<Self, InvalidSessionId> {
        Self::try_from_session_stream(StreamId::new(varint))
    }

    #[cfg(test)]
    pub(crate) fn maybe_invalid(varint: VarInt) -> Self {
        Self(StreamId::new(varint))
    }
}

impl fmt::Debug for SessionId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for SessionId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Error for invalid Quarter Stream ID value (too large).
#[derive(Debug, thiserror::Error)]
#[error("invalid QStream ID")]
pub struct InvalidQStreamId;

/// HTTP3 Quarter Stream ID.
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct QStreamId(VarInt);

impl QStreamId {
    /// The largest quarter stream id.
    // SAFETY: value is less than max varint
    pub const MAX: QStreamId =
        unsafe { Self(VarInt::from_u64_unchecked(1_152_921_504_606_846_975)) };

    /// Creates a quarter stream id from its corresponding [`SessionId`]
    #[inline(always)]
    pub const fn from_session_id(session_id: SessionId) -> Self {
        let value = session_id.into_u64() >> 2;
        debug_assert!(value <= Self::MAX.into_u64());

        // SAFETY: after bitwise operation from stream id, result is surely a varint
        let varint = unsafe { VarInt::from_u64_unchecked(value) };

        Self(varint)
    }

    /// Returns its corresponding [`StreamId`].
    ///
    /// This is a *client-initiated* *bidirectional* stream.
    #[inline(always)]
    pub const fn into_stream_id(self) -> StreamId {
        // SAFETY: Quarter Stream ID origin from a valid Stream ID
        let varint = unsafe {
            debug_assert!(self.0.into_inner() << 2 <= VarInt::MAX.into_inner());
            VarInt::from_u64_unchecked(self.0.into_inner() << 2)
        };

        StreamId::new(varint)
    }

    /// Returns its corresponding [`SessionId`].
    #[inline(always)]
    pub const fn into_session_id(self) -> SessionId {
        let stream_id = self.into_stream_id();

        // SAFETY: corresponding stream for qstream is bidirectional and client-initiated
        unsafe {
            debug_assert!(stream_id.is_bidirectional() && stream_id.is_client_initiated());
            SessionId::from_session_stream_unchecked(stream_id)
        }
    }

    /// Returns the integer value as `u64`.
    #[inline(always)]
    pub const fn into_u64(self) -> u64 {
        self.0.into_inner()
    }

    /// Returns the quarter stream id as [`VarInt`] value.
    #[inline(always)]
    pub const fn into_varint(self) -> VarInt {
        self.0
    }

    pub(crate) fn try_from_varint(varint: VarInt) -> Result<Self, InvalidQStreamId> {
        if varint <= Self::MAX.into_varint() {
            Ok(Self(varint))
        } else {
            Err(InvalidQStreamId)
        }
    }

    #[cfg(test)]
    pub(crate) fn maybe_invalid(varint: VarInt) -> QStreamId {
        Self(varint)
    }
}

impl fmt::Debug for QStreamId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for QStreamId {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Error for invalid HTTP status code.
#[derive(Debug, thiserror::Error)]
#[error("invalid HTTP status code")]
pub struct InvalidStatusCode;

/// HTTP status code (rfc9110).
#[derive(Default, Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct StatusCode(u16);

impl StatusCode {
    /// The largest code.
    pub const MAX: Self = Self(599);

    /// The smallest code.
    pub const MIN: Self = Self(100);

    /// HTTP 200 OK status code.
    pub const OK: Self = Self(200);

    /// HTTP 403 Forbidden status code.
    pub const FORBIDDEN: Self = Self(403);

    /// HTTP 404 Not Found status code.
    pub const NOT_FOUND: Self = Self(404);

    /// HTTP 429 Too Many Requests status code.
    pub const TOO_MANY_REQUESTS: Self = Self(429);

    /// Tries to construct from `u32`.
    #[inline(always)]
    pub fn try_from_u32(value: u32) -> Result<Self, InvalidStatusCode> {
        value.try_into()
    }

    /// Extracts the integer value as `u16`.
    #[inline(always)]
    pub fn into_inner(self) -> u16 {
        self.0
    }

    /// Returns true if the status code is 2xx.
    #[inline(always)]
    pub fn is_successful(self) -> bool {
        (200..300).contains(&self.0)
    }
}

impl TryFrom<u8> for StatusCode {
    type Error = InvalidStatusCode;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        if u16::from(value) >= Self::MIN.0 && u16::from(value) <= Self::MAX.0 {
            Ok(Self(u16::from(value)))
        } else {
            Err(InvalidStatusCode)
        }
    }
}

impl TryFrom<u16> for StatusCode {
    type Error = InvalidStatusCode;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        if (Self::MIN.0..=Self::MAX.0).contains(&value) {
            Ok(Self(value))
        } else {
            Err(InvalidStatusCode)
        }
    }
}

impl TryFrom<u32> for StatusCode {
    type Error = InvalidStatusCode;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        if value >= u32::from(Self::MIN.0) && value <= u32::from(Self::MAX.0) {
            Ok(Self(value as u16))
        } else {
            Err(InvalidStatusCode)
        }
    }
}

impl TryFrom<u64> for StatusCode {
    type Error = InvalidStatusCode;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        if value >= u64::from(Self::MIN.0) && value <= u64::from(Self::MAX.0) {
            Ok(Self(value as u16))
        } else {
            Err(InvalidStatusCode)
        }
    }
}

impl FromStr for StatusCode {
    type Err = InvalidStatusCode;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(s.parse().map_err(|_| InvalidStatusCode)?))
    }
}

impl fmt::Debug for StatusCode {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for StatusCode {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use utils::stream_types;
    use utils::StreamType;

    use super::*;

    #[test]
    fn stream_properties() {
        for (id, stream_type) in stream_types(1024) {
            let stream_id = StreamId::new(id);

            match stream_type {
                StreamType::ClientBi => {
                    assert!(stream_id.is_bidirectional());
                    assert!(stream_id.is_client_initiated());
                    assert!(stream_id.is_local(false));
                    assert!(!stream_id.is_local(true));
                }
                StreamType::ServerBi => {
                    assert!(stream_id.is_bidirectional());
                    assert!(!stream_id.is_client_initiated());
                    assert!(!stream_id.is_local(false));
                    assert!(stream_id.is_local(true));
                }
                StreamType::ClientUni => {
                    assert!(!stream_id.is_bidirectional());
                    assert!(stream_id.is_client_initiated());
                    assert!(stream_id.is_local(false));
                    assert!(!stream_id.is_local(true));
                }
                StreamType::ServerUni => {
                    assert!(!stream_id.is_bidirectional());
                    assert!(!stream_id.is_client_initiated());
                    assert!(!stream_id.is_local(false));
                    assert!(stream_id.is_local(true));
                }
            }
        }
    }

    #[test]
    fn session_id() {
        for (id, stream_type) in stream_types(1024) {
            if let StreamType::ClientBi = stream_type {
                assert!(SessionId::try_from_varint(id).is_ok());
                assert!(SessionId::try_from_session_stream(StreamId::new(id)).is_ok());
            } else {
                assert!(SessionId::try_from_varint(id).is_err());
                assert!(SessionId::try_from_session_stream(StreamId::new(id)).is_err());
            }
        }
    }

    #[test]
    fn qstream_id() {
        for (quarter, id) in stream_types(1024)
            .filter(|(_id, r#type)| matches!(r#type, StreamType::ClientBi))
            .map(|(id, _type)| id)
            .enumerate()
        {
            let session_id = SessionId::try_from_varint(id).unwrap();
            let qstream_id = QStreamId::from_session_id(session_id);

            assert_eq!(qstream_id.into_stream_id(), session_id.session_stream());
            assert_eq!(qstream_id.into_session_id(), session_id);
            assert_eq!(qstream_id.into_u64(), quarter as u64);
        }
    }

    mod utils {
        use super::*;

        #[derive(Copy, Clone, Debug)]
        pub enum StreamType {
            ClientBi,
            ServerBi,
            ClientUni,
            ServerUni,
        }

        pub fn stream_types(max_id: u32) -> impl Iterator<Item = (VarInt, StreamType)> {
            [
                StreamType::ClientBi,
                StreamType::ServerBi,
                StreamType::ClientUni,
                StreamType::ServerUni,
            ]
            .into_iter()
            .cycle()
            .enumerate()
            .map(|(index, r#type)| (VarInt::from_u32(index as u32), r#type))
            .take(max_id as usize)
        }
    }
}