diff --git a/Cargo.lock b/Cargo.lock index bd4bfa3..94193ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,7 @@ name = "badge" version = "0.2.0" dependencies = [ "base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rusttype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/libs/badge/Cargo.toml b/libs/badge/Cargo.toml index 8efc1e4..6d3dc0f 100644 --- a/libs/badge/Cargo.toml +++ b/libs/badge/Cargo.toml @@ -12,4 +12,5 @@ path = "badge.rs" [dependencies] base64 = "0.9.0" +lazy_static = "1.0.0" rusttype = "0.4.0" diff --git a/libs/badge/badge.rs b/libs/badge/badge.rs index 1fc1db1..1c5de72 100644 --- a/libs/badge/badge.rs +++ b/libs/badge/badge.rs @@ -1,6 +1,7 @@ //! Simple badge generator extern crate base64; +#[macro_use] extern crate lazy_static; extern crate rusttype; @@ -34,31 +35,37 @@ impl Default for BadgeOptions { } -pub struct Badge { - options: BadgeOptions, +struct BadgeStaticData { font: Font<'static>, scale: Scale, - offset: Point, + offset: Point } -impl Badge { - pub fn new(options: BadgeOptions) -> Result { +lazy_static! { + static ref DATA: BadgeStaticData = { let collection = FontCollection::from_bytes(FONT_DATA); - // this should never fail in practice - let font = try!(collection.into_font().ok_or("Failed to load font data".to_owned())); + let font = collection.into_font().expect("Failed to load font data"); let scale = Scale { x: FONT_SIZE, y: FONT_SIZE, }; let v_metrics = font.v_metrics(scale); let offset = point(0.0, v_metrics.ascent); - Ok(Badge { - options: options, - font: font, - scale: scale, - offset: offset, - }) + + BadgeStaticData { font, scale, offset } + }; +} + + +pub struct Badge { + options: BadgeOptions +} + + +impl Badge { + pub fn new(options: BadgeOptions) -> Badge { + Badge { options } } @@ -117,7 +124,7 @@ impl Badge { fn calculate_width(&self, text: &str) -> u32 { let glyphs: Vec = - self.font.layout(text, self.scale, self.offset).collect(); + DATA.font.layout(text, DATA.scale, DATA.offset).collect(); let width = glyphs.iter() .rev() .filter_map(|g| { @@ -140,17 +147,11 @@ mod tests { BadgeOptions::default() } - - #[test] - fn test_new() { - assert!(Badge::new(options()).is_ok()); - } - #[test] fn test_calculate_width() { - let badge = Badge::new(options()).unwrap(); - assert_eq!(badge.calculate_width("build"), 31); - assert_eq!(badge.calculate_width("passing"), 48); + let badge = Badge::new(options()); + assert_eq!(badge.calculate_width("build"), 29); + assert_eq!(badge.calculate_width("passing"), 44); } #[test] @@ -164,7 +165,7 @@ mod tests { status: "passing".to_owned(), ..BadgeOptions::default() }; - let badge = Badge::new(options).unwrap(); + let badge = Badge::new(options); file.write_all(badge.to_svg().as_bytes()).unwrap(); } } diff --git a/src/server/views/badge.rs b/src/server/views/badge.rs index fca3a48..b2be7ef 100644 --- a/src/server/views/badge.rs +++ b/src/server/views/badge.rs @@ -39,7 +39,6 @@ pub fn badge(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Badge { }; Badge::new(opts) - .expect("failed to create badge") } pub fn response(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Response {