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, a custom SVG icon, and a border radius.
Colors
Choose a named color or pass a validated three- or six-digit hexadecimal value. The built-in colors retain the original palette's hues with enough depth for a consistent white foreground; White uses dark text instead.
use ;
let svg = new
.label
.value
.value_color
.to_svg;
Styles
Badges use the rounded Flat style by default. Select FlatSquare for square corners and solid backgrounds without the subtle highlight used by Flat. Use ForTheBadge for a larger badge with uppercase text and a bold value:
use ;
let svg = new
.label
.value
.style
.to_svg;
Use radius when you only want to override the corner radius. A custom radius does not change the selected style's background treatment.
Gradients
Pass two or more colors to distribute them evenly from left to right:
use ;
let svg = new
.label
.value
.value_gradient
.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.
Simple Icons
The bundled Simple Icons collection is opt-in so applications that do not use it avoid its compile-time and binary-size cost:
With the feature enabled, logo and logo_color look up and customize a slug from the current collection or any historical release supported by badgelib. Without the feature, these builder methods are not available and incoming logo fields are ignored during deserialization:
Standard Simple Icons follow the text color unless logo_color is set explicitly.
use ;
let svg = new
.value
.logo
.logo_color
.to_svg;
logo accepts unknown slugs and simply omits the image. Use try_logo when an unknown slug should be an error:
let badge = new
.try_logo
.expect;
Custom icons
Custom SVG icons remain available without features. Use icon_svg for project-specific artwork or icons that require multiple colors or shapes:
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;
logo and logoColor also appear when the simple-icons feature is enabled. format and cache 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.