1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::badge::color::util::u64_to_hex;
use crate::badge::style::Style;
use crate::badge::{Links, Logo};

use crate::render::renderers::styles::*;
use crate::render::BadgeRenderer;
use seahash::hash;

/// The purpose of a unique id is to make the svg compatible with direct website embedding.
///
/// Hashing is done as a escaping mechanism.
fn gen_id(
  label: &Option<String>,
  message: &str,
  label_color: &str,
  color: &str,
  style: &Style,
) -> String {
  let buf = vec![
    label.as_ref().unwrap_or(&"not_set".to_string()).as_bytes(),
    message.as_bytes(),
    label_color.as_bytes(),
    color.as_bytes(),
    style.to_string().as_bytes(),
  ]
  .iter()
  .cloned()
  .flatten()
  .cloned()
  .collect::<Vec<u8>>();
  u64_to_hex(hash(&buf))
}

/// Badges are valid and have all the necessary
/// fields to construct an SVG without error.
/// Use the [BadgeBuilder](crate::badge::BadgeBuilder) to construct.
///
/// # Example
///
/// ```rust
/// use badge_maker::BadgeBuilder;
/// # use badge_maker::error::Error;
///
/// let badge = BadgeBuilder::new()
///   .message("Example")
///   .build()?;
///
/// # Ok::<(), Error>(())
/// ```
///
#[derive(Debug, Eq, PartialEq)]
pub struct Badge {
  label: Option<String>,
  message: String,
  label_color: String,
  color: String,
  style: Style,
  links: Links,
  logo: Option<Logo>,
  id: String,
  ids: String,
  idr: String,
}

impl Badge {
  pub(crate) fn new(
    label: Option<String>,
    message: String,
    label_color: String,
    color: String,
    style: Style,
    links: Links,
    logo: Option<Logo>,
  ) -> Self {
    let id = gen_id(&label, &message, &label_color, &color, &style);
    let ids = format!("bms-{}", id);
    let idr = format!("bmr-{}", id);

    Self {
      label,
      message,
      label_color,
      color,
      style,
      links,
      logo,
      id,
      ids,
      idr,
    }
  }

  pub fn svg(&self) -> String {
    match self.style {
      Style::Flat => Flat::render(self),
      Style::Plastic => Plastic::render(self),
      Style::FlatSquare => FlatSquare::render(self),
    }
  }

  pub fn label(&self) -> &Option<String> {
    &self.label
  }

  pub fn message(&self) -> &str {
    &self.message
  }

  pub fn label_color(&self) -> &str {
    &self.label_color
  }

  pub fn color(&self) -> &str {
    &self.color
  }

  pub fn style(&self) -> Style {
    self.style
  }

  pub fn links(&self) -> &Links {
    &self.links
  }

  pub fn logo(&self) -> &Option<Logo> {
    &self.logo
  }

  pub fn ids(&self) -> &str {
    &self.ids
  }

  pub fn idr(&self) -> &str {
    &self.idr
  }
  pub fn id(&self) -> &str {
    &self.id
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::badge::style::Style::Flat;

  #[test]
  fn gen_id_test() {
    assert_eq!(
      gen_id(&Some("hello".to_string()), "e", "e", "3", &Flat),
      "bf81a730957fd01d"
    );
    assert_eq!(
      gen_id(
        &Some("hello".to_string()),
        "message",
        "label_color",
        "#ec3d",
        &Flat
      ),
      "58129eed803f3f02"
    );
  }
}