-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce opt-in callback for limit layer
allows you to keep it compatible with inner service Result closes #324
- Loading branch information
Showing
3 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::fmt; | ||
|
||
pub(super) trait ErrorIntoResponse<Error>: Send + Sync + 'static { | ||
type Response: Send + 'static; | ||
type Error: Send + Sync + 'static; | ||
|
||
fn error_into_response(&self, error: Error) -> Result<Self::Response, Self::Error>; | ||
} | ||
|
||
/// Wrapper around a user-specific transformer callback. | ||
pub struct ErrorIntoResponseFn<F>(pub(super) F); | ||
|
||
impl<F: fmt::Debug> fmt::Debug for ErrorIntoResponseFn<F> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("ErrorIntoResponseFn").field(&self.0).finish() | ||
} | ||
} | ||
|
||
impl<F: Clone> Clone for ErrorIntoResponseFn<F> { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} | ||
|
||
impl<Response, ErrorIn, ErrorOut, F> ErrorIntoResponse<ErrorIn> for ErrorIntoResponseFn<F> | ||
where | ||
F: Fn(ErrorIn) -> Result<Response, ErrorOut> + Send + Sync + 'static, | ||
Response: Send + 'static, | ||
ErrorOut: Send + Sync + 'static, | ||
{ | ||
type Response = Response; | ||
type Error = ErrorOut; | ||
|
||
fn error_into_response(&self, error: ErrorIn) -> Result<Self::Response, ErrorOut> { | ||
(self.0)(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters