use std::{any::Any, future::Future};
use derive_where::derive_where;
use topcoat::{Result, context::Cx};
mod builtin;
use super::{Mounted, Resource, WriteError};
use crate::{
BoxFuture,
form::{FieldError, FieldErrors, FormInput, FormValues},
naming::{derived_id, sentence_case},
policy::Ability,
schema::Schema,
};
pub(crate) const RESERVED_KEYS: [&str; 3] = [crate::csrf::FIELD_NAME, "confirm", "ids"];
pub trait Action<R: Resource>: 'static {
type Input: FormInput;
const NAME: &'static str;
const PLACES: Places = Places::ALL;
const CONFIRM: bool = false;
fn label(cx: &Cx) -> String {
crate::Text::derived(derived_id("action", Self::NAME), sentence_case(Self::NAME))
.localize(cx)
}
fn can_run(_cx: &Cx, _record: &R::Model) -> bool {
true
}
fn validate_input(_cx: &Cx, _input: &Self::Input) -> FieldErrors {
FieldErrors::new()
}
fn run(
cx: &Cx,
records: &[R::Model],
input: Self::Input,
ex: &mut dyn toasty::Executor,
) -> impl Future<Output = Result<crate::Outcome, WriteError>> + Send;
fn success(cx: &Cx, count: usize) -> String {
crate::t!(
cx,
"tablo-action-on-records",
action = Self::label(cx),
count = count
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Places(u8);
impl Places {
pub const ROW: Self = Self(1);
pub const DETAIL: Self = Self(1 << 1);
pub const EDIT: Self = Self(1 << 2);
pub const BULK: Self = Self(1 << 3);
pub const RECORD: Self = Self(Self::ROW.0 | Self::DETAIL.0 | Self::EDIT.0);
pub const ALL: Self = Self(Self::RECORD.0 | Self::BULK.0);
#[must_use]
pub const fn with(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn intersects(self, other: Self) -> bool {
self.0 & other.0 != 0
}
}
impl<M> Ability<'_, M> {
pub fn is_action<A, R>(self) -> bool
where
A: Action<R>,
R: Resource<Model = M>,
{
matches!(self, Ability::RunAny { action } | Ability::Run { action, .. } if action == A::NAME)
}
}
impl<M> crate::policy::Abilities<M> {
#[must_use]
pub fn run<A, R>(mut self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static) -> Self
where
A: Action<R>,
R: Resource<Model = M>,
{
self.set_action_record(A::NAME, Box::new(check));
self
}
#[must_use]
pub fn run_any<A, R>(mut self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static) -> Self
where
A: Action<R>,
R: Resource<Model = M>,
{
self.set_action_any(A::NAME, Box::new(check));
self
}
}
pub(crate) type RunAction<R> = for<'a> fn(
&'a Cx,
&'a [<R as Resource>::Model],
ErasedInput,
&'a mut dyn toasty::Executor,
) -> BoxFuture<'a, Result<crate::Outcome, WriteError>>;
pub(crate) type ErasedInput = Box<dyn Any + Send>;
pub(crate) type InputResult = std::result::Result<ErasedInput, Vec<FieldError>>;
#[derive(Clone, Copy)]
pub(crate) struct InputSpec {
pub(crate) schema: fn() -> Schema,
pub(crate) takes_input: bool,
pub(crate) parse: fn(&Cx, &FormValues) -> InputResult,
}
impl InputSpec {
pub(crate) fn of<I: FormInput>(parse: fn(&Cx, &FormValues) -> InputResult) -> Self {
Self {
schema: I::schema,
takes_input: !I::schema().is_empty(),
parse,
}
}
pub(crate) fn none() -> Self {
Self {
schema: Schema::empty,
takes_input: false,
parse: parse_nothing,
}
}
}
pub(crate) struct Actions<R: Resource> {
entries: Vec<ActionEntry<R>>,
}
impl<R: Resource> Default for Actions<R> {
fn default() -> Self {
Self {
entries: Vec::new(),
}
}
}
impl<R: Resource> std::fmt::Debug for Actions<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list()
.entries(self.entries.iter().map(|e| e.name))
.finish()
}
}
impl<R: Resource> Actions<R> {
pub(crate) fn add<A: Action<R>>(mut self) -> Self {
const {
assert!(
crate::declaration::segment_fault(A::NAME).is_none(),
"`Action::NAME` must be a single path segment"
);
}
self.entries.push(ActionEntry {
name: A::NAME,
label: A::label,
places: A::PLACES,
resource_wide: Ability::RunAny { action: A::NAME },
can_run: can_run_erased::<R, A>,
input: InputSpec::of::<A::Input>(parse_input_erased::<R, A>),
run: run_erased::<R, A>,
success: A::success,
change: super::acted::<R, A>,
failure: "tablo-failed-run",
confirm: A::CONFIRM,
trashed: super::Trashed::Without,
removes: false,
prompt: "tablo-confirm-run",
});
self
}
pub(crate) fn add_trash_actions(&mut self) {
self.entries.push(ActionEntry::restore());
self.entries.push(ActionEntry::force_delete());
}
pub(crate) fn entries(&self) -> &[ActionEntry<R>] {
&self.entries
}
pub(crate) fn find(&self, name: &str) -> Option<&ActionEntry<R>> {
self.entries.iter().find(|e| e.name == name)
}
}
#[derive_where(Clone, Copy)]
pub(crate) struct ActionEntry<R: Resource> {
pub(crate) name: &'static str,
pub(crate) label: fn(&Cx) -> String,
pub(crate) places: Places,
pub(crate) resource_wide: Ability<'static, R::Model>,
pub(crate) can_run: fn(&Mounted<R>, &Cx, &R::Model) -> bool,
pub(crate) input: InputSpec,
pub(crate) run: RunAction<R>,
pub(crate) success: fn(&Cx, usize) -> String,
pub(crate) change: fn(Vec<R::Model>) -> super::Change<R::Model>,
pub(crate) failure: &'static str,
pub(crate) confirm: bool,
pub(crate) trashed: super::Trashed,
pub(crate) removes: bool,
pub(crate) prompt: &'static str,
}
impl<R: Resource> ActionEntry<R> {
pub(crate) fn allows(&self, resource: &Mounted<R>, cx: &Cx, record: &R::Model) -> bool {
self.trashed.admits(resource.is_trashed(record)) && (self.can_run)(resource, cx, record)
}
}
fn can_run_erased<R: Resource, A: Action<R>>(
resource: &Mounted<R>,
cx: &Cx,
record: &R::Model,
) -> bool {
resource.can(
cx,
Ability::Run {
action: A::NAME,
record,
},
) && A::can_run(cx, record)
}
fn parse_input_erased<R: Resource, A: Action<R>>(cx: &Cx, values: &FormValues) -> InputResult {
parse_validated(cx, values, A::validate_input)
}
pub(crate) fn parse_validated<I: FormInput>(
cx: &Cx,
values: &FormValues,
validate: fn(&Cx, &I) -> FieldErrors,
) -> InputResult {
let input = I::parse(cx, values)?;
let refused: Vec<FieldError> = validate(cx, &input).into_iter().collect();
if !refused.is_empty() {
return Err(refused);
}
Ok(Box::new(input))
}
fn parse_nothing(_cx: &Cx, _values: &FormValues) -> InputResult {
Ok(Box::new(()))
}
fn run_erased<'a, R: Resource, A: Action<R>>(
cx: &'a Cx,
records: &'a [R::Model],
input: ErasedInput,
ex: &'a mut dyn toasty::Executor,
) -> BoxFuture<'a, Result<crate::Outcome, WriteError>> {
let input = *input
.downcast::<A::Input>()
.expect("the pipeline parses the input `run` takes");
Box::pin(A::run(cx, records, input, ex))
}