Expand description
§shields
A Rust library for generating SVG badges, inspired by shields.io.
This crate provides flexible APIs for creating customizable status badges for CI, version, downloads, and more, supporting multiple styles (flat, plastic, social, for-the-badge, etc.).
§Features
- Generate SVG badge strings with custom label, message, color, logo, and links.
- Multiple badge styles: flat, flat-square, plastic, social, for-the-badge.
- Accurate text width calculation using font width tables embedded at compile time.
- Builder pattern and parameter struct APIs.
- Color normalization and aliasing (e.g., “critical” → red).
- No runtime file I/O required for badge generation.
§Example
use shields::{BadgeStyle, BadgeParams, render_badge_svg};
let params = BadgeParams {
style: BadgeStyle::Flat,
label: Some("build"),
message: Some("passing"),
label_color: Some("green"),
message_color: Some("brightgreen"),
link: Some("https://ci.example.com"),
extra_link: None,
logo: None,
logo_color: None,
};
let svg = render_badge_svg(¶ms);
assert!(svg.contains("passing"));Or use the builder API:
use shields::{BadgeStyle};
use shields::builder::Badge;
let svg = Badge::style(BadgeStyle::Plastic)
.label("version")
.message("1.0.0")
.logo("github")
.build();
assert!(svg.contains("version"));See BadgeParams, BadgeStyle, and BadgeBuilder for details.
Modules§
- builder
- Badge builder module for shields crate.
- measurer
- Font character width measurer for SVG badge rendering.
Structs§
- Badge
Params - Parameters for generating a badge SVG.
- Badge
Params Owned - Owned variant of
BadgeParams, for callers that cannot borrow — typically deserializing from an HTTP query string or JSON body. - Parse
Badge Style Error - Error returned when parsing an unknown
BadgeStylename. - Render
Error - Error type of
try_render_badge_svg. Rendering can no longer fail, so no value of it is ever produced. - Render
Options - Additional rendering options that extend
BadgeParamswithout breaking its literal-construction API.
Enums§
- Badge
Style - Badge style variants supported by the shields crate.
- Font
- Font enumeration for supported fonts
Traits§
- Font
Metrics - Font width calculation trait, to be implemented and injected by the main project
Functions§
- colors_
for_ background - Dynamically calculates foreground and shadow colors based on background color (equivalent to JS colorsForBackground)
- default_
label_ color - Returns the default label color hex string (
#555). - default_
message_ color - Returns the default message color hex string (
#007ec6). - get_
text_ width - Calculates the width of text in the given font (in pixels)
- render_
badge_ svg - Generate an SVG badge string from
BadgeParams. - render_
badge_ svg_ into - Appends the badge SVG to
outinstead of returning a newString. - render_
badge_ svg_ with - Generate an SVG badge string from
BadgeParamsplusRenderOptions. - try_
render_ badge_ svg - Fallible variant of
render_badge_svg. - try_
render_ badge_ svg_ with try_render_badge_svgwith additionalRenderOptions.- warm_up
- Performs the one-time initialization the first logo badge would otherwise pay for, so it does not land on the first request.