correlate 0.3.0

correlate is a standalone server that listens for Stripe webhook events and sends notification emails about successful orders.
use rocket::serde::Deserialize;
use validator::Validate;

use super::{checkout::StripeCheckoutSession, date::StripeDate};

/// Represents the different types of data that can be contained in a Stripe event.
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
#[serde(untagged)]
pub enum StripeEventData {
    /// Represents a Stripe Checkout Session object event.
    /// Used for payment completion, session expiration, etc.
    StripeCheckoutObject { object: StripeCheckoutSession },
}

/// Custom implementation of the Validate trait for StripeEventData.
impl Validate for StripeEventData {
    /// Validates the inner data of each enum variant.
    fn validate(&self) -> Result<(), validator::ValidationErrors> {
        match self {
            StripeEventData::StripeCheckoutObject { object } => object.validate(),
        }
    }
}

/// Represents a webhook event received from Stripe.
#[derive(Debug, Deserialize, Validate)]
#[serde(crate = "rocket::serde")]
pub struct StripeEvent<'d> {
    /// The unique identifier for the event.
    #[validate(contains(pattern = "evt_"))]
    pub id: &'d str,

    /// The timestamp when the event was created at Stripe.
    pub created: StripeDate,

    /// The actual event data, which can be of different types.
    /// The specific type is determined during deserialization.
    #[validate(nested)]
    pub data: StripeEventData,
}