---
to: "src/middleware/pattern_{{ index }}.rs"
vars:
  index: 17
  middleware: "middleware_pattern_17"
---

use std::future::Future;
use std::pin::Pin;

pub type Middleware17Fn = Box<
    dyn Fn(String) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send>> + Send + Sync,
>;

/// Middleware pattern 17 for request/response handling
pub struct MiddlewarePattern17 {
    handler: Middleware17Fn,
}

impl MiddlewarePattern17 {
    pub fn new<F>(handler: F) -> Self
    where
        F: Fn(String) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send>> + Send + Sync + 'static,
    {
        Self {
            handler: Box::new(handler),
        }
    }

    pub async fn handle(&self, input: String) -> Result<String, String> {
        (self.handler)(input).await
    }
}
