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

12
Cargo.lock generated
View File

@@ -58,6 +58,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "async-trait"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96cf8829f67d2eab0b2dfa42c5d0ef737e0724e4a82b01b3e292456202b19716"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "atty"
version = "0.2.14"
@@ -1255,6 +1266,7 @@ name = "pixiv_downloader"
version = "0.0.1"
dependencies = [
"RustyXML",
"async-trait",
"atty",
"bindgen",
"c_fixed_string",

View File

@@ -6,6 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-trait = { version = "0.1", optional = true }
atty = "0.2"
c_fixed_string = { version = "0.2", optional = true }
cfg-if = "1"
@@ -45,7 +46,7 @@ cmake = { version = "0.1", optional = true }
all = ["exif", "ugoira", "server"]
avdict = ["bindgen", "cmake", "flagset"]
exif = ["bindgen", "c_fixed_string", "cmake", "link-cplusplus", "utf16string"]
server = ["hyper"]
server = ["async-trait", "hyper"]
ugoira = ["avdict", "bindgen", "cmake", "link-cplusplus"]
[profile.release-with-debug]

View File

@@ -1,3 +1,6 @@
#[cfg(feature = "async-trait")]
#[macro_use]
extern crate async_trait;
#[macro_use]
extern crate cfg_if;
#[macro_use]

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]})
}
}