Expand description
§url-preview
A high-performance Rust library for generating rich URL previews with specialized support for Twitter/X and GitHub.
§Features
- High Performance: Optimized with concurrent processing and smart caching
- Platform Support: Specialized handlers for Twitter/X and GitHub
- Rich Metadata: Extract titles, descriptions, images, and more
- Security: SSRF protection, content limits, and URL validation
- Error Handling: Detailed error types for better debugging
- Flexible Configuration: Customize timeouts, user agents, caching, and security policies
§Quick Start
use url_preview::{PreviewService, PreviewError};
#[tokio::main]
async fn main() -> Result<(), PreviewError> {
let service = PreviewService::new();
let preview = service.generate_preview("https://www.rust-lang.org").await?;
println!("Title: {:?}", preview.title);
println!("Description: {:?}", preview.description);
Ok(())
}
§Security (New in v0.5.0)
The library includes comprehensive security features by default:
use url_preview::{PreviewService, PreviewError};
let service = PreviewService::new();
// These will be blocked by default
match service.generate_preview("http://localhost:8080").await {
Err(PreviewError::LocalhostBlocked) => println!("Localhost blocked"),
_ => {}
}
match service.generate_preview("http://192.168.1.1").await {
Err(PreviewError::PrivateIpBlocked(ip)) => println!("Private IP {} blocked", ip),
_ => {}
}
§Error Handling
The library provides specific error types:
use url_preview::{PreviewService, PreviewError};
let service = PreviewService::new();
match service.generate_preview("https://invalid.url").await {
Ok(preview) => { /* handle preview */ },
Err(PreviewError::NotFound(msg)) => println!("404: {}", msg),
Err(PreviewError::DnsError(msg)) => println!("DNS failed: {}", msg),
Err(PreviewError::TimeoutError(msg)) => println!("Timeout: {}", msg),
Err(PreviewError::ServerError { status, message }) => {
println!("Server error {}: {}", status, message)
},
Err(e) => println!("Other error: {}", e),
}
Structs§
- Content
Limits - Content size and time limits configuration
- Fetcher
- Fetcher
Config - Metadata
Extractor - Metadata extractor, responsible for extracting preview information from webpage content
- Preview
- Preview
Service - PreviewService provides a unified preview generation service It can automatically identify different types of URLs and use appropriate processing strategies
- Preview
Service Config - UrlPreview
Generator - UrlValidation
Config - Configuration for URL validation
- UrlValidator
- Validates a URL according to security policies