use super::StatusCode;
use crate::SharedString;
use serde::Serialize;
use std::borrow::Cow;
pub trait ResponseCode {
type ErrorCode: Serialize;
type BusinessCode: Serialize;
const OK: Self;
const BAD_REQUEST: Self;
const INTERNAL_SERVER_ERROR: Self;
fn status_code(&self) -> u16;
fn is_success(&self) -> bool;
#[inline]
fn error_code(&self) -> Option<Self::ErrorCode> {
None
}
#[inline]
fn business_code(&self) -> Option<Self::BusinessCode> {
None
}
fn type_uri(&self) -> Option<SharedString> {
None
}
fn title(&self) -> Option<SharedString> {
None
}
fn message(&self) -> Option<SharedString> {
None
}
}
impl ResponseCode for StatusCode {
type ErrorCode = SharedString;
type BusinessCode = u16;
const OK: Self = StatusCode::OK;
const BAD_REQUEST: Self = StatusCode::BAD_REQUEST;
const INTERNAL_SERVER_ERROR: Self = StatusCode::INTERNAL_SERVER_ERROR;
#[inline]
fn status_code(&self) -> u16 {
self.as_u16()
}
#[inline]
fn is_success(&self) -> bool {
self.is_success()
}
#[inline]
fn type_uri(&self) -> Option<SharedString> {
None
}
#[inline]
fn title(&self) -> Option<SharedString> {
if self.is_success() {
None
} else {
self.canonical_reason().map(Cow::Borrowed)
}
}
#[inline]
fn message(&self) -> Option<SharedString> {
if self.is_success() {
self.canonical_reason().map(Cow::Borrowed)
} else {
None
}
}
}