use super::{
ActionSpec, AddonName, AddonSpec, Fields, ModuleName, ModuleSpec, TableName, TableSpec,
ViewSpec,
};
use crate::action::DynAction;
use crate::action::{Action as BusinessAction, TypedAction};
use std::sync::Arc;
#[derive(Default)]
pub struct Actions(Vec<(ActionSpec, Arc<dyn DynAction>)>);
impl Actions {
pub const fn new() -> Self {
Self(Vec::new())
}
pub fn action<A>(mut self, spec: ActionSpec, handler: A) -> Self
where
A: DynAction,
{
self.0.push((spec, Arc::new(handler)));
self
}
pub fn native<A>(mut self, handler: A) -> Self
where
A: BusinessAction + TypedAction,
{
let name = handler.name();
let path = if handler.path().is_empty() {
format!("/{name}")
} else {
handler.path().to_string()
};
let permissions = handler
.permissions()
.iter()
.map(|permission| permission.name().to_string())
.collect::<Vec<_>>();
let mut spec = ActionSpec::new(
super::ActionName::__from_validated_literal(name),
super::RouteSpec::new(handler.http_method(), path, name),
)
.display_name(handler.display_name())
.description(handler.description())
.permissions(permissions, handler.permission_mode())
.public(handler.is_public())
.response_kind(handler.response_kind())
.success_status(handler.success_status())
.params(<A as BusinessAction>::params());
if let Some(multipart) = handler.multipart_spec() {
spec = spec.multipart(multipart);
}
for call in handler.calls() {
spec = spec.calls(call);
}
self.0.push((spec, Arc::new(handler)));
self
}
pub(crate) fn into_vec(self) -> Vec<(ActionSpec, Arc<dyn DynAction>)> {
self.0
}
}
impl std::fmt::Debug for Actions {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Actions")
.field("len", &self.0.len())
.finish()
}
}
impl ModuleSpec {
pub fn native_action<A>(self, handler: A) -> Self
where
A: BusinessAction + TypedAction,
{
let mut values = Actions::new().native(handler).into_vec();
let (mut spec, handler) = values.remove(0);
if spec.route.operation_id == spec.name.as_str() {
spec.route.operation_id = format!("{}.{}", self.name, spec.name);
}
self.dyn_action(spec, handler)
}
}
#[derive(Debug, Clone, Default)]
pub struct Modules(Vec<ModuleSpec>);
impl Modules {
pub const fn new() -> Self {
Self(Vec::new())
}
pub fn module<M>(mut self, module: M) -> Self
where
M: Module,
{
self.0.push(module.into_spec());
self
}
pub(crate) fn into_vec(self) -> Vec<ModuleSpec> {
self.0
}
}
pub trait Module: Sized {
fn name(&self) -> ModuleName;
fn table(&self) -> Option<TableName> {
None
}
fn fields(&self) -> Fields {
Fields::new()
}
fn configure_table(&self, table: TableSpec) -> TableSpec {
table
}
fn actions(&self) -> Actions {
Actions::new()
}
fn views(&self) -> Vec<ViewSpec> {
Vec::new()
}
fn into_spec(self) -> ModuleSpec {
let module_name = self.name();
let mut spec = ModuleSpec::new(module_name.clone());
if let Some(table) = self.table() {
let table = TableSpec::new(table).fields(self.fields());
spec = spec.table(self.configure_table(table));
}
for (mut action, handler) in self.actions().into_vec() {
if action.route.operation_id == action.name.as_str() {
action.route.operation_id = format!("{}.{}", module_name, action.name);
}
spec = spec.dyn_action(action, handler);
}
for view in self.views() {
spec = spec.view(view);
}
spec
}
}
pub trait Addon: Sized {
fn name(&self) -> AddonName;
fn modules(&self) -> Modules;
fn dependencies(&self) -> Vec<AddonName> {
Vec::new()
}
fn into_spec(self) -> AddonSpec {
let mut spec = AddonSpec::new(self.name());
for dependency in self.dependencies() {
spec = spec.depends_on(dependency);
}
for module in self.modules().into_vec() {
spec = spec.module(module);
}
spec
}
}
impl<T> From<T> for AddonSpec
where
T: Addon,
{
fn from(value: T) -> Self {
value.into_spec()
}
}