use renox::serde_json::Value;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum FieldKind {
Text,
Email,
Password,
Url,
Tel,
Textarea,
Number,
Money,
Quantity,
Date,
DateTime,
Select,
Checkbox,
Toggle,
BelongsTo,
}
#[derive(Debug, Clone, Serialize)]
#[non_exhaustive]
pub struct Field {
name: String,
label: String,
kind: FieldKind,
options: Vec<(String, String)>,
required: bool,
hint: Option<String>,
placeholder: Option<String>,
prefix: Option<String>,
suffix: Option<String>,
span: Option<String>,
rows: u32,
step: Option<String>,
min: Option<String>,
max: Option<String>,
autocomplete: Option<String>,
searchable: bool,
default: Option<Value>,
on_create: bool,
on_edit: bool,
readonly_on_edit: bool,
scale: u32,
#[serde(skip)]
currency_from: Option<String>,
#[serde(skip)]
unit_from: Option<String>,
#[serde(skip)]
relation: Option<(String, String)>,
}
impl Field {
fn new(name: &str, label: &str, kind: FieldKind) -> Self {
Self {
name: name.to_owned(),
label: label.to_owned(),
kind,
options: Vec::new(),
required: false,
hint: None,
placeholder: None,
prefix: None,
suffix: None,
span: None,
rows: 4,
step: None,
min: None,
max: None,
autocomplete: None,
searchable: false,
default: None,
on_create: true,
on_edit: true,
readonly_on_edit: false,
scale: 0,
currency_from: None,
unit_from: None,
relation: None,
}
}
pub fn text(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Text)
}
pub fn email(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Email)
}
pub fn password(name: &str, label: &str) -> Self {
let mut field = Self::new(name, label, FieldKind::Password);
field.autocomplete = Some("new-password".into());
field
}
pub fn url(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Url)
}
pub fn tel(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Tel)
}
pub fn textarea(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Textarea)
}
pub fn number(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Number)
}
pub fn money(name: &str, label: &str) -> Self {
let mut field = Self::new(name, label, FieldKind::Money);
field.min = Some("0".into());
field
}
pub fn currency_from(mut self, key: &str) -> Self {
if self.kind == FieldKind::Money && plain_name(key) {
self.currency_from = Some(key.to_owned());
}
self
}
pub fn quantity(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Quantity)
}
pub fn scale(mut self, scale: u32) -> Self {
if self.kind == FieldKind::Quantity {
self.scale = scale.min(9);
}
self
}
pub fn unit_from(mut self, key: &str) -> Self {
if self.kind == FieldKind::Quantity && plain_name(key) {
self.unit_from = Some(key.to_owned());
}
self
}
pub fn date(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Date)
}
pub fn datetime(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::DateTime)
}
pub fn select<V: Into<String>, L: Into<String>>(
name: &str,
label: &str,
options: impl IntoIterator<Item = (V, L)>,
) -> Self {
let mut field = Self::new(name, label, FieldKind::Select);
field.options = options
.into_iter()
.map(|(value, label)| (value.into(), label.into()))
.collect();
field
}
pub fn checkbox(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Checkbox)
}
pub fn toggle(name: &str, label: &str) -> Self {
Self::new(name, label, FieldKind::Toggle)
}
pub fn belongs_to(name: &str, label: &str, table: &str, title: &str) -> Self {
let mut field = Self::new(name, label, FieldKind::BelongsTo);
field.relation = Some((table.to_owned(), title.to_owned()));
field.searchable = true;
field
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
pub fn hint(mut self, hint: &str) -> Self {
self.hint = Some(hint.to_owned());
self
}
pub fn placeholder(mut self, placeholder: &str) -> Self {
self.placeholder = Some(placeholder.to_owned());
self
}
pub fn prefix(mut self, prefix: &str) -> Self {
self.prefix = Some(prefix.to_owned());
self
}
pub fn suffix(mut self, suffix: &str) -> Self {
self.suffix = Some(suffix.to_owned());
self
}
pub fn span_full(mut self) -> Self {
self.span = Some("full".into());
self
}
pub fn rows(mut self, rows: u32) -> Self {
self.rows = rows;
self
}
pub fn step(mut self, step: &str) -> Self {
self.step = Some(step.to_owned());
self
}
pub fn min(mut self, min: impl ToString) -> Self {
self.min = Some(min.to_string());
self
}
pub fn max(mut self, max: impl ToString) -> Self {
self.max = Some(max.to_string());
self
}
pub fn autocomplete(mut self, autocomplete: &str) -> Self {
self.autocomplete = Some(autocomplete.to_owned());
self
}
pub fn searchable(mut self) -> Self {
self.searchable = true;
self
}
pub fn default_value(mut self, value: impl Serialize) -> Self {
self.default = renox::serde_json::to_value(value).ok();
self
}
pub fn only_on_create(mut self) -> Self {
self.on_edit = false;
self
}
pub fn only_on_edit(mut self) -> Self {
self.on_create = false;
self
}
pub fn readonly_on_edit(mut self) -> Self {
self.readonly_on_edit = true;
self
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> FieldKind {
self.kind
}
pub(crate) fn currency_column(&self) -> Option<&str> {
self.currency_from.as_deref()
}
pub(crate) fn unit_column(&self) -> Option<&str> {
self.unit_from.as_deref()
}
pub(crate) fn default_text(&self) -> Option<&str> {
self.default.as_ref().and_then(Value::as_str)
}
pub(crate) fn is_readonly_on_edit(&self) -> bool {
self.readonly_on_edit
}
pub(crate) fn shown(&self, creating: bool) -> bool {
if creating {
self.on_create
} else {
self.on_edit
}
}
pub(crate) fn relation(&self) -> Option<(&str, &str)> {
self.relation
.as_ref()
.map(|(table, title)| (table.as_str(), title.as_str()))
}
pub(crate) fn set_options(&mut self, options: Vec<(String, String)>) {
self.options = options;
}
pub(crate) fn label_text(&self) -> &str {
&self.label
}
pub(crate) fn has_placeholder(&self) -> bool {
self.placeholder.is_some()
}
pub(crate) fn is_required(&self) -> bool {
self.required
}
pub(crate) fn min_text(&self) -> Option<&str> {
self.min.as_deref()
}
pub(crate) fn max_text(&self) -> Option<&str> {
self.max.as_deref()
}
pub(crate) fn choices(&self) -> &[(String, String)] {
&self.options
}
pub(crate) fn translate(&mut self, translate: &dyn Fn(&str) -> String) {
self.label = translate(&self.label);
for text in [
&mut self.hint,
&mut self.placeholder,
&mut self.prefix,
&mut self.suffix,
]
.into_iter()
.flatten()
{
*text = translate(text);
}
for (_, label) in &mut self.options {
*label = translate(label);
}
}
}
pub(crate) fn plain_name(name: &str) -> bool {
!name.is_empty()
&& name.len() <= 64
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
&& !name.starts_with(|c: char| c.is_ascii_digit())
}