use crate::error::RoutePathError;
use serde_json::Value;
use super::join_paths;
pub type OpenApiSchemaRegistrar =
fn(&mut Vec<(String, Value)>) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[derive(Clone, Debug)]
pub struct RouteMetadata {
method: &'static str,
path: &'static str,
summary: Option<&'static str>,
tags: &'static [&'static str],
response_status: Option<http::StatusCode>,
request_schema: Option<&'static str>,
response_schema: Option<&'static str>,
request_schema_registrar: Option<OpenApiSchemaRegistrar>,
response_schema_registrar: Option<OpenApiSchemaRegistrar>,
guards: &'static [&'static str],
pipes: &'static [&'static str],
validates: bool,
}
impl RouteMetadata {
pub const fn new(method: &'static str, path: &'static str) -> Self {
Self {
method,
path,
summary: None,
tags: &[],
response_status: None,
request_schema: None,
response_schema: None,
request_schema_registrar: None,
response_schema_registrar: None,
guards: &[],
pipes: &[],
validates: false,
}
}
pub const fn with_summary(
method: &'static str,
path: &'static str,
summary: &'static str,
) -> Self {
Self {
method,
path,
summary: Some(summary),
tags: &[],
response_status: None,
request_schema: None,
response_schema: None,
request_schema_registrar: None,
response_schema_registrar: None,
guards: &[],
pipes: &[],
validates: false,
}
}
pub const fn with_annotations(
method: &'static str,
path: &'static str,
summary: Option<&'static str>,
guards: &'static [&'static str],
pipes: &'static [&'static str],
validates: bool,
) -> Self {
Self {
method,
path,
summary,
tags: &[],
response_status: None,
request_schema: None,
response_schema: None,
request_schema_registrar: None,
response_schema_registrar: None,
guards,
pipes,
validates,
}
}
pub const fn with_openapi_annotations(
method: &'static str,
path: &'static str,
summary: Option<&'static str>,
tags: &'static [&'static str],
guards: &'static [&'static str],
pipes: &'static [&'static str],
validates: bool,
) -> Self {
Self {
method,
path,
summary,
tags,
response_status: None,
request_schema: None,
response_schema: None,
request_schema_registrar: None,
response_schema_registrar: None,
guards,
pipes,
validates,
}
}
pub const fn with_openapi_schemas(
mut self,
request_schema: Option<&'static str>,
response_schema: Option<&'static str>,
) -> Self {
self.request_schema = request_schema;
self.response_schema = response_schema;
self
}
pub fn with_openapi_schema_registrars(
mut self,
request_schema: Option<OpenApiSchemaRegistrar>,
response_schema: Option<OpenApiSchemaRegistrar>,
) -> Self {
self.request_schema_registrar = request_schema;
self.response_schema_registrar = response_schema;
self
}
pub const fn with_openapi_status(mut self, response_status: Option<http::StatusCode>) -> Self {
self.response_status = response_status;
self
}
pub const fn method(&self) -> &'static str {
self.method
}
pub const fn path(&self) -> &'static str {
self.path
}
pub const fn summary(&self) -> Option<&'static str> {
self.summary
}
pub const fn tags(&self) -> &'static [&'static str] {
self.tags
}
pub const fn response_status(&self) -> Option<http::StatusCode> {
self.response_status
}
pub const fn request_schema(&self) -> Option<&'static str> {
self.request_schema
}
pub const fn response_schema(&self) -> Option<&'static str> {
self.response_schema
}
pub const fn request_schema_registrar(&self) -> Option<OpenApiSchemaRegistrar> {
self.request_schema_registrar
}
pub const fn response_schema_registrar(&self) -> Option<OpenApiSchemaRegistrar> {
self.response_schema_registrar
}
pub const fn guards(&self) -> &'static [&'static str] {
self.guards
}
pub const fn pipes(&self) -> &'static [&'static str] {
self.pipes
}
pub const fn validates(&self) -> bool {
self.validates
}
pub fn full_path(&self, controller_prefix: &str) -> String {
self.try_full_path(controller_prefix)
.unwrap_or_else(|error| panic!("{error}"))
}
pub fn try_full_path(&self, controller_prefix: &str) -> Result<String, RoutePathError> {
join_paths(controller_prefix, self.path)
}
}