a3s-boot 0.1.0

Adapter-first modular Rust web framework for A3S inspired by Nest.js
Documentation
use crate::{ExceptionFilter, Guard, HttpMethod, Interceptor, Pipe, Result};
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::routing::handler::RouteHandler;
use crate::routing::path::{
    match_path_params, match_path_shape, route_param_names, route_shape_key, validate_route_path,
};

/// A framework-neutral route definition.
#[derive(Clone)]
pub struct RouteDefinition {
    pub(super) method: HttpMethod,
    pub(super) path: String,
    pub(super) handler: Arc<dyn RouteHandler>,
    pub(super) pipes: Vec<Arc<dyn Pipe>>,
    pub(super) guards: Vec<Arc<dyn Guard>>,
    pub(super) interceptors: Vec<Arc<dyn Interceptor>>,
    pub(super) filters: Vec<Arc<dyn ExceptionFilter>>,
    pub(super) module_name: Option<String>,
    pub(super) controller_prefix: Option<String>,
}

impl RouteDefinition {
    pub fn new<H>(method: HttpMethod, path: impl Into<String>, handler: H) -> Result<Self>
    where
        H: RouteHandler,
    {
        let path = path.into();
        validate_route_path(&path)?;
        Ok(Self {
            method,
            path,
            handler: Arc::new(handler),
            pipes: Vec::new(),
            guards: Vec::new(),
            interceptors: Vec::new(),
            filters: Vec::new(),
            module_name: None,
            controller_prefix: None,
        })
    }

    pub fn method(&self) -> HttpMethod {
        self.method
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn path_shape(&self) -> String {
        route_shape_key(&self.path)
    }

    pub fn path_param_names(&self) -> Vec<&str> {
        route_param_names(&self.path)
    }

    pub fn matches_path(&self, path: &str) -> bool {
        match_path_shape(&self.path, path)
    }

    pub fn path_params(&self, path: &str) -> Result<Option<BTreeMap<String, String>>> {
        match_path_params(&self.path, path)
    }

    pub fn module_name(&self) -> Option<&str> {
        self.module_name.as_deref()
    }

    pub fn controller_prefix(&self) -> Option<&str> {
        self.controller_prefix.as_deref()
    }

    pub fn handler(&self) -> Arc<dyn RouteHandler> {
        Arc::clone(&self.handler)
    }
}