This commit is contained in:
2022-07-07 14:18:02 +00:00
committed by GitHub
parent 6857334f53
commit 52ce86764c
6 changed files with 29 additions and 8 deletions

View File

@@ -34,7 +34,7 @@ impl Service<Request<Body>> for PixivDownloaderSvc {
fn call(&mut self, req: Request<Body>) -> Self::Future {
match self.routes.match_route(&req) {
Some(route) => Box::pin(async move {
match route.response(req) {
match route.response(req).await {
Ok(data) => Ok(data),
Err(e) => {
println!("{}", e);

View File

@@ -9,20 +9,24 @@ pub trait MatchRoute<T, R> {
fn match_route(&self, req: &Request<T>) -> bool;
}
#[async_trait]
pub trait ResponseFor<T, R> {
fn response(&self, req: Request<T>) -> Result<Response<R>, PixivDownloaderError>;
async fn response(&self, req: Request<T>) -> Result<Response<R>, PixivDownloaderError>;
}
#[async_trait]
pub trait ResponseJsonFor<T> {
fn response_json(&self, req: Request<T>) -> Result<JsonValue, PixivDownloaderError>;
async fn response_json(&self, req: Request<T>) -> Result<JsonValue, PixivDownloaderError>;
}
#[async_trait]
impl<T, U> ResponseFor<T, Body> for U
where
U: ResponseJsonFor<T>,
U: ResponseJsonFor<T> + Sync + Send,
T: Sync + Send + 'static,
{
fn response(&self, req: Request<T>) -> Result<Response<Body>, PixivDownloaderError> {
let re = self.response_json(req)?;
async fn response(&self, req: Request<T>) -> Result<Response<Body>, PixivDownloaderError> {
let re = self.response_json(req).await?;
Ok(Response::new(Body::from(re.to_string())))
}
}

View File

@@ -17,8 +17,9 @@ impl VersionContext {
}
}
#[async_trait]
impl ResponseJsonFor<Body> for VersionContext {
fn response_json(&self, _req: Request<Body>) -> Result<JsonValue, PixivDownloaderError> {
async fn response_json(&self, _req: Request<Body>) -> Result<JsonValue, PixivDownloaderError> {
Ok(json::object! {"version": [0, 0, 1, 0]})
}
}