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
use crate::{
frame::ConnectionClose,
varint::{VarInt, VarIntError},
};
use core::{convert::TryFrom, ops};
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Error(VarInt);
impl core::fmt::Debug for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "application::Error({})", self.0.as_u64())
}
}
impl Error {
pub const UNKNOWN: Self = Self(VarInt::from_u8(0));
pub fn new(value: u64) -> Result<Self, VarIntError> {
Ok(Self(VarInt::new(value)?))
}
}
impl ops::Deref for Error {
type Target = u64;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl TryInto for Error {
fn application_error(&self) -> Option<Error> {
Some(*self)
}
}
impl From<VarInt> for Error {
fn from(value: VarInt) -> Self {
Self(value)
}
}
impl From<Error> for VarInt {
fn from(e: Error) -> Self {
e.0
}
}
impl From<Error> for u64 {
fn from(e: Error) -> Self {
e.0.as_u64()
}
}
impl<'a> From<Error> for ConnectionClose<'a> {
fn from(error: Error) -> Self {
ConnectionClose {
error_code: error.0,
frame_type: None,
reason: None,
}
}
}
macro_rules! convert {
($ty:ident) => {
impl From<$ty> for Error {
fn from(value: $ty) -> Self {
Self(VarInt::from(value))
}
}
};
}
convert!(u8);
convert!(u16);
convert!(u32);
impl TryFrom<u64> for Error {
type Error = VarIntError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
Ok(VarInt::try_from(value)?.into())
}
}
pub trait TryInto {
fn application_error(&self) -> Option<Error>;
}