laracards 0.1.0

Editorial social cards and blog covers for Laravel Rust: SVG templates rendered natively, measured text fitting and pluggable backgrounds.
Documentation
use std::sync::{Arc, Mutex, PoisonError};

use crate::contracts::CardSource;

/// The card sources `cards:generate` walks, by name.
///
/// In PHP they are the `sources` key of the config, a name and a class. A source is code, so here they
/// are registered on the provider, or with [`crate::source`] from another package's provider.
#[derive(Default)]
pub struct CardSources {
    sources: Mutex<Vec<(String, Arc<dyn CardSource>)>>,
}

impl CardSources {
    pub(crate) fn new(sources: Vec<(String, Arc<dyn CardSource>)>) -> Self {
        Self {
            sources: Mutex::new(sources),
        }
    }

    /// Register a source. A source with the same name is replaced.
    pub fn add(&self, name: impl Into<String>, source: impl CardSource + 'static) {
        let name = name.into();
        let mut sources = self.sources.lock().unwrap_or_else(PoisonError::into_inner);

        sources.retain(|(existing, _)| *existing != name);
        sources.push((name, Arc::new(source)));
    }

    /// Every source, in the order it was registered.
    pub fn all(&self) -> Vec<(String, Arc<dyn CardSource>)> {
        self.sources.lock().unwrap_or_else(PoisonError::into_inner).clone()
    }
}