use std::{future::Future, pin::Pin};
use topcoat::{Result, context::Cx};
use super::{Mounted, Resource};
use crate::policy::Ability;
pub trait Action<R: Resource>: 'static {
const NAME: &'static str;
const ROW: bool = true;
const BULK: bool = true;
const CONFIRM: bool = false;
fn label(cx: &Cx) -> String;
fn can_run(_cx: &Cx, _record: &R::Model) -> bool {
true
}
fn run(
cx: &Cx,
records: &[R::Model],
ex: &mut dyn toasty::Executor,
) -> impl Future<Output = Result<()>> + Send;
fn success(cx: &Cx, count: usize) -> String {
let noun = if count == 1 { "record" } else { "records" };
format!("{}: {count} {noun}", Self::label(cx))
}
}
pub(crate) type ActionFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
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,
row: A::ROW,
bulk: A::BULK,
resource_wide: Ability::ViewAny,
can_run: can_run_erased::<R, A>,
run: run_erased::<R, A>,
success: A::success,
acted: super::Committed::acted::<R, A>,
failure: "run the action",
confirm: A::CONFIRM,
});
self
}
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)
}
}
pub(crate) struct ActionEntry<R: Resource> {
pub(crate) name: &'static str,
pub(crate) label: fn(&Cx) -> String,
pub(crate) row: bool,
pub(crate) bulk: bool,
pub(crate) resource_wide: Ability<'static, R::Model>,
pub(crate) can_run: fn(&Mounted<R>, &Cx, &R::Model) -> bool,
pub(crate) run:
for<'a> fn(&'a Cx, &'a [R::Model], &'a mut dyn toasty::Executor) -> ActionFuture<'a>,
pub(crate) success: fn(&Cx, usize) -> String,
pub(crate) acted: fn(Vec<R::Model>) -> super::Committed<R::Model>,
pub(crate) failure: &'static str,
pub(crate) confirm: bool,
}
impl<R: Resource> Clone for ActionEntry<R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Resource> Copy for ActionEntry<R> {}
impl<R: Resource> ActionEntry<R> {
pub(crate) fn delete() -> Self {
Self::deleting(false)
}
pub(crate) fn bulk_delete() -> Self {
Self::deleting(true)
}
fn deleting(bulk: bool) -> Self {
Self {
name: "delete",
label: |_| "Delete".to_string(),
row: !bulk,
bulk,
resource_wide: Ability::DeleteAny,
can_run: |resource, cx, record| resource.can(cx, Ability::Delete(record)),
run: if bulk {
bulk_delete_erased::<R>
} else {
delete_erased::<R>
},
success: if bulk {
|_, _| "Bulk deleted".to_string()
} else {
|_, _| "Deleted".to_string()
},
acted: super::Committed::deleted,
failure: if bulk {
"delete the selected rows"
} else {
"delete the record"
},
confirm: true,
}
}
}
fn can_run_erased<R: Resource, A: Action<R>>(_: &Mounted<R>, cx: &Cx, record: &R::Model) -> bool {
A::can_run(cx, record)
}
fn run_erased<'a, R: Resource, A: Action<R>>(
cx: &'a Cx,
records: &'a [R::Model],
ex: &'a mut dyn toasty::Executor,
) -> ActionFuture<'a> {
Box::pin(A::run(cx, records, ex))
}
fn delete_erased<'a, R: Resource>(
cx: &'a Cx,
records: &'a [R::Model],
ex: &'a mut dyn toasty::Executor,
) -> ActionFuture<'a> {
Box::pin(async move {
for record in records {
R::delete_record(cx, record, &mut *ex).await?;
}
Ok(())
})
}
fn bulk_delete_erased<'a, R: Resource>(
cx: &'a Cx,
records: &'a [R::Model],
ex: &'a mut dyn toasty::Executor,
) -> ActionFuture<'a> {
Box::pin(R::bulk_delete_records(cx, records, ex))
}