carbon_core/
account_deletion.rs

1//! Provides structures and traits for handling account deletion events within
2//! the pipeline.
3//!
4//! This module defines the `AccountDeletionPipe` struct and
5//! `AccountDeletionPipes` trait, which allow for the processing of account
6//! deletion events as they occur in the pipeline. By implementing
7//! `AccountDeletionPipes`, you can create custom behavior for managing
8//! the deletion of accounts and associated resources, integrating with metrics
9//! for monitoring.
10
11use {
12    crate::{
13        datasource::AccountDeletion, error::CarbonResult, filter::Filter,
14        metrics::MetricsCollection, processor::Processor,
15    },
16    async_trait::async_trait,
17    std::sync::Arc,
18};
19
20/// A processing pipe for handling account deletions.
21///
22/// The `AccountDeletionPipe` struct encapsulates the logic needed to process
23/// account deletion events within the pipeline. It uses a `Processor` to define
24/// how account deletions are managed, allowing for customizable handling based
25/// on the needs of the application.
26///
27/// ## Functionality
28///
29/// This struct processes an `AccountDeletion` event by passing it through a
30/// user-defined `Processor`. The processor is responsible for managing the
31/// specific logic of the account deletion event, such as cleaning up resources
32/// or updating other parts of the system. The `AccountDeletionPipe` also
33/// integrates with `Metrics`, enabling the tracking and monitoring of deletion
34/// events.
35///
36/// # Example
37///
38/// ```ignore
39/// use carbon_core::error::CarbonResult;
40/// use carbon_core::metrics::MetricsCollection;
41/// use carbon_core::datasource::AccountDeletion;
42/// use carbon_core::processor::Processor;
43/// use async_trait::async_trait;
44/// use std::sync::Arc;
45///
46/// struct MyAccountDeletionProcessor;
47///
48/// #[async_trait]
49/// impl Processor for MyAccountDeletionProcessor {
50///     async fn process(
51///         &self,
52///         account_deletion: AccountDeletion,
53///         metrics: Arc<MetricsCollection>,
54///     ) -> CarbonResult<()> {
55///         // Custom deletion logic
56///         Ok(())
57///     }
58/// }
59/// ```
60///
61/// ## Fields
62///
63/// - `processor`: A boxed `Processor` that handles the specific logic of
64///   processing an account deletion event.
65/// - `filters`: A collection of filters that determine which account deletion
66///   events should be processed. Each filter in this collection is applied to
67///   incoming account deletion events, and only events that pass all filters
68///   (return `true`) will be processed. If this collection is empty, all
69///   events are processed.
70///
71/// # Notes
72///
73/// - Ensure that the `Processor` implementation provided is designed to handle
74///   account deletions.
75/// - This struct is typically used within the broader pipeline structure for
76///   managing updates.
77pub struct AccountDeletionPipe {
78    pub processor: Box<dyn Processor<InputType = AccountDeletion> + Send + Sync>,
79    pub filters: Vec<Box<dyn Filter + Send + Sync + 'static>>,
80}
81
82/// An async trait for processing account deletions.
83///
84/// The `AccountDeletionPipes` trait allows for processing of account deletions.
85///
86/// # Required Methods
87///
88/// - `run`: Processes an account deletion event and tracks the operation with
89///   metrics.
90/// - `filters`: Returns a reference to the filters associated with this pipe,
91///   which are used by the pipeline to determine which account deletion events
92///   should be processed.
93#[async_trait]
94pub trait AccountDeletionPipes: Send + Sync {
95    async fn run(
96        &mut self,
97        account_deletion: AccountDeletion,
98        metrics: Arc<MetricsCollection>,
99    ) -> CarbonResult<()>;
100
101    fn filters(&self) -> &Vec<Box<dyn Filter + Send + Sync + 'static>>;
102}
103
104#[async_trait]
105impl AccountDeletionPipes for AccountDeletionPipe {
106    async fn run(
107        &mut self,
108        account_deletion: AccountDeletion,
109        metrics: Arc<MetricsCollection>,
110    ) -> CarbonResult<()> {
111        log::trace!(
112            "AccountDeletionPipe::run(account_deletion: {:?}, metrics)",
113            account_deletion,
114        );
115
116        self.processor.process(account_deletion, metrics).await?;
117
118        Ok(())
119    }
120
121    fn filters(&self) -> &Vec<Box<dyn Filter + Send + Sync + 'static>> {
122        &self.filters
123    }
124}