mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +00:00
Appease Clippy 1.68 and refactor http_api (#4068)
## Proposed Changes Two tiny updates to satisfy Clippy 1.68 Plus refactoring of the `http_api` into less complex types so the compiler can chew and digest them more easily. Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
@@ -6,3 +6,4 @@ pub mod metrics;
|
||||
pub mod query;
|
||||
pub mod reject;
|
||||
pub mod task;
|
||||
pub mod uor;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use serde::Serialize;
|
||||
use warp::reply::{Reply, Response};
|
||||
|
||||
/// A convenience wrapper around `blocking_task`.
|
||||
pub async fn blocking_task<F, T>(func: F) -> Result<T, warp::Rejection>
|
||||
@@ -8,16 +9,29 @@ where
|
||||
{
|
||||
tokio::task::spawn_blocking(func)
|
||||
.await
|
||||
.unwrap_or_else(|_| Err(warp::reject::reject())) // This should really be a 500
|
||||
.unwrap_or_else(|_| Err(warp::reject::reject()))
|
||||
}
|
||||
|
||||
/// A convenience wrapper around `blocking_task` that returns a `warp::reply::Response`.
|
||||
///
|
||||
/// Using this method consistently makes it possible to simplify types using `.unify()` or `.uor()`.
|
||||
pub async fn blocking_response_task<F, T>(func: F) -> Result<Response, warp::Rejection>
|
||||
where
|
||||
F: FnOnce() -> Result<T, warp::Rejection> + Send + 'static,
|
||||
T: Reply + Send + 'static,
|
||||
{
|
||||
blocking_task(func).await.map(Reply::into_response)
|
||||
}
|
||||
|
||||
/// A convenience wrapper around `blocking_task` for use with `warp` JSON responses.
|
||||
pub async fn blocking_json_task<F, T>(func: F) -> Result<warp::reply::Json, warp::Rejection>
|
||||
pub async fn blocking_json_task<F, T>(func: F) -> Result<Response, warp::Rejection>
|
||||
where
|
||||
F: FnOnce() -> Result<T, warp::Rejection> + Send + 'static,
|
||||
T: Serialize + Send + 'static,
|
||||
{
|
||||
blocking_task(func)
|
||||
.await
|
||||
.map(|resp| warp::reply::json(&resp))
|
||||
blocking_response_task(|| {
|
||||
let response = func()?;
|
||||
Ok(warp::reply::json(&response))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
25
common/warp_utils/src/uor.rs
Normal file
25
common/warp_utils/src/uor.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use warp::{filters::BoxedFilter, Filter, Rejection};
|
||||
|
||||
/// Mixin trait for `Filter` providing the unifying-or method.
|
||||
pub trait UnifyingOrFilter: Filter<Error = Rejection> + Sized + Send + Sync + 'static
|
||||
where
|
||||
Self::Extract: Send,
|
||||
{
|
||||
/// Unifying `or`.
|
||||
///
|
||||
/// This is a shorthand for `self.or(other).unify().boxed()`, which is useful because it keeps
|
||||
/// the filter type simple and prevents type-checker explosions.
|
||||
fn uor<F>(self, other: F) -> BoxedFilter<Self::Extract>
|
||||
where
|
||||
F: Filter<Extract = Self::Extract, Error = Rejection> + Clone + Send + Sync + 'static,
|
||||
{
|
||||
self.or(other).unify().boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> UnifyingOrFilter for F
|
||||
where
|
||||
F: Filter<Error = Rejection> + Sized + Send + Sync + 'static,
|
||||
F::Extract: Send,
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user