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.
See how these examples are generated.
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;
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 with its Simple Icons submodule, then run the existing checks:
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.