yourevalid 0.1.0

Validation type and trait. For internal use in polyproto-related crates, currently
Documentation
/*!
 * # `yourevalid`
 *
 * Validation type + trait used in sonata and, soon, polyproto-rs
*/
/// Module defining the [valid::Valid] type
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};

use chrono::{DateTime, Utc};
use sqlx::PgPool;

/// *Look, it's you! <3*
///
/// Wrapper type, signaling that the type `T` makes no guarantees about
/// validity itself, and that by calling [Verify::verify] on `T`
/// successfully, you can receive a version of `T` (`Self`), which is
/// guaranteed to be verified and valid.
///
/// New instances of this type can only be instantiated through the
/// [Validate] trait.
///
/// ## Defining "Verified and Valid"
///
/// *Wow, it's you again! <3*
///
/// - **"Verified"** is meant as in "Verified to be well-formed". A type
///   that is verified **must** exhibit all the acceptance criteria and none
///   of the rejection criteria as according to the specification document
///   or documentation of the type.
/// - **"Valid"** means that, at a given point in time, one can attest that
///   this specific instance of a type is cryptographically and contextually
///   fit/allowed to make the claims that this instance of the type is
///   making, and optionally that this instance of a type can be used to
///   validate other information (as would be the case with a signature
///   verifying/public key). **Most importantly,** the validity has only
///   been attested to for this specific point in time. It **must** be
///   carefully evaluated whether re-validation is in order to keep up those
///   guarantees of cryptographic and contextual fitness-for-purpose, if
///   this instance of the type is to be used to validate information made
///   at a different (earlier/later) point in time.
#[derive(Debug)]
pub struct Valid<T: Debug> {
    /// The type marked as valid
    pub t: T,
    /// The point in time at which the validity of this type has been
    /// checked and established. This is important information, since an
    /// ID-Cert could theoretically get manually invalidated just one second
    /// after a server has established its validity. Validity is therefore
    /// not an eternal property, but sensitive to the context of time.
    at: DateTime<Utc>,
}

impl<T: Debug> Valid<T> {
    /// The point in time at which the validity of this type has been
    /// checked and established. This is important information, since an
    /// ID-Cert could theoretically get manually invalidated just one second
    /// after a server has established its validity. Validity is therefore
    /// not an eternal property, but sensitive to the context of time.
    pub const fn validated_at(&self) -> &DateTime<Utc> {
        &self.at
    }
}

impl<T: Debug> Deref for Valid<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.t
    }
}

impl<T: Debug> DerefMut for Valid<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.t
    }
}

/// Implement this trait on types `T`, where validity is not a guaranteed
/// property.
///
/// ## Example
///
/// ID-Certs have a lifetime `not_valid_before` to `not_valid_after`. In
/// addition, ID-Certs can be manually revoked/invalidated. This means, that
/// the well-formedness of an ID-Cert says nothing about whether it is valid
/// at a certain point in time. The `Valid<T>` trait can help to establish
/// that the validity of a specific ID-Cert has been verified at a certain
/// point in time.
pub trait Validate<T: Debug> {
    /// An error type.
    type Error: std::error::Error;
    /// Validate the type `T` using the given validation_function.
    async fn validate<F>(
        maybe_db: Option<&PgPool>,
        to_validate: T,
        at_time: DateTime<Utc>,
        validation_function: F,
    ) -> Result<Valid<T>, Self::Error>
    where
        F: AsyncFnOnce(Option<&PgPool>, &T, &DateTime<Utc>) -> Result<(), Self::Error>,
    {
        validation_function(maybe_db, &to_validate, &at_time).await?;
        Ok(Valid {
            t: to_validate,
            at: at_time,
        })
    }
}