Skip to main content

ntex_grpc/
status.rs

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