From 8e97cbdf3db53ab4535ccb96dacaf56fdb084c90 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Fri, 26 Jul 2019 14:18:36 +0200 Subject: [PATCH] Add associated type for api response to service type --- src/main.rs | 13 +++++++++---- src/service.rs | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7876245..c934b32 100644 --- a/src/main.rs +++ b/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,13 +67,18 @@ fn redirect( .and_then(move |mut response| match response.status() { StatusCode::OK => Box::new( response - .json::() + .json::() .map(move |resp| { HttpResponse::SeeOther() .header( LOCATION, - T::redirect_url(&data.user, &data.repo, &resp.sha, &data.file) - .as_str(), + T::redirect_url( + &data.user, + &data.repo, + resp.commit_ref(), + &data.file, + ) + .as_str(), ) .finish() }) diff --git a/src/service.rs b/src/service.rs index feab05d..60d19f1 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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/{}/{}/{}/{}",