#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum CrudOperation {
List,
View,
Create,
Edit,
Delete,
}
#[derive(Debug, Clone, Default, serde::Serialize)]
pub enum ColumnFilter {
#[default]
All,
Include(Vec<(String, String)>),
Exclude(Vec<String>),
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct ParentScope {
pub parent_key: &'static str,
pub fk_col: &'static str,
pub local_key: Option<&'static str>,
}
impl ParentScope {
#[must_use]
pub fn is_composite(&self) -> bool {
self.local_key.is_some()
}
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct DisplayConfig {
pub icon: Option<String>,
pub columns: ColumnFilter,
pub pagination: usize,
pub list_filter: Vec<(String, String, u64)>,
}
impl DisplayConfig {
pub fn new() -> Self {
Self {
icon: None,
columns: ColumnFilter::All,
pagination: 25,
list_filter: Vec::new(),
}
}
pub fn icon(mut self, icon: &str) -> Self {
self.icon = Some(icon.to_string());
self
}
pub fn pagination(mut self, per_page: usize) -> Self {
self.pagination = per_page;
self
}
pub fn columns_include(mut self, cols: Vec<(&str, &str)>) -> Self {
self.columns = ColumnFilter::Include(
cols.iter()
.map(|(c, l)| (c.to_string(), l.to_string()))
.collect(),
);
self
}
pub fn columns_exclude(mut self, cols: Vec<&str>) -> Self {
self.columns = ColumnFilter::Exclude(cols.iter().map(|s| s.to_string()).collect());
self
}
pub fn list_filter(mut self, filters: Vec<(&str, &str, u64)>) -> Self {
self.list_filter = filters
.iter()
.map(|(c, l, limit)| (c.to_string(), l.to_string(), *limit))
.collect();
self
}
}
impl Default for DisplayConfig {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct AdminResource {
pub key: &'static str,
pub model_path: &'static str,
pub form_path: &'static str,
pub title: &'static str,
pub display: DisplayConfig,
pub template_list: Option<String>,
pub template_create: Option<String>,
pub template_edit: Option<String>,
pub template_detail: Option<String>,
pub template_delete: Option<String>,
pub extra_context: crate::utils::aliases::StrMap,
pub inject_password: bool,
pub fk_display: Vec<(String, String, String)>,
pub parent_scope: Option<ParentScope>,
}
impl AdminResource {
pub fn new(
key: &'static str,
model_path: &'static str,
form_path: &'static str,
title: &'static str,
) -> Self {
Self {
key,
model_path,
form_path,
title,
display: DisplayConfig::new(),
template_list: None,
template_create: None,
template_edit: None,
template_detail: None,
template_delete: None,
extra_context: std::collections::HashMap::new(),
inject_password: false,
fk_display: Vec::new(),
parent_scope: None,
}
}
pub fn inject_password(mut self, v: bool) -> Self {
self.inject_password = v;
self
}
#[must_use]
pub fn fk_display(mut self, specs: Vec<(String, String, String)>) -> Self {
self.fk_display = specs;
self
}
#[must_use]
pub fn parent_scope(
mut self,
parent_key: &'static str,
fk_col: &'static str,
local_key: Option<&'static str>,
) -> Self {
self.parent_scope = Some(ParentScope {
parent_key,
fk_col,
local_key,
});
self
}
pub fn display(mut self, display: DisplayConfig) -> Self {
self.display = display;
self
}
pub fn template_list(mut self, path: &str) -> Self {
self.template_list = Some(path.to_string());
self
}
pub fn template_create(mut self, path: &str) -> Self {
self.template_create = Some(path.to_string());
self
}
pub fn template_edit(mut self, path: &str) -> Self {
self.template_edit = Some(path.to_string());
self
}
pub fn template_detail(mut self, path: &str) -> Self {
self.template_detail = Some(path.to_string());
self
}
pub fn template_delete(mut self, path: &str) -> Self {
self.template_delete = Some(path.to_string());
self
}
pub fn extra(mut self, key: &str, value: &str) -> Self {
self.extra_context
.insert(key.to_string(), value.to_string());
self
}
}