laracaptcha 0.1.0

Driver-based captcha for Laravel Rust: Cloudflare Turnstile and Google reCAPTCHA v2 and v3 behind one API.
Documentation
use laravel::config::{Config, Env};
use laravel::container::Registrar;
use laravel::foundation::ServiceProvider;

use crate::contracts::CaptchaDriver;
use crate::manager::CaptchaManager;

/// Wires Laracaptcha into the application: the configuration, the manager and the
/// `laracaptcha_widget()` function in the views.
///
/// ```ignore
/// Application::configure().with_providers(|providers| {
///     providers.add(LaracaptchaServiceProvider::new());
/// })
/// ```
#[derive(Clone, Default)]
pub struct LaracaptchaServiceProvider {
    manager: CaptchaManager,
}

impl LaracaptchaServiceProvider {
    /// A provider with the three drivers that ship with the package.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a driver of your own under a name, the way PHP's `Captcha::extend('name', fn () =>
    /// new MyDriver($config))` does. The closure decides where the credentials come from.
    #[must_use]
    pub fn extend<D: CaptchaDriver + 'static>(
        self,
        name: impl Into<String>,
        creator: impl Fn() -> D + Send + Sync + 'static,
    ) -> Self {
        self.manager.extend(name, creator);

        self
    }
}

impl ServiceProvider for LaracaptchaServiceProvider {
    fn config(&self, config: &mut Config, env: &Env) {
        config.discover(env!("CARGO_CRATE_NAME"), env);
    }

    fn register(&self, app: &mut Registrar) {
        app.instance(self.manager.clone());

        app.discover(env!("CARGO_CRATE_NAME"));

        #[cfg(feature = "minijinja")]
        {
            let manager = self.manager.clone();

            app.views()
                .minijinja(move |environment| crate::views::register(environment, manager.clone()));
        }
    }
}