use crate::{
App,
middleware::{
MwContext, Next,
make_fn::{make_mw, make_on, make_on_command},
},
types::{Message, Response},
};
use std::future::Future;
impl App {
pub fn wrap<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_mw(middleware));
self
}
pub fn wrap_notification<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options
.add_middleware(make_on(middleware, |msg| msg.is_notification()));
self
}
pub fn wrap_request<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options
.add_middleware(make_on(middleware, |msg| msg.is_request()));
self
}
pub fn wrap_response<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options
.add_middleware(make_on(middleware, |msg| msg.is_response()));
self
}
pub fn wrap_tools<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::tool::commands::CALL,
));
self
}
pub fn wrap_prompts<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::prompt::commands::GET,
));
self
}
pub fn wrap_resources<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::resource::commands::READ,
));
self
}
pub fn wrap_list_resources<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::resource::commands::LIST,
));
self
}
pub fn wrap_list_resource_templates<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::resource::commands::TEMPLATES_LIST,
));
self
}
pub fn wrap_list_tools<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::tool::commands::LIST,
));
self
}
pub fn wrap_list_prompts<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on_command(
middleware,
crate::types::prompt::commands::LIST,
));
self
}
pub fn wrap_init<F, R>(mut self, middleware: F) -> Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options
.add_middleware(make_on_command(middleware, crate::commands::INIT));
self
}
pub fn wrap_command<F, R>(&mut self, name: &'static str, middleware: F) -> &mut Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options
.add_middleware(make_on_command(middleware, name));
self
}
pub fn wrap_tool<F, R>(&mut self, name: &'static str, middleware: F) -> &mut Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on(middleware, move |msg| {
if let Message::Request(req) = msg {
req.method == crate::types::tool::commands::CALL
&& req
.params
.as_ref()
.is_some_and(|p| p.get("name").is_some_and(|n| n == name))
} else {
false
}
}));
self
}
pub fn wrap_prompt<F, R>(&mut self, name: &'static str, middleware: F) -> &mut Self
where
F: Fn(MwContext, Next) -> R + Clone + Send + Sync + 'static,
R: Future<Output = Response> + Send + 'static,
{
self.options.add_middleware(make_on(middleware, move |msg| {
if let Message::Request(req) = msg {
req.method == crate::types::prompt::commands::GET
&& req
.params
.as_ref()
.is_some_and(|p| p.get("name").is_some_and(|n| n == name))
} else {
false
}
}));
self
}
}