codeuchain/core/
middleware.rs

1/*!
2Middleware Trait: The Enhancement Layer Core
3
4The Middleware trait defines optional enhancement hooks.
5Trait with default implementations—implementations can override any/all methods.
6*/
7
8use async_trait::async_trait;
9use crate::core::context::Context;
10use crate::core::link::LegacyLink;
11
12/// Gentle enhancer—optional hooks with forgiving defaults.
13/// Trait that middleware implementations can implement.
14/// Implementors can override any combination of before(), after(), and on_error().
15#[async_trait]
16pub trait Middleware: Send + Sync {
17    /// With selfless optionality, do nothing by default.
18    async fn before(&self, _link: Option<&dyn LegacyLink>, _ctx: &Context) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
19        Ok(())
20    }
21
22    /// Forgiving default.
23    async fn after(&self, _link: Option<&dyn LegacyLink>, _ctx: &Context) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
24        Ok(())
25    }
26
27    /// Compassionate error handling.
28    async fn on_error(&self, _link: Option<&dyn LegacyLink>, _error: &Box<dyn std::error::Error + Send + Sync>, _ctx: &Context) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
29        Ok(())
30    }
31}