resp_result/convert/
mod.rs

1pub mod from_request;
2use std::future::Future;
3
4use crate::{RespError, RespResult};
5
6/// convert into [`RespResult`](crate::RespResult)
7pub trait IntoRespResult<T, E: RespError> {
8    fn into_rresult(self) -> RespResult<T, E>;
9}
10
11/// convert into [`RespResult`](crate::RespResult) with provide error
12pub trait IntoRespResultWithErr<T, E: RespError> {
13    fn into_with_err<Et: Into<E>>(self, err: Et) -> RespResult<T, E>;
14}
15
16impl<E, T> IntoRespResult<T, E> for Result<T, E>
17where
18    E: RespError,
19{
20    #[inline]
21    fn into_rresult(self) -> RespResult<T, E> {
22        RespResult::from(self)
23    }
24}
25
26impl<E, T> IntoRespResult<T, E> for RespResult<T, E>
27where
28    E: RespError,
29{
30    #[inline]
31    fn into_rresult(self) -> RespResult<T, E> {
32        self
33    }
34}
35
36impl<T, E> IntoRespResultWithErr<T, E> for Option<T>
37where
38    E: RespError,
39{
40    #[inline]
41    fn into_with_err<Et: Into<E>>(self, err: Et) -> RespResult<T, E> {
42        self.ok_or(err).map_err(Into::into).into_rresult()
43    }
44}
45
46#[inline]
47/// receive a [Future](core::future::Future) applying it immediately, then convent the result into [RespResult](crate::RespResult)
48pub async fn resp_try<Fut, T, E>(future: Fut) -> RespResult<T, E>
49where
50    Fut: Future,
51    Fut::Output: IntoRespResult<T, E>,
52    E: RespError,
53{
54    future.await.into_rresult()
55}