laravel-rust-devtools 0.1.0

Developer tools for Laravel for Rust applications: an app inspector and request debugger in a floating toolbar.
Documentation
//! The condition an application may put on who sees the developer tools.

#[cfg(debug_assertions)]
use std::pin::Pin;

use laravel::http::Request;

/// Who may see the developer tools, beyond what [`DevtoolsServiceProvider`] already checks.
///
/// The provider asks the gate for every request that would otherwise see them, in the `web`
/// middleware group, after the session has started: a gate may ask who is logged in. A gate that
/// says no, or fails, hides the toolbar and answers the developer tools' own pages with `404`; the
/// CSRF check may answer before it with `419`, and the router, for a method a route doesn't have,
/// with `405`, or `200` for `OPTIONS`, and `Allow`. It asks only those the provider lets in, on the application's
/// machine or with the key, so it hides what the developer tools hold, not that they are there.
///
/// ```
/// use laravel::http::Request;
/// use laravel_rust_devtools::{DevtoolsGate, DevtoolsServiceProvider};
///
/// /// Only those logged in.
/// struct LoggedIn;
///
/// impl DevtoolsGate for LoggedIn {
///     async fn allows(&self, request: &Request) -> laravel::Result<bool> {
///         request.authenticated().await
///     }
/// }
///
/// laravel::Application::configure().with_providers(|providers| {
///     providers.add(DevtoolsServiceProvider::new().with_gate(LoggedIn));
/// });
/// ```
///
/// [`DevtoolsServiceProvider`]: crate::DevtoolsServiceProvider
pub trait DevtoolsGate: Send + Sync + 'static {
    /// Whether the request may see the developer tools.
    ///
    /// # Errors
    ///
    /// An error, such as a database that can't be reached, counts as no.
    fn allows(&self, request: &Request) -> impl Future<Output = laravel::Result<bool>> + Send;
}

/// A [`DevtoolsGate`] behind a pointer, whatever its type.
#[cfg(debug_assertions)]
pub(crate) trait AnyGate: Send + Sync {
    fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>>;
}

#[cfg(debug_assertions)]
impl<G: DevtoolsGate> AnyGate for G {
    fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>> {
        Box::pin(DevtoolsGate::allows(self, request))
    }
}