Skip to content

Commit

Permalink
Optional subject (#11)
Browse files Browse the repository at this point in the history
* Make subject optional

- switch clap (Better cli)
- rename bin to run as cargo sucommand

Signed-off-by: Suntharesan Mohan <[email protected]>

* Update badge on api

Signed-off-by: Suntharesan Mohan <[email protected]>
  • Loading branch information
msuntharesan committed Jul 25, 2020
1 parent 9f68f85 commit 82074a3
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ harness = false

[dependencies]
ab_glyph = "0.2"
argh = "0.1"
clap = { version = "3.0.0-beta.1", features = ["derive", "suggestions", "color"] }
cssparser = "0.27"
maud = "0.22"
phf = "0.8"
Expand Down
34 changes: 22 additions & 12 deletions benches/badge_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,85 @@ use merit::{Badge, Color, Icon, Size, Styles, DEFAULT_BLUE, DEFAULT_WHITE};
use std::convert::TryFrom;

pub fn criterion_benchmark(c: &mut Criterion) {
let all_text = Badge::new("Hello")
let all_text = Badge::new()
.subject("Hello")
.color(Color("#6f42c1".to_string()))
.style(Styles::Flat)
.icon(Icon::try_from("github").unwrap())
.icon_color(Color("0366d6".to_string()))
.size(Size::Large)
.text("text content");

let all_data = Badge::new("Hello")
let all_data = Badge::new()
.subject("Hello")
.color(Color("#6f42c1".to_string()))
.style(Styles::Flat)
.icon(Icon::try_from("github").unwrap())
.icon_color(Color("#0366d6".to_string()))
.size(Size::Large)
.data(vec![7, 5, 2, 4, 8, 3, 7]);

let subject = Badge::new("Hello")
let subject = Badge::new()
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.subject();
.text("Hello");

let with_text = Badge::new("Hello")
let with_text = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.text("text content");

let medium_size = Badge::new("Hello")
let medium_size = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.size(Size::Medium)
.text("text content");

let large_size = Badge::new("Hello")
let large_size = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.size(Size::Large)
.text("text content");

let red = Badge::new("Hello")
let red = Badge::new()
.subject("Hello")
.color(Color("ff0000".to_string()))
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.text("red");

let icon_brand = Badge::new("Hello")
let icon_brand = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon(Icon::try_from("github").unwrap())
.icon_color(DEFAULT_WHITE.parse().unwrap())
.text("brand");

let icon_solid = Badge::new("Hello")
let icon_solid = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon(Icon::try_from("code").unwrap())
.icon_color(DEFAULT_WHITE.parse().unwrap())
.text("solid");

let data = Badge::new("Hello")
let data = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
.data(vec![1, 5, 2, 4, 8, 3, 7]);

let flat = Badge::new("Hello")
let flat = Badge::new()
.subject("Hello")
.color(DEFAULT_BLUE.parse().unwrap())
.style(Styles::Classic)
.icon_color(DEFAULT_WHITE.parse().unwrap())
Expand Down
2 changes: 1 addition & 1 deletion examples/badge_default.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use merit::Badge;

fn main() {
let badge = Badge::new("Badge Maker");
let badge = Badge::new().text("Badge Maker");

println!("{}", badge);
}
6 changes: 4 additions & 2 deletions merit-api/src/badge_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ async fn url_badge_handler(

let data: BadgeOptions = b.json().await?;

let mut badge = Badge::new(&data.subject);
let mut badge = Badge::new();
badge.subject(&data.subject);

match (data.color, query.color) {
(_, Some(c)) => {
Expand Down Expand Up @@ -95,7 +96,8 @@ struct BadgeInfo {

fn badge_handler((params, query): (web::Path<BadgeInfo>, web::Query<QueryInfo>)) -> HttpResponse {
let query = query.into_inner();
let mut req_badge = Badge::new(&params.subject);
let mut req_badge = Badge::new();
req_badge.subject(&params.subject);
if let Some(c) = query.color {
req_badge.color(c);
}
Expand Down
6 changes: 2 additions & 4 deletions merit-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extern crate actix_web;
mod badge_routes;
mod utils;

use actix_files::Files;
use actix_web::{
http::{header, StatusCode},
middleware, web, App, HttpResponse, HttpServer, Responder,
Expand All @@ -31,8 +30,8 @@ async fn favicon() -> impl Responder {
}

async fn default_404() -> impl Responder {
let mut badge = Badge::new("Error");
badge.color(DEFAULT_GRAY.parse().unwrap());
let mut badge = Badge::new();
badge.subject("Error").color(DEFAULT_GRAY.parse().unwrap());

HttpResponse::NotFound()
.content_type("image/svg+xml")
Expand All @@ -56,7 +55,6 @@ async fn main() -> io::Result<()> {
.service(index)
.service(favicon)
.configure(badge_routes::config)
.service(Files::new("/", "./merit-web/dist").index_file("index.html"))
});

server = if let Some(l) = listenfd.take_tcp_listener(0)? {
Expand Down
5 changes: 2 additions & 3 deletions merit-api/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ impl Default for BadgeError {
impl BadgeError {
pub fn err_badge(&self) -> String {
let icon: Icon = Icon::try_from("exclamation-circle").unwrap();
let mut badge = Badge::new("Error");
badge.icon(icon);
badge.color("red".parse().unwrap());
let mut badge = Badge::new();
badge.subject("Error").icon(icon).color("red".parse().unwrap());

let text = match self {
BadgeError::Http {
Expand Down
25 changes: 25 additions & 0 deletions src/badge/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl Content for &str {
}
}

#[derive(Default)]
pub(super) struct ContentSize {
pub(super) x: usize,
pub(super) y: usize,
Expand Down Expand Up @@ -110,4 +111,28 @@ mod tests {
let bc = s.content(20);
assert!(bc.width > 0);
}
#[test]
fn content_text_has_width() {
let text = "".content(20);
assert_eq!(text.width, 0);
let text = "npm".content(20);
assert_eq!(text.width, 36);
let text = "long text".content(20);
assert_eq!(text.width, 73);
}

#[test]
fn content_data_has_width() {
let d1 = vec![].content(20);
assert_eq!(d1.width, 0);
let d2 = vec![2, 4, 3, 2].content(20);
assert_eq!(d2.width, 100);
}

#[test]
fn content_data_is_same() {
let d1 = vec![2, 4, 3, 2].content(20);
let d2 = &vec![2, 4, 3, 2].content(20);
assert_eq!(d1.content, d2.content);
}
}
Loading

0 comments on commit 82074a3

Please sign in to comment.