laracards 0.1.0

Editorial social cards and blog covers for Laravel Rust: SVG templates rendered natively, measured text fitting and pluggable backgrounds.
Documentation
//! The contracts a card is made of: where its background comes from, how it is drawn, and where cards
//! come from.

use std::path::PathBuf;

use async_trait::async_trait;
use image::RgbaImage;

use crate::card::Card;
use crate::exceptions::RenderFailed;

/// Resolves to a local image file that gets embedded into the card.
///
/// Every driver returns the same thing, a path on disk, so a flat colour, an Unsplash photo and an
/// AI-generated PNG all travel through one code path.
#[async_trait]
pub trait Background: Send + Sync {
    /// Absolute path to a local image, or `None` for no background layer.
    ///
    /// # Errors
    ///
    /// Fails when the driver has to fetch the image and cannot.
    async fn resolve(&self) -> laravel::Result<Option<PathBuf>>;

    /// Stable identity of this background, so the manifest can detect changes.
    fn fingerprint(&self) -> String;
}

/// Turns a composed SVG into a raster image.
///
/// The PHP contract takes the path of an SVG file and writes a PNG. The native renderer has no process
/// to hand a file to, so it takes the SVG text and returns the pixels.
pub trait Renderer: Send + Sync {
    /// Renders `svg` at the given size.
    ///
    /// # Errors
    ///
    /// Fails with [`RenderFailed`] when the SVG cannot be parsed or drawn.
    fn render(&self, svg: &str, width: u32, height: u32) -> Result<RgbaImage, RenderFailed>;

    /// Whether the renderer can draw on this machine. The native renderer always can.
    fn available(&self) -> bool {
        true
    }
}

/// Maps a collection of content (blog posts, tools, cities) to cards.
///
/// Implement one per kind of content and register it on the provider, or with [`crate::source`] from
/// another package. The generate command walks every source it is given.
///
/// PHP returns an iterable, which can be a generator. Here the source returns the cards it has, and
/// `async` so it can read them from the database.
#[async_trait]
pub trait CardSource: Send + Sync {
    /// The cards of this source.
    ///
    /// # Errors
    ///
    /// Fails when the content cannot be read.
    async fn cards(&self) -> laravel::Result<Vec<Card>>;
}