badgelib
badgelib is a Rust library for rendering customizable SVG badges directly in your application. Use it when you want to generate badges without running a separate badge service. It powers badges.ws; the web service and its data integrations live in the main badges repository.
Install
Quick start
use ;
Badge::to_svg() returns the complete SVG document as a String. Badges can include separate label and value colors, left-to-right gradients, an icon from Simple Icons, a custom icon color, and a border radius.
Gradients
Pass two or more colors to distribute them evenly from left to right:
use ;
let svg = new
.label
.value
.value_gradient
.logo
.to_svg;
Use label_gradient for the left side of a two-part badge. Calling a solid-color setter replaces the corresponding gradient, and calling a gradient setter replaces the solid color.
Custom icons
logo looks up a slug in Simple Icons, including slugs removed or renamed in historical releases. Simple Icons only supports single-color icons; for anything else, pass raw SVG markup directly with icon_svg:
use Badge;
let svg = new
.value
.icon_svg
.to_svg;
Animations
Select one opinionated animation with animation. Calling it again replaces the previous selection; speed, direction, easing, and target area are intentionally not configurable. All animations respect the viewer's reduced-motion preference.
Animation::Flow scrolls every configured gradient continuously. A gradient on only the label or value animates only that section; solid-color sections remain static.
use ;
let svg = new
.value
.value_gradient
.animation
.to_svg;
Animation::Shine periodically sweeps a soft diagonal light streak across the full width of the badge, including both label and value sections.
Animation::Aurora layers one continuous field of slowly drifting, softly blurred lights across the full badge. Its palette is derived from all existing label and value background colors.
let svg = new
.value
.value_color
.animation
.to_svg;
Built-in badge types
The library includes helpers for common badge values and their default formatting:
use ;
let version = new.for_version;
let license = new.for_license;
let downloads = new.for_downloads;
let build = new.for_ci_status;
let size = new.for_size;
let rating = new.for_rating;
Available helpers cover versions, licenses, downloads, CI status, counts, sizes, ratings, star ratings, and relative durations. You can override their labels and colors with the regular builder methods.
JSON output
Use Badge::to_json() when you need the badge parameters as JSON instead of rendered SVG:
let json = new
.label
.value
.to_json;
format and cache also appear when the axum feature is enabled.
Axum integration
Enable the optional axum feature to return a Badge directly from a handler:
use Badge;
async
With this feature enabled, Badge implements Axum's IntoResponse and returns SVG by default with the appropriate content type and cache headers.
Development
Clone the repository, then run the existing checks:
make update refreshes dependencies and regenerates the embedded historical Simple Icons map and font-width table.
Credits
- Inspired by Shields.io and Badgen.
- Icons are provided by Simple Icons.
- Badge text is rendered with DejaVu Sans.
License
Distributed under the MIT License.