They are just helper functions to make it easy to convert between std Result to axum results.
here is the implementation:
#[allow(unused)]
pub trait AxumResult<T> {
fn into_server_error(self) -> std::result::Result<T, Response>;
fn into_failed_dependency(self) -> std::result::Result<T, Response>;
fn into_not_found(self) -> std::result::Result<T, Response>;
fn into_bad_request(self) -> std::result::Result<T, Response>;
fn into_forbidden(self) -> std::result::Result<T, Response>;
fn into_not_acceptable(self) -> std::result::Result<T, Response>;
fn into_unauthorized(self) -> std::result::Result<T, Response>;
fn into_conflict(self) -> std::result::Result<T, Response>;
}
impl<T, E> AxumResult<T> for Result<T, E>
where
E: std::fmt::Debug + Send + 'static,
{
fn into_server_error(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::INTERNAL_SERVER_ERROR,
&json!({
"err": format!("Internal Server Error: {e:?}")
}),
)),
}
}
fn into_failed_dependency(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::FAILED_DEPENDENCY,
&json!({
"err": format!("Failed Dependency Error: {e:?}")
}),
)),
}
}
fn into_not_found(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::NOT_FOUND,
&json!({
"err": format!("Not Found Error: {e:?}")
}),
)),
}
}
fn into_bad_request(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::BAD_REQUEST,
&json!({
"err": format!("Bad Request Error: {e:?}")
}),
)),
}
}
fn into_forbidden(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::FORBIDDEN,
&json!({
"err": format!("Forbidden Error: {e:?}")
}),
)),
}
}
fn into_not_acceptable(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::NOT_ACCEPTABLE,
&json!({
"err": format!("Not Acceptable Error: {e:?}")
}),
)),
}
}
fn into_unauthorized(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::UNAUTHORIZED,
&json!({
"err": format!("Unauthorized Error: {e:?}")
}),
)),
}
}
fn into_conflict(self) -> std::result::Result<T, Response> {
match self {
Ok(e) => Ok(e),
Err(e) => Err(json_response(
StatusCode::CONFLICT,
&json!({
"err": format!("Conflict Error: {e:?}")
}),
)),
}
}
}