use crate::blueprint::conversions::raw_callable2registered_callable;
use crate::blueprint::reflection::RawCallable;
use crate::blueprint::router::MethodGuard;
use crate::blueprint::Blueprint;
use pavex_bp_schema::{Blueprint as BlueprintSchema, Callable, Component};
pub struct RegisteredRoute<'a> {
pub(crate) blueprint: &'a mut BlueprintSchema,
pub(crate) component_id: usize,
}
impl<'a> RegisteredRoute<'a> {
#[track_caller]
pub fn error_handler(mut self, error_handler: RawCallable) -> Self {
let callable = raw_callable2registered_callable(error_handler);
self.route().error_handler = Some(callable);
self
}
fn route(&mut self) -> &mut pavex_bp_schema::Route {
let component = &mut self.blueprint.components[self.component_id];
let Component::Route(c) = component else {
unreachable!("The component should be a route")
};
c
}
}
#[derive(Clone, Debug)]
pub struct Route {
pub(in crate::blueprint) method_guard: MethodGuard,
pub(in crate::blueprint) path: String,
pub(in crate::blueprint) callable: Callable,
pub(in crate::blueprint) error_handler: Option<Callable>,
}
impl Route {
#[track_caller]
pub fn new(method_guard: MethodGuard, path: &str, callable: RawCallable) -> Self {
Self {
callable: raw_callable2registered_callable(callable),
error_handler: None,
method_guard,
path: path.to_owned(),
}
}
#[track_caller]
pub fn error_handler(mut self, error_handler: RawCallable) -> Self {
self.error_handler = Some(raw_callable2registered_callable(error_handler));
self
}
pub fn register(self, bp: &mut Blueprint) -> RegisteredRoute {
bp.register_route(self)
}
}