Skip to main content

outbox_core/idempotency/
strategy.rs

1use crate::config::IdempotencyStrategy;
2use crate::model::Event;
3use serde::Serialize;
4use std::fmt::Debug;
5
6impl<P> IdempotencyStrategy<P>
7where
8    P: Debug + Clone + Serialize,
9{
10    /// Invokes the idempotency strategy to generate or retrieve a token.
11    ///
12    /// # Panics
13    ///
14    /// Panics if the strategy is set to `Custom`, but the provided `get_event`
15    /// closure returns `None`.
16    pub fn invoke<F>(&self, provided_token: Option<String>, get_event: F) -> Option<String>
17    where
18        F: FnOnce() -> Option<Event<P>>,
19    {
20        match self {
21            IdempotencyStrategy::Provided => provided_token,
22            IdempotencyStrategy::Custom(f) => {
23                let event = get_event().expect("Strategy is Custom, but no Event context provided");
24                Some(f(&event))
25            }
26            IdempotencyStrategy::Uuid => Some(uuid::Uuid::now_v7().to_string()),
27            // IdempotencyStrategy::HashPayload => {
28            //     Some("hash_payload".to_string())
29            // }
30            IdempotencyStrategy::None => None,
31        }
32    }
33}