auditlog 0.1.0

Audit trail for your data models — an ORM-agnostic core with a pluggable, async sqlx backend (SQLite & Postgres).
Documentation
//! A polymorphic record identifier.

use std::fmt;

use serde::{Deserialize, Serialize};

/// The identifier half of a polymorphic reference (e.g. `auditable_id`, `user_id`,
/// `associated_id`).
///
/// Ids are stored as TEXT so that integer and UUID primary keys both work uniformly. Because this
/// crate is ORM-agnostic and cannot know your primary-key type at compile time, ids are kept as
/// their string representation. `From` is implemented for the
/// common integer types, [`uuid::Uuid`], `String`, and `&str`, so passing an id is ergonomic:
///
/// ```
/// use auditlog::AuditId;
/// let a: AuditId = 42_i64.into();
/// let b: AuditId = "user-7".into();
/// assert_eq!(a.as_str(), "42");
/// assert_eq!(b.as_str(), "user-7");
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AuditId(String);

impl AuditId {
    /// Build an id from anything string-like.
    pub fn new(value: impl Into<String>) -> Self {
        AuditId(value.into())
    }

    /// Borrow the string representation.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Consume into the underlying `String`.
    pub fn into_string(self) -> String {
        self.0
    }
}

impl fmt::Display for AuditId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<String> for AuditId {
    fn from(value: String) -> Self {
        AuditId(value)
    }
}

impl From<&str> for AuditId {
    fn from(value: &str) -> Self {
        AuditId(value.to_owned())
    }
}

impl From<&String> for AuditId {
    fn from(value: &String) -> Self {
        AuditId(value.clone())
    }
}

impl From<uuid::Uuid> for AuditId {
    fn from(value: uuid::Uuid) -> Self {
        AuditId(value.to_string())
    }
}

macro_rules! impl_from_int {
    ($($t:ty),*) => {
        $(
            impl From<$t> for AuditId {
                fn from(value: $t) -> Self {
                    AuditId(value.to_string())
                }
            }
        )*
    };
}

impl_from_int!(
    i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
);