openapi-ui
A Rust library for generating custom UI for OpenAPI/Swagger documentation.
Table of Contents
- Features
- Installation
- Quick Start
- Web Framework Integration
- Theming
- API Reference
- OpenAPI Structures
- Error Handling
- Complete Example
- License
Features
- ✅ OpenAPI 3.0.x and 3.1.x support
- ✅ Light and dark themes with toggle
- ✅ Responsive sidebar navigation
- ✅ Endpoint search/filter
- ✅ Syntax highlighting (Highlight.js)
- ✅ Code copy buttons
- ✅ Method color coding
- ✅ Parameter tables
- ✅ Request/response examples
- ✅ Security scheme display
- ✅ Tag-based grouping
Installation
Add this to your Cargo.toml:
[]
= "0.1.9"
Quick Start
Generate from OpenAPI JSON
use ;
Theme Modes
use ;
// System mode - both themes with user toggle (default)
let html = generate_docs?;
// Light mode only - no dark theme
let html = generate_docs?;
// Dark mode only - no light theme
let html = generate_docs?;
Using the Builder
use ;
let spec: OpenAPISpec = from_str?;
let html = new
.theme // "light", "dark", or "system"
.build;
write?;
Web Framework Integration
openapi-ui is framework-agnostic and returns a simple String of HTML. This makes it easy to integrate with any Rust web framework.
Check the examples/ directory for complete implementations:
Basic Axum Integration
use ;
use ;
async
async
Using with utoipa
The OpenAPI JSON string can be generated at compile time using utoipa. Annotate your routes and models, then pass the generated JSON to openapi-ui:
use OpenApi;
use ;
;
// Generate the OpenAPI JSON from utoipa
let openapi_json = openapi.to_pretty_json.unwrap;
// Pass it to openapi-ui
let html = generate_docs.unwrap;
write.unwrap;
All framework examples in the examples/ directory include comments showing how to integrate with utoipa.
Theming
Includes built-in light and dark themes with a theme toggle.
Default Theme Variables
Light Theme
| Variable | Value | Description |
|---|---|---|
--bg |
#faf9f5 |
Main background |
--bg-card |
#ffffff |
Card backgrounds |
--t1 |
#1f1e1d |
Primary text |
--accent |
#c96442 |
Accent color (terracotta) |
--green |
#2d8a4e |
GET method, 2xx status |
--blue |
#1c6bbb |
POST method |
--orange |
#b56613 |
PUT method, 4xx status |
--red |
#c0392b |
DELETE method, 5xx status |
Dark Theme
| Variable | Value | Description |
|---|---|---|
--bg |
#1e1c19 |
Main background |
--bg-card |
#2d2b27 |
Card backgrounds |
--t1 |
#ede8df |
Primary text |
--accent |
#d4714f |
Accent color (terracotta) |
--green |
#4caf72 |
GET method, 2xx status |
--blue |
#5a9fe0 |
POST method |
--orange |
#d4943a |
PUT method, 4xx status |
--red |
#e05c4b |
DELETE method, 5xx status |
Customizing Themes
Use the custom_css parameter of generate_docs to inject custom CSS that overrides theme variables:
use ;
let custom_css = r#"
:root {
--accent: #3b82f6; /* Custom blue accent */
--accent-h: #2563eb; /* Hover state */
--accent-bg: #eff6ff; /* Accent background tint */
}
[data-theme="dark"] {
--accent: #60a5fa; /* Lighter blue for dark mode */
--accent-h: #93c5fd;
--accent-bg: #1e3a5f;
}
"#;
let html = generate_docs?;
write?;
Example: Brand Colors
let brand_css = r#"
:root {
/* Brand: Forest Green */
--accent: #2d5a3d;
--accent-h: #3d7a52;
--accent-bg: #e8f5e9;
/* Override method colors if needed */
--green: #2d5a3d; /* GET */
--blue: #1b5e7a; /* POST */
--purple: #5a3d7a; /* PATCH */
}
[data-theme="dark"] {
--accent: #4caf72;
--accent-h: #66bb8a;
--accent-bg: #1b3a25;
}
"#;
Using Theme CSS Directly
You can also access the built-in theme CSS for reference or extension:
use ;
// Get light theme CSS
let light_css = Light.as_css;
// Get dark theme CSS
let dark_css = Dark.as_css;
// Get both themes (for system theme switching)
let all_css = get_complete_theme_css;
// Get specific theme by name
let css = get_theme_css;
API Reference
Core Functions
generate_docs(json: &str, mode: ThemeMode, custom_css: Option<&str>) -> Result<String>
Generate HTML documentation from an OpenAPI JSON string with theme mode and optional custom CSS.
use ;
// System mode (default) - both themes with user toggle
let html = generate_docs?;
// Light mode only
let html = generate_docs?;
// Dark mode only
let html = generate_docs?;
// With custom CSS
let custom_css = ":root { --accent: #3b82f6; }";
let html = generate_docs?;
generate_ui(spec: &OpenAPISpec) -> String
Generate HTML from a parsed OpenAPISpec struct.
let spec: OpenAPISpec = from_str?;
let html = generate_ui;
generate_base_ui() -> String
Generate a template with sample Petstore data (useful for demos).
let html = generate_base_ui;
ThemeMode Enum
use ThemeMode;
// Theme variants
Light // Light theme only
Dark // Dark theme only
System // Both themes with toggle (default)
// Convert from string
let mode = from_str;
// Convert to string
let mode_str = System.as_str; // "system"
UIBuilder
Fluent builder for customizing output:
use UIBuilder;
let html = new
.theme // Default: "system"
.base_url
.build;
Theme Functions
use ;
// Enum variants
Light
Dark
// Convert string to Theme
let theme = from_str;
// Get CSS for a theme
let css = Light.as_css;
// Get all theme CSS
let css = get_complete_theme_css;
OpenAPI Structures
The openapi module provides types matching the OpenAPI 3.x specification:
use ;
Building a Spec Programmatically
use ;
use HashMap;
let mut paths = new;
// Add a GET /users endpoint
paths.insert;
let spec = OpenAPISpec ;
let html = generate_ui;
Error Handling
use ;
match generate_docs
Or with ?:
Complete Example
use ;
use fs;
License
MIT