From 8b7c89bedd8af2128963d38d963ee6025dbf9fb9 Mon Sep 17 00:00:00 2001 From: Sam Reis Date: Wed, 14 Feb 2018 10:49:08 +1100 Subject: [PATCH] introduce dynamic generation of badges --- Cargo.toml | 1 + assets/badges/outdated.svg | 1 - assets/badges/unknown.svg | 1 - assets/badges/up-to-date.svg | 1 - src/main.rs | 1 + src/server/assets.rs | 9 ------- src/server/mod.rs | 2 +- src/server/views/badge.rs | 43 +++++++++++++++++++++++++++++++++ src/server/views/html/status.rs | 16 ++++++------ src/server/views/mod.rs | 4 +-- src/server/views/status_svg.rs | 20 --------------- 11 files changed, 56 insertions(+), 43 deletions(-) delete mode 100644 assets/badges/outdated.svg delete mode 100644 assets/badges/unknown.svg delete mode 100644 assets/badges/up-to-date.svg create mode 100644 src/server/views/badge.rs delete mode 100644 src/server/views/status_svg.rs diff --git a/Cargo.toml b/Cargo.toml index 5983baf..29c0c21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Sam Rijs "] [dependencies] +badge = "0.1.0" base64 = "0.9.0" failure = "0.1.1" futures = "0.1.18" diff --git a/assets/badges/outdated.svg b/assets/badges/outdated.svg deleted file mode 100644 index 69434ff..0000000 --- a/assets/badges/outdated.svg +++ /dev/null @@ -1 +0,0 @@ -dependenciesdependenciesoutdatedoutdated \ No newline at end of file diff --git a/assets/badges/unknown.svg b/assets/badges/unknown.svg deleted file mode 100644 index 959f89d..0000000 --- a/assets/badges/unknown.svg +++ /dev/null @@ -1 +0,0 @@ -dependenciesdependenciesunknownunknown \ No newline at end of file diff --git a/assets/badges/up-to-date.svg b/assets/badges/up-to-date.svg deleted file mode 100644 index 9222ce5..0000000 --- a/assets/badges/up-to-date.svg +++ /dev/null @@ -1 +0,0 @@ -dependenciesdependenciesup to dateup to date \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f48016a..a243b95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ #![feature(ip_constructors)] #![feature(proc_macro)] +extern crate badge; extern crate base64; #[macro_use] extern crate failure; #[macro_use] extern crate futures; diff --git a/src/server/assets.rs b/src/server/assets.rs index 568fbf4..2aad748 100644 --- a/src/server/assets.rs +++ b/src/server/assets.rs @@ -1,12 +1,3 @@ -//pub mod templates; - -pub static BADGE_UPTODATE_SVG: &'static [u8; 975] = - include_bytes!("../../assets/badges/up-to-date.svg"); -pub static BADGE_OUTDATED_SVG: &'static [u8; 974] = - include_bytes!("../../assets/badges/outdated.svg"); -pub static BADGE_UNKNOWN_SVG: &'static [u8; 972] = - include_bytes!("../../assets/badges/unknown.svg"); - pub static STATIC_STYLE_CSS: &'static str = include_str!(concat!(env!("OUT_DIR"), "/style.css")); pub static STATIC_FAVICON_PNG: &'static [u8; 1338] = diff --git a/src/server/mod.rs b/src/server/mod.rs index c21011e..7e93f36 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -148,7 +148,7 @@ impl Server { fn status_format_analysis(analysis_outcome: Option, format: StatusFormat, repo_path: RepoPath) -> Response { match format { StatusFormat::Svg => - views::status_svg(analysis_outcome), + views::badge::response(analysis_outcome.as_ref()), StatusFormat::Html => views::html::status::render(analysis_outcome, repo_path) } diff --git a/src/server/views/badge.rs b/src/server/views/badge.rs new file mode 100644 index 0000000..224451c --- /dev/null +++ b/src/server/views/badge.rs @@ -0,0 +1,43 @@ +use badge::{Badge, BadgeOptions}; +use hyper::Response; +use hyper::header::ContentType; + +use ::engine::AnalyzeDependenciesOutcome; + +pub fn svg(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Vec { + let opts = match analysis_outcome { + Some(outcome) => { + if outcome.any_outdated() { + BadgeOptions { + subject: "dependencies".into(), + status: "outdated".into(), + color: "#dfb317".into() + } + } else { + BadgeOptions { + subject: "dependencies".into(), + status: "up to date".into(), + color: "#4c1".into() + } + } + }, + None => { + BadgeOptions { + subject: "dependencies".into(), + status: "unknown".into(), + color: "#9f9f9f".into() + } + } + }; + + Badge::new(opts) + .expect("failed to create badge") + .to_svg() + .into_bytes() +} + +pub fn response(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Response { + Response::new() + .with_header(ContentType("image/svg+xml;charset=utf-8".parse().unwrap())) + .with_body(svg(analysis_outcome)) +} diff --git a/src/server/views/html/status.rs b/src/server/views/html/status.rs index 1e0e210..199d1be 100644 --- a/src/server/views/html/status.rs +++ b/src/server/views/html/status.rs @@ -6,7 +6,8 @@ use ordermap::OrderMap; use ::engine::AnalyzeDependenciesOutcome; use ::models::crates::{CrateName, AnalyzedDependency, AnalyzedDependencies}; use ::models::repo::{RepoSite, RepoPath}; -use ::server::assets; + +use super::super::badge; fn dependency_tables(crate_name: CrateName, deps: AnalyzedDependencies) -> Markup { html! { @@ -125,13 +126,14 @@ fn render_success(analysis_outcome: AnalyzeDependenciesOutcome, repo_path: RepoP let status_base_url = format!("{}/{}", &super::SELF_BASE_URL as &str, self_path); let site_icon = get_site_icon(&repo_path.site); - let (hero_class, status_asset) = if analysis_outcome.any_outdated() { - ("is-warning", assets::BADGE_OUTDATED_SVG.as_ref()) - } else { - ("is-success", assets::BADGE_UPTODATE_SVG.as_ref()) - }; + let status_badge = badge::svg(Some(&analysis_outcome)); + let status_data_url = format!("data:image/svg+xml;base64,{}", Base64Display::standard(&status_badge)); - let status_data_url = format!("data:image/svg+xml;base64,{}", Base64Display::standard(status_asset)); + let hero_class = if analysis_outcome.any_outdated() { + "is-warning" + } else { + "is-success" + }; html! { section class=(format!("hero {}", hero_class)) { diff --git a/src/server/views/mod.rs b/src/server/views/mod.rs index d399361..387fe62 100644 --- a/src/server/views/mod.rs +++ b/src/server/views/mod.rs @@ -1,4 +1,2 @@ pub mod html; - -mod status_svg; -pub use self::status_svg::status_svg; +pub mod badge; diff --git a/src/server/views/status_svg.rs b/src/server/views/status_svg.rs deleted file mode 100644 index 63320a1..0000000 --- a/src/server/views/status_svg.rs +++ /dev/null @@ -1,20 +0,0 @@ -use hyper::Response; -use hyper::header::ContentType; - -use ::server::assets; -use ::engine::AnalyzeDependenciesOutcome; - -pub fn status_svg(analysis_outcome: Option) -> Response { - let mut response = Response::new() - .with_header(ContentType("image/svg+xml;charset=utf-8".parse().unwrap())); - if let Some(outcome) = analysis_outcome { - if outcome.any_outdated() { - response.set_body(assets::BADGE_OUTDATED_SVG.to_vec()); - } else { - response.set_body(assets::BADGE_UPTODATE_SVG.to_vec()); - } - } else { - response.set_body(assets::BADGE_UNKNOWN_SVG.to_vec()); - } - response -}