auditlog 0.1.0

Audit trail for your data models — an ORM-agnostic core with a pluggable, async sqlx backend (SQLite & Postgres).
Documentation
//! Error type for the crate.

use thiserror::Error;

/// Errors that can occur while recording or querying audits.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AuditError {
    /// An `audit_comment` was required for this action but none was supplied.
    ///
    /// Some actions can be configured to require an `audit_comment`; this error is raised when
    /// such a comment is mandatory but missing. For `update`
    /// and `destroy` (which are audited *before* the database write) you can use this error to
    /// abort the operation. For `create` (audited *after* the write) the row already exists, so
    /// prefer pre-checking with [`crate::Auditable::comment_required_for`].
    #[error("audit comment required for `{action}` but none was supplied")]
    CommentRequired {
        /// The action that required the comment.
        action: crate::Action,
    },

    /// `undo` was called on an audit with an unrecognized action string.
    #[error("invalid action given `{0}`")]
    InvalidAction(String),

    /// The persistence backend failed.
    #[error("audit backend error: {0}")]
    Backend(#[source] Box<dyn std::error::Error + Send + Sync>),

    /// (De)serialization of `audited_changes` failed.
    #[error("failed to (de)serialize audited_changes: {0}")]
    Serialization(#[source] serde_json::Error),
}

impl AuditError {
    /// Wrap an arbitrary backend error.
    pub fn backend<E>(err: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        AuditError::Backend(Box::new(err))
    }
}

/// Convenience alias used throughout the crate.
pub type Result<T> = std::result::Result<T, AuditError>;