URL Preview
A high-performance Rust library for generating rich URL previews with specialized support for Twitter/X and GitHub. This library offers efficient caching, concurrent processing, comprehensive metadata extraction, and detailed error reporting.
What's New in v0.4.0
- Enhanced Error Handling: New specific error types for better error differentiation
- Invalid URL Detection: Properly detects and reports 404s and invalid resources
- Improved Performance: Refactored internals for better concurrent processing
- Better Feature Management: Fixed compilation issues with optional features
Features
- High Performance: Optimized for speed with concurrent processing and batch operations
- Smart Caching: Built-in DashMap-based caching system for lightning-fast responses
- Platform-Specific Handlers:
- Twitter/X: Specialized handling with oEmbed support
- GitHub: Enhanced repository information extraction with API integration
- Flexible Configuration:
- Customizable HTTP clients with different configurations
- Adjustable concurrent request limits
- Configurable cache sizes and strategies
- Rich Metadata Extraction:
- Title, description, and images
- Open Graph and Twitter Card metadata
- Favicons and site information
- Advanced Error Handling:
- Specific error types for DNS, timeout, and HTTP errors
- Detailed error messages for debugging
- Proper 404 and invalid resource detection
- Modern Rust Features:
- Async/await with Tokio
- Thread-safe components
- Zero-cost abstractions
Installation
Add this to your Cargo.toml:
[]
= "0.4.0"
# Optional features
= { = "0.4.0", = ["full"] }
# Or select specific features
= { = "0.4.0", = ["cache", "logging", "github", "twitter"] }
Feature Flags
default: Basic functionality with default reqwest featurescache: Enable caching support with DashMaplogging: Enable structured logging with tracinggithub: Enable GitHub-specific preview enhancementstwitter: Enable Twitter/X oEmbed integrationfull: Enable all features
Quick Start
Here's a simple example to get started:
use ;
async
Advanced Usage
Error Handling
The library now provides detailed error types for better error handling:
use ;
async
Batch Processing
Process multiple URLs efficiently:
use PreviewService;
use join_all;
async
Custom Configuration
Configure the service with specific requirements:
use ;
use Duration;
let config = new // Cache capacity
.with_max_concurrent_requests
.with_default_fetcher;
let service = new_with_config;
Specialized Platform Support
Twitter/X Integration
GitHub Repository Information
Caching Strategies
Control caching behavior for different use cases:
use ;
// Service with caching enabled (default)
let cached_service = new;
// Service with caching disabled
let no_cache_service = no_cache;
// Manual cache operations if needed
Logging Configuration
Configure comprehensive logging:
Performance Optimization
Concurrent Request Limiting
Control the number of concurrent requests to prevent resource exhaustion:
use ;
let config = new
.with_max_concurrent_requests; // Default is MAX_CONCURRENT_REQUESTS (500)
let service = new_with_config;
Retry Strategy
The library automatically retries on server errors and timeouts:
// The fetcher will automatically retry up to 3 times for:
// - Server errors (5xx)
// - Timeouts
// - Connection errors
// But NOT for client errors (4xx) or DNS failures
Examples
Check the examples/ directory for more comprehensive examples:
url_preview.rs- Basic usage and caching demonstrationgithub_preview.rs- GitHub-specific featurestwitter_preview.rs- Twitter/X integrationbatch_concurrent.rs- Batch processing examplestest_invalid_urls.rs- Error handling examples
Run examples with:
Benchmarks
The library includes comprehensive benchmarks:
Testing
Run the test suite:
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Development Setup
- Clone the repository
- Install dependencies:
cargo build - Run tests:
cargo test - Run benchmarks:
cargo bench - Format code:
cargo fmt - Run clippy:
cargo clippy -- -D warnings
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Uses scraper for HTML parsing
- Uses reqwest for HTTP requests
- Uses dashmap for concurrent caching
- Uses tokio for async runtime
Testing Websites for Rich Results
Google provides the Rich Results Analysis Tool to help you validate your website's tags.
Use this tool to ensure your website follows the conventions for optimal preview generation.