Add associated type for api response to service type
This commit is contained in:
parent
97066d1e06
commit
8e97cbdf3d
11
src/main.rs
11
src/main.rs
@ -11,7 +11,7 @@ mod statics;
|
||||
use crate::{
|
||||
data::FilePath,
|
||||
error::Result,
|
||||
service::{GitHubApiResponse, Github, Service},
|
||||
service::{ApiResponse, Bitbucket, Github, Service},
|
||||
};
|
||||
use actix_web::{
|
||||
http::header::{self, CacheControl, CacheDirective, Expires, LOCATION},
|
||||
@ -67,12 +67,17 @@ fn redirect<T: Service>(
|
||||
.and_then(move |mut response| match response.status() {
|
||||
StatusCode::OK => Box::new(
|
||||
response
|
||||
.json::<GitHubApiResponse>()
|
||||
.json::<T::Response>()
|
||||
.map(move |resp| {
|
||||
HttpResponse::SeeOther()
|
||||
.header(
|
||||
LOCATION,
|
||||
T::redirect_url(&data.user, &data.repo, &resp.sha, &data.file)
|
||||
T::redirect_url(
|
||||
&data.user,
|
||||
&data.repo,
|
||||
resp.commit_ref(),
|
||||
&data.file,
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.finish()
|
||||
|
@ -1,15 +1,22 @@
|
||||
use crate::data::FilePath;
|
||||
// use actix_web::Error;
|
||||
// use awc::Client;
|
||||
// use futures::Future;
|
||||
// use std::borrow::Cow;
|
||||
|
||||
pub(crate) trait ApiResponse {
|
||||
fn commit_ref(&self) -> &str;
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(crate) struct GitHubApiResponse {
|
||||
pub(crate) sha: String,
|
||||
}
|
||||
|
||||
impl ApiResponse for GitHubApiResponse {
|
||||
fn commit_ref(&self) -> &str {
|
||||
&self.sha
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait Service {
|
||||
type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static;
|
||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||
fn api_url(path: &FilePath) -> String;
|
||||
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||
@ -18,6 +25,8 @@ pub(crate) trait Service {
|
||||
pub(crate) struct Github;
|
||||
|
||||
impl Service for Github {
|
||||
type Response = GitHubApiResponse;
|
||||
|
||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
||||
format!(
|
||||
"https://raw.githubusercontent.com/{}/{}/{}/{}",
|
||||
|
Loading…
Reference in New Issue
Block a user