laravel-comments 0.2.1

Comments for Laravel Rust models: threaded replies, moderation, reactions, and events, without N+1 queries.
Documentation
//! The package's errors.

use std::error::Error;
use std::fmt;

use laravel::errors::Status;

/// An operation on a comment that can't be done.
#[derive(Debug, Clone, PartialEq, Eq, Status)]
#[status(422)]
#[non_exhaustive]
pub enum CommentError {
    /// The text is empty or only whitespace.
    EmptyBody,
    /// The text is longer than `COMMENTS_MAX_LENGTH`.
    BodyTooLong {
        /// The most characters allowed.
        max: usize,
    },
    /// A guest's name or email address is missing.
    InvalidGuest,
    /// Replies are turned off: `COMMENTS_MAX_DEPTH` is `0`.
    RepliesDisabled,
    /// The comment was removed, so it can't get replies, edits, or reactions.
    Deleted {
        /// The comment's key.
        id: i64,
    },
    /// The comment isn't approved, so it can't get replies or reactions.
    NotApproved {
        /// The comment's key.
        id: i64,
    },
    /// The reaction isn't one of `COMMENTS_REACTIONS`.
    UnknownReaction {
        /// The reaction asked for.
        reaction: String,
    },
    /// The comment is no longer in the table.
    NotFound {
        /// The comment's key.
        id: i64,
    },
}

impl fmt::Display for CommentError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyBody => f.write_str("A comment can't be empty."),
            Self::BodyTooLong { max } => write!(f, "A comment can't be longer than {max} characters."),
            Self::InvalidGuest => f.write_str("A guest's comment needs a name and an email address."),
            Self::RepliesDisabled => f.write_str("Replies are turned off."),
            Self::Deleted { id } => write!(f, "The comment `{id}` was removed."),
            Self::NotApproved { id } => write!(f, "The comment `{id}` isn't approved."),
            Self::UnknownReaction { reaction } => write!(f, "`{reaction}` isn't an allowed reaction."),
            Self::NotFound { id } => write!(f, "The comment `{id}` no longer exists."),
        }
    }
}

impl Error for CommentError {}