laravel-comments 0.2.1

Comments for Laravel Rust models: threaded replies, moderation, reactions, and events, without N+1 queries.
Documentation
//! The rule that gives a new comment its status.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::Status;

/// A comment about to be posted, as a moderation rule sees it.
#[derive(Debug, Clone)]
pub struct NewComment {
    /// The morph name of the model it belongs to, such as `post`.
    pub commentable_type: &'static str,
    /// The key of the model it belongs to.
    pub commentable_id: i64,
    /// The parent's key, for replies.
    pub parent_id: Option<i64>,
    /// The author's key, for comments by users.
    pub author_id: Option<i64>,
    /// The guest's email address, for comments by guests.
    pub guest_email: Option<String>,
    /// The text.
    pub body: String,
}

type RuleFuture = Pin<Box<dyn Future<Output = laravel::Result<Status>> + Send>>;

/// The application's moderation rule, registered with
/// [`CommentsServiceProvider::moderate_with`](crate::CommentsServiceProvider::moderate_with).
#[derive(Clone)]
pub(crate) struct ModerationRule(pub(crate) Option<Arc<dyn Fn(NewComment) -> RuleFuture + Send + Sync>>);

impl ModerationRule {
    pub(crate) fn new<F, Fut>(rule: F) -> Self
    where
        F: Fn(NewComment) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = laravel::Result<Status>> + Send + 'static,
    {
        Self(Some(Arc::new(move |comment| Box::pin(rule(comment)))))
    }
}