badgelib
A Rust library for generating customizable SVG badges, similar to shields.io.
Features
- Generate SVG badges with custom text, colors, and icons
- Built-in width calculation for proper badge sizing
- Support for Simple Icons
- Optional Axum web framework integration
- Flexible badge styling options
- Built-in formatters for common badge types:
- Version badges
- Download count badges
- Rating badges
- Star count badges
- Duration/age badges
- License badges
Installation
Add badgelib to your project using the following cargo command:
To enable Axum integration, use:
Usage
use Badge;
// Create a simple version badge
let badge = default.for_version;
// Generate SVG
let svg = badge.to_svg;
// Or generate JSON
let json = badge.to_json;
Customization
Badges can be customized with:
- Custom label and value text
- Label and value colors
- Icons (using Simple Icons)
- Icon colors
- Border radius
- Different styles (flat, flat-square, etc)
use ;
let badge = default
.label
.value
.label_color
.value_color
.logo
.logo_color
.with_radius
.to_svg;
Using with Axum
When enabled with the axum feature, badges can be used as responses in Axum handlers:
async
Built-in Formatters
The library includes several built-in formatters for common badge types:
use ;
use ;
// Version badge (automatically adds v prefix and colors)
let version = default.for_version; // Shows as "v1.0.0"
// License badge
let license = default.for_license;
// Download count badge with period and automatic formatting
let downloads = default.for_downloads; // Shows as "1.2M/month"
// CI status badge (green/red)
let ci = default.for_ci_status; // Shows as "build passing"
// Generic count badge with automatic formatting
let count = default.for_count; // Shows as "1.2k"
// Size badge with IEC formatting
let size = default.for_size; // Shows as "1.2 MiB"
// Rating badge (0-5 scale)
let rating = default.for_rating; // Shows as "4.5/5"
// Star rating badge with visual stars
let stars = default.for_stars; // Shows as "★★★★½"
// Duration/age badge with automatic formatting and colors
let date = parse_from_rfc3339.unwrap;
let age = default.for_duration; // Shows as "1 year ago"
Each formatter provides:
- Automatic value formatting (numbers, dates, etc)
- Contextual colors based on values
- Sensible defaults for labels
- Built-in data visualization where appropriate