zino_http/response/
response_code.rs1use serde::Serialize;
2use std::borrow::Cow;
3use zino_core::SharedString;
4
5pub trait ResponseCode {
8 type ErrorCode: Serialize;
10
11 type BusinessCode: Serialize;
13
14 const OK: Self;
16 const BAD_REQUEST: Self;
18 const INTERNAL_SERVER_ERROR: Self;
20
21 fn status_code(&self) -> u16;
23
24 fn is_success(&self) -> bool;
26
27 #[inline]
29 fn error_code(&self) -> Option<Self::ErrorCode> {
30 None
31 }
32
33 #[inline]
35 fn business_code(&self) -> Option<Self::BusinessCode> {
36 None
37 }
38
39 fn type_uri(&self) -> Option<SharedString> {
42 None
43 }
44
45 fn title(&self) -> Option<SharedString> {
48 None
49 }
50
51 fn message(&self) -> Option<SharedString> {
54 None
55 }
56}
57
58macro_rules! impl_response_code {
59 ($Ty:ty) => {
60 impl ResponseCode for $Ty {
61 type ErrorCode = SharedString;
62 type BusinessCode = u16;
63
64 const OK: Self = Self::OK;
65 const BAD_REQUEST: Self = Self::BAD_REQUEST;
66 const INTERNAL_SERVER_ERROR: Self = Self::INTERNAL_SERVER_ERROR;
67
68 #[inline]
69 fn status_code(&self) -> u16 {
70 self.as_u16()
71 }
72
73 #[inline]
74 fn is_success(&self) -> bool {
75 self.is_success()
76 }
77
78 #[inline]
79 fn type_uri(&self) -> Option<SharedString> {
80 None
81 }
82
83 #[inline]
84 fn title(&self) -> Option<SharedString> {
85 if self.is_success() {
86 None
87 } else {
88 self.canonical_reason().map(Cow::Borrowed)
89 }
90 }
91
92 #[inline]
93 fn message(&self) -> Option<SharedString> {
94 if self.is_success() {
95 self.canonical_reason().map(Cow::Borrowed)
96 } else {
97 None
98 }
99 }
100 }
101 };
102}
103
104impl_response_code!(http::StatusCode);
105
106#[cfg(feature = "http02")]
107impl_response_code!(http02::StatusCode);