1use ntex_h2::frame::Reason;
2use ntex_http::HeaderValue;
3
4macro_rules! gen_error_code {
5 (
6 $( #[$enum_attr:meta] )*
7 pub enum $name:ident {
8 $(
9 $( #[$enum_item_attr:meta] )*
10 $var:ident=$val:expr
11 ),+
12 }) => {
13 $( #[$enum_attr] )*
14 #[repr(u8)]
15 pub enum $name {
16 $(
17 $( #[$enum_item_attr] )*
18 $var = $val
19 ),+
20 }
21
22 impl $name {
23 #[inline]
24 pub const fn as_str(&self) -> &'static str {
25 match self {
26 $($name::$var => stringify!($var)),+
27 }
28 }
29
30 #[inline]
31 pub const fn signature(&self) -> &'static str {
32 match self {
33 $($name::$var => concat!("grpc-status-", stringify!($var))),+
34 }
35 }
36
37 #[inline]
38 pub const fn code(&self) -> u8 {
39 match self {
40 $($name::$var => $val),+
41 }
42 }
43
44 #[inline]
45 pub const fn code_str(&self) -> &'static str {
46 match self {
47 $($name::$var => stringify!($val)),+
48 }
49 }
50 }
51
52 impl std::convert::TryFrom<u8> for $name {
53 type Error = ();
54 #[inline]
55 fn try_from(v: u8) -> Result<Self, Self::Error> {
56 match v {
57 $($val => Ok($name::$var)),+
58 ,_ => Err(())
59 }
60 }
61 }
62
63 impl From<$name> for u8 {
64 #[inline]
65 fn from(v: $name) -> Self {
66 unsafe { ::std::mem::transmute(v) }
67 }
68 }
69
70 impl From<$name> for HeaderValue {
71 #[inline]
72 fn from(v: $name) -> Self {
73 HeaderValue::from_static(v.code_str())
74 }
75 }
76 };
77}
78
79gen_error_code! {
80 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
81 pub enum GrpcStatus {
82 Ok = 0,
83 Cancelled = 1,
84 Unknown = 2,
85 InvalidArgument = 3,
86 DeadlineExceeded = 4,
87 NotFound = 5,
88 AlredyExists = 6,
89 PermissionDenied = 7,
90 ResourceExhausted = 8,
91 FailedPrecondition = 9,
92 Aborted = 10,
93 OutOfRange = 11,
94 Unimplemented = 12,
95 Internal = 13,
96 Unavailable = 14,
97 DataLoss = 15,
98 Unauthenticated = 16
99 }
100}
101
102impl From<Reason> for GrpcStatus {
103 fn from(reason: Reason) -> GrpcStatus {
104 match reason {
105 Reason::NO_ERROR
106 | Reason::PROTOCOL_ERROR
107 | Reason::INTERNAL_ERROR
108 | Reason::FLOW_CONTROL_ERROR
109 | Reason::SETTINGS_TIMEOUT
110 | Reason::FRAME_SIZE_ERROR
111 | Reason::COMPRESSION_ERROR
112 | Reason::CONNECT_ERROR => GrpcStatus::Internal,
113 Reason::REFUSED_STREAM => GrpcStatus::Unavailable,
114 Reason::CANCEL => GrpcStatus::Cancelled,
115 Reason::ENHANCE_YOUR_CALM => GrpcStatus::ResourceExhausted,
116 Reason::INADEQUATE_SECURITY => GrpcStatus::PermissionDenied,
117 _ => GrpcStatus::Unknown,
118 }
119 }
120}