use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use renox::db::{Model, Query};
use renox::grid::{Column, Grid};
use renox::prelude::*;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::{Entry, Field};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub trait AdminResource: Send + Sync + 'static {
type Model: Model + Policy + Serialize + Default + Send + Sync;
type Form: DeserializeOwned + Validate + Serialize + Send + 'static;
fn label(&self) -> &str;
fn plural_label(&self) -> &str;
fn record_title(&self, record: &Self::Model) -> String {
format!("{} #{}", self.label(), record.id())
}
fn slug(&self) -> &str {
Self::Model::TABLE
}
fn navigation_group(&self) -> Option<&str> {
None
}
fn columns(&self) -> Vec<Column>;
fn fields(&self) -> Vec<Field>;
fn fill(&self, record: &mut Self::Model, form: Self::Form);
fn rules(&self, form: &Self::Form, record: Option<&Self::Model>, v: &mut Validator) {
let _ = (form, record, v);
}
fn entries(&self) -> Vec<Entry> {
self.columns()
.iter()
.filter_map(Entry::from_column)
.collect()
}
fn filters(&self) -> Vec<Filter<Self::Model>> {
Vec::new()
}
fn actions(&self) -> Vec<AdminAction<Self::Model>> {
Vec::new()
}
fn query(&self) -> Query<Self::Model> {
Self::Model::query()
}
fn grid(&self, grid: Grid) -> Grid {
grid
}
fn allows(&self, user: &AuthUser, ability: &str, record: Option<&Self::Model>) -> bool {
match record {
Some(record) => user.can(ability, record),
None => user.can(ability, &Self::Model::default()),
}
}
}
#[derive(Clone)]
#[non_exhaustive]
pub struct ActionContext {
pub state: AppState,
pub user: AuthUser,
}
type Run<M> = Arc<dyn Fn(Vec<M>, ActionContext) -> BoxFuture<'static, Result<Toast>> + Send + Sync>;
pub struct AdminAction<M> {
key: String,
label: String,
ability: String,
confirm: Option<String>,
danger: bool,
bulk: bool,
row: bool,
run: Run<M>,
}
impl<M> Clone for AdminAction<M> {
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
label: self.label.clone(),
ability: self.ability.clone(),
confirm: self.confirm.clone(),
danger: self.danger,
bulk: self.bulk,
row: self.row,
run: self.run.clone(),
}
}
}
impl<M> fmt::Debug for AdminAction<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AdminAction")
.field("key", &self.key)
.field("label", &self.label)
.field("ability", &self.ability)
.finish_non_exhaustive()
}
}
impl<M: Send + 'static> AdminAction<M> {
pub fn new<F, Fut>(key: &str, label: &str, run: F) -> Self
where
F: Fn(Vec<M>, ActionContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Toast>> + Send + 'static,
{
Self {
key: key.to_owned(),
label: label.to_owned(),
ability: "update".into(),
confirm: None,
danger: false,
bulk: true,
row: false,
run: Arc::new(move |records, cx| Box::pin(run(records, cx))),
}
}
pub fn ability(mut self, ability: &str) -> Self {
self.ability = ability.to_owned();
self
}
pub fn confirm(mut self, question: &str) -> Self {
self.confirm = Some(question.to_owned());
self
}
pub fn danger(mut self) -> Self {
self.danger = true;
self
}
pub fn row(mut self) -> Self {
self.row = true;
self
}
pub fn row_only(mut self) -> Self {
self.row = true;
self.bulk = false;
self
}
pub(crate) fn key(&self) -> &str {
&self.key
}
pub(crate) fn label(&self) -> &str {
&self.label
}
pub(crate) fn ability_name(&self) -> &str {
&self.ability
}
pub(crate) fn question(&self) -> Option<&str> {
self.confirm.as_deref()
}
pub(crate) fn is_danger(&self) -> bool {
self.danger
}
pub(crate) fn on_bulk(&self) -> bool {
self.bulk
}
pub(crate) fn on_row(&self) -> bool {
self.row
}
pub(crate) fn run(
&self,
records: Vec<M>,
cx: ActionContext,
) -> BoxFuture<'static, Result<Toast>> {
(self.run)(records, cx)
}
}
type Scope<M> = Arc<dyn Fn(Query<M>) -> Query<M> + Send + Sync>;
pub struct Filter<M> {
key: String,
label: String,
scope: Scope<M>,
}
impl<M> Clone for Filter<M> {
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
label: self.label.clone(),
scope: self.scope.clone(),
}
}
}
impl<M> fmt::Debug for Filter<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Filter")
.field("key", &self.key)
.field("label", &self.label)
.finish_non_exhaustive()
}
}
impl<M> Filter<M> {
pub fn new(
key: &str,
label: &str,
scope: impl Fn(Query<M>) -> Query<M> + Send + Sync + 'static,
) -> Self {
Self {
key: key.to_owned(),
label: label.to_owned(),
scope: Arc::new(scope),
}
}
pub(crate) fn key(&self) -> &str {
&self.key
}
pub(crate) fn label(&self) -> &str {
&self.label
}
pub(crate) fn apply(&self, query: Query<M>) -> Query<M> {
(self.scope)(query)
}
}