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
use ntex_h2::frame::Reason;
use ntex_http::HeaderValue;

macro_rules! gen_error_code {
    (
        $( #[$enum_attr:meta] )*
        pub enum $name:ident {
            $(
                $( #[$enum_item_attr:meta] )*
                    $var:ident=$val:expr
            ),+
        }) => {
        $( #[$enum_attr] )*
        #[repr(u8)]
        pub enum $name {
            $(
                $( #[$enum_item_attr] )*
                    $var = $val
            ),+
        }

        impl $name {
            #[inline]
            pub const fn as_str(&self) -> &'static str {
                match self {
                    $($name::$var => stringify!($var)),+
                }
            }
            #[inline]
            pub const fn code(&self) -> u8 {
                match self {
                    $($name::$var => $val),+
                }
            }

            #[inline]
            pub const fn code_str(&self) -> &'static str {
                match self {
                    $($name::$var => stringify!($val)),+
                }
            }
        }

        impl std::convert::TryFrom<u8> for $name {
            type Error = ();
            #[inline]
            fn try_from(v: u8) -> Result<Self, Self::Error> {
                match v {
                    $($val => Ok($name::$var)),+
                    ,_ => Err(())
                }
            }
        }

        impl From<$name> for u8 {
            #[inline]
            fn from(v: $name) -> Self {
                unsafe { ::std::mem::transmute(v) }
            }
        }

        impl From<$name> for HeaderValue {
            #[inline]
            fn from(v: $name) -> Self {
                HeaderValue::from_static(v.code_str())
            }
        }
    };
}

gen_error_code! {
    #[derive(Copy, Clone, PartialEq, Eq, Debug)]
    pub enum GrpcStatus {
        Ok = 0,
        Cancelled = 1,
        Unknown = 2,
        InvalidArgument = 3,
        DeadlineExceeded = 4,
        NotFound = 5,
        AlredyExists = 6,
        PermissionDenied = 7,
        ResourceExhausted = 8,
        FailedPrecondition = 9,
        Aborted = 10,
        OutOfRange = 11,
        Unimplemented = 12,
        Internal = 13,
        Unavailable = 14,
        DataLoss = 15,
        Unauthenticated = 16
    }
}

impl From<Reason> for GrpcStatus {
    fn from(reason: Reason) -> GrpcStatus {
        match reason {
            Reason::NO_ERROR => GrpcStatus::Internal,
            Reason::PROTOCOL_ERROR => GrpcStatus::Internal,
            Reason::INTERNAL_ERROR => GrpcStatus::Internal,
            Reason::FLOW_CONTROL_ERROR => GrpcStatus::Internal,
            Reason::SETTINGS_TIMEOUT => GrpcStatus::Internal,
            Reason::FRAME_SIZE_ERROR => GrpcStatus::Internal,
            Reason::REFUSED_STREAM => GrpcStatus::Unavailable,
            Reason::CANCEL => GrpcStatus::Cancelled,
            Reason::COMPRESSION_ERROR => GrpcStatus::Internal,
            Reason::CONNECT_ERROR => GrpcStatus::Internal,
            Reason::ENHANCE_YOUR_CALM => GrpcStatus::ResourceExhausted,
            Reason::INADEQUATE_SECURITY => GrpcStatus::PermissionDenied,
            _ => GrpcStatus::Unknown,
        }
    }
}