|
| 1 | +# crates_io_og_image |
| 2 | + |
| 3 | +A Rust crate for generating Open Graph images for crates.io packages. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +`crates_io_og_image` is a specialized library for generating visually appealing Open Graph images for Rust crates. These images are designed to be displayed when crates.io links are shared on social media platforms, providing rich visual context about the crate including its name, description, authors, and key metrics. |
| 10 | + |
| 11 | +The generated images include: |
| 12 | + |
| 13 | +- Crate name and description |
| 14 | +- Tags/keywords |
| 15 | +- Author information with avatars (when available) |
| 16 | +- Key metrics (releases, latest version, license, lines of code, size) |
| 17 | +- Consistent crates.io branding |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- The [Typst](https://typst.app/) CLI must be installed and available in your `PATH` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Basic Example |
| 26 | + |
| 27 | +```rust |
| 28 | +use crates_io_og_image::{OgImageData, OgImageGenerator, OgImageAuthorData}; |
| 29 | + |
| 30 | +#[tokio::main] |
| 31 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 32 | + // Create a generator instance |
| 33 | + let generator = OgImageGenerator::default(); |
| 34 | + |
| 35 | + // Define the crate data |
| 36 | + let data = OgImageData { |
| 37 | + name: "example-crate", |
| 38 | + version: "v1.2.3", |
| 39 | + description: "An example crate for testing OpenGraph image generation", |
| 40 | + license: "MIT/Apache-2.0", |
| 41 | + tags: &["example", "testing", "og-image"], |
| 42 | + authors: &[ |
| 43 | + OgImageAuthorData::with_url( |
| 44 | + "Turbo87", |
| 45 | + "https://avatars.githubusercontent.com/u/141300", |
| 46 | + ), |
| 47 | + ], |
| 48 | + lines_of_code: Some(2000), |
| 49 | + crate_size: 75, |
| 50 | + releases: 5, |
| 51 | + }; |
| 52 | + |
| 53 | + // Generate the image |
| 54 | + let temp_file = generator.generate(data).await?; |
| 55 | + |
| 56 | + // The temp_file contains the path to the generated PNG image |
| 57 | + println!("Image generated at: {}", temp_file.path().display()); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +The path to the Typst CLI can be configured through the `TYPST_PATH` environment variables. |
| 66 | + |
| 67 | +## Development |
| 68 | + |
| 69 | +### Running Tests |
| 70 | + |
| 71 | +```bash |
| 72 | +cargo test |
| 73 | +``` |
| 74 | + |
| 75 | +Note that some tests require Typst to be installed and will be skipped if it's not available. |
| 76 | + |
| 77 | +### Example |
| 78 | + |
| 79 | +The crate includes an example that demonstrates how to generate an image: |
| 80 | + |
| 81 | +```bash |
| 82 | +cargo run --example test_generator |
| 83 | +``` |
| 84 | + |
| 85 | +This will generate a test image in the current directory. This will also test the avatar fetching functionality, which requires network access and isn't run as part of the automated tests. |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +Licensed under either of: |
| 90 | + |
| 91 | +- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 92 | +- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) |
| 93 | + |
| 94 | +at your option. |
0 commit comments