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
- Error Handling: Detailed error types for better debugging
- Flexible Configuration: Customize timeouts, user agents, and caching
§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(())
}§Error Handling (New in v0.4.0)
The library now 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§
- Fetcher
- Fetcher
Config - Configuration for the Fetcher
- 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