nestforge_core/
http_ext.rs1use std::fmt::Display;
2
3use crate::HttpException;
4
5pub trait ResultHttpExt<T, E> {
6 fn or_bad_request(self) -> Result<T, HttpException>;
7}
8
9impl<T, E> ResultHttpExt<T, E> for Result<T, E>
10where
11 E: Display,
12{
13 fn or_bad_request(self) -> Result<T, HttpException> {
14 self.map_err(|_err| HttpException::bad_request("invalid request"))
15 }
16}
17
18pub trait OptionHttpExt<T> {
19 fn or_not_found(self, message: impl Into<String>) -> Result<T, HttpException>;
20 fn or_not_found_id(self, resource: &str, id: impl Display) -> Result<T, HttpException>;
21}
22
23impl<T> OptionHttpExt<T> for Option<T> {
24 fn or_not_found(self, message: impl Into<String>) -> Result<T, HttpException> {
25 self.ok_or_else(|| HttpException::not_found(message.into()))
26 }
27
28 fn or_not_found_id(self, resource: &str, id: impl Display) -> Result<T, HttpException> {
29 self.ok_or_else(|| {
30 HttpException::not_found(format!("{} with id {} not found", resource, id))
31 })
32 }
33}