use crate::protocol::{FieldKind, FieldSchemaDto, FormSectionSchemaDto, SelectOptionDto};
use crate::schema::humanize;
#[derive(Debug, Clone)]
pub struct SelectOption {
pub value: String,
pub label: String,
pub description: Option<String>,
}
impl SelectOption {
#[must_use]
pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
Self {
value: value.into(),
label: label.into(),
description: None,
}
}
#[must_use]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub(crate) fn to_dto(&self) -> SelectOptionDto {
SelectOptionDto {
value: self.value.clone(),
label: self.label.clone(),
description: self.description.clone(),
}
}
}
impl<V: Into<String>, L: Into<String>> From<(V, L)> for SelectOption {
fn from((value, label): (V, L)) -> Self {
Self::new(value, label)
}
}
impl From<&str> for SelectOption {
fn from(value: &str) -> Self {
Self::new(value, humanize(value))
}
}
#[derive(Debug, Clone)]
pub struct Field {
pub(crate) name: String,
pub(crate) label: String,
pub(crate) kind: FieldKind,
pub(crate) required: bool,
pub(crate) nullable: bool,
pub(crate) disabled: bool,
pub(crate) placeholder: Option<String>,
pub(crate) helper: Option<String>,
pub(crate) prefix: Option<String>,
pub(crate) suffix: Option<String>,
pub(crate) rows: Option<u8>,
pub(crate) min: Option<f64>,
pub(crate) max: Option<f64>,
pub(crate) max_length: Option<usize>,
pub(crate) column_span: u8,
pub(crate) default_value: Option<serde_json::Value>,
pub(crate) options: Vec<SelectOptionDto>,
pub(crate) create_only: bool,
pub(crate) edit_only: bool,
}
impl Field {
fn new(name: impl Into<String>, kind: FieldKind) -> Self {
let name = name.into();
let label = humanize(&name);
Self {
name,
label,
kind,
required: false,
nullable: false,
disabled: false,
placeholder: None,
helper: None,
prefix: None,
suffix: None,
rows: if kind == FieldKind::Textarea { Some(4) } else { None },
min: None,
max: None,
max_length: None,
column_span: 1,
default_value: None,
options: Vec::new(),
create_only: false,
edit_only: false,
}
}
#[must_use]
pub fn text(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Text)
}
#[must_use]
pub fn email(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Email)
}
#[must_use]
pub fn url(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Url)
}
#[must_use]
pub fn password(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Password)
}
#[must_use]
pub fn textarea(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Textarea)
}
#[must_use]
pub fn number(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Number)
}
#[must_use]
pub fn decimal(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Decimal)
}
#[must_use]
pub fn select(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Select)
}
#[must_use]
pub fn radio(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Radio)
}
#[must_use]
pub fn toggle(name: impl Into<String>) -> Self {
let mut f = Self::new(name, FieldKind::Toggle);
f.default_value = Some(serde_json::Value::Bool(false));
f
}
#[must_use]
pub fn checkbox(name: impl Into<String>) -> Self {
let mut f = Self::new(name, FieldKind::Checkbox);
f.default_value = Some(serde_json::Value::Bool(false));
f
}
#[must_use]
pub fn date(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Date)
}
#[must_use]
pub fn datetime(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::Datetime)
}
#[must_use]
pub fn civil_datetime(name: impl Into<String>) -> Self {
Self::new(name, FieldKind::CivilDatetime)
}
#[must_use]
pub fn tags(name: impl Into<String>) -> Self {
let mut f = Self::new(name, FieldKind::Tags);
f.default_value = Some(serde_json::Value::Array(Vec::new()));
f
}
#[must_use]
pub fn json(name: impl Into<String>) -> Self {
let mut f = Self::new(name, FieldKind::Json);
f.rows = Some(6);
f
}
#[must_use]
pub fn readonly(name: impl Into<String>) -> Self {
let mut f = Self::new(name, FieldKind::Readonly);
f.disabled = true;
f
}
#[must_use]
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
#[must_use]
pub fn required(mut self) -> Self {
self.required = true;
self
}
#[must_use]
pub fn nullable(mut self) -> Self {
self.nullable = true;
self
}
#[must_use]
pub fn disabled(mut self) -> Self {
self.disabled = true;
self
}
#[must_use]
pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = Some(placeholder.into());
self
}
#[must_use]
pub fn helper(mut self, helper: impl Into<String>) -> Self {
self.helper = Some(helper.into());
self
}
#[must_use]
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = Some(prefix.into());
self
}
#[must_use]
pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
self.suffix = Some(suffix.into());
self
}
#[must_use]
pub fn rows(mut self, rows: u8) -> Self {
self.rows = Some(rows);
self
}
#[must_use]
pub fn min(mut self, min: f64) -> Self {
self.min = Some(min);
self
}
#[must_use]
pub fn max(mut self, max: f64) -> Self {
self.max = Some(max);
self
}
#[must_use]
pub fn max_length(mut self, max_length: usize) -> Self {
self.max_length = Some(max_length);
self
}
#[must_use]
pub fn column_span_full(mut self) -> Self {
self.column_span = 2;
self
}
#[must_use]
pub fn default_value(mut self, value: impl Into<serde_json::Value>) -> Self {
self.default_value = Some(value.into());
self
}
#[must_use]
pub fn options<I, O>(mut self, options: I) -> Self
where
I: IntoIterator<Item = O>,
O: Into<SelectOption>,
{
self.options = options.into_iter().map(|opt| opt.into().to_dto()).collect();
self
}
#[must_use]
pub fn create_only(mut self) -> Self {
self.create_only = true;
self
}
#[must_use]
pub fn edit_only(mut self) -> Self {
self.edit_only = true;
self
}
pub(crate) fn to_dto(&self) -> FieldSchemaDto {
FieldSchemaDto {
name: self.name.clone(),
label: self.label.clone(),
kind: self.kind,
required: self.required,
nullable: self.nullable,
disabled: self.disabled,
placeholder: self.placeholder.clone(),
helper: self.helper.clone(),
prefix: self.prefix.clone(),
suffix: self.suffix.clone(),
rows: self.rows,
min: self.min,
max: self.max,
max_length: self.max_length,
column_span: self.column_span,
default_value: self.default_value.clone(),
options: self.options.clone(),
create_only: self.create_only,
edit_only: self.edit_only,
}
}
}
#[derive(Debug, Clone)]
pub struct FormSection {
pub(crate) title: Option<String>,
pub(crate) description: Option<String>,
pub(crate) columns: u8,
pub(crate) fields: Vec<Field>,
}
impl FormSection {
#[must_use]
pub fn new(title: impl Into<String>) -> Self {
Self {
title: Some(title.into()),
description: None,
columns: 2,
fields: Vec::new(),
}
}
#[must_use]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn columns(mut self, columns: u8) -> Self {
self.columns = columns.clamp(1, 3);
self
}
#[must_use]
pub fn field(mut self, field: Field) -> Self {
self.fields.push(field);
self
}
#[must_use]
pub fn fields(mut self, fields: impl IntoIterator<Item = Field>) -> Self {
self.fields.extend(fields);
self
}
pub(crate) fn to_dto(&self) -> FormSectionSchemaDto {
FormSectionSchemaDto {
title: self.title.clone(),
description: self.description.clone(),
columns: self.columns,
fields: self.fields.iter().map(Field::to_dto).collect(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct FormBuilder {
pub(crate) sections: Vec<FormSection>,
pub(crate) loose_fields: Vec<Field>,
}
impl FormBuilder {
#[must_use]
pub fn field(mut self, field: Field) -> Self {
self.loose_fields.push(field);
self
}
#[must_use]
pub fn fields(mut self, fields: impl IntoIterator<Item = Field>) -> Self {
self.loose_fields.extend(fields);
self
}
#[must_use]
pub fn section(mut self, section: FormSection) -> Self {
self.sections.push(section);
self
}
pub(crate) fn to_dto(&self) -> Vec<FormSectionSchemaDto> {
let mut out = Vec::new();
if !self.loose_fields.is_empty() {
out.push(FormSectionSchemaDto {
title: None,
description: None,
columns: 2,
fields: self.loose_fields.iter().map(Field::to_dto).collect(),
});
}
out.extend(self.sections.iter().map(FormSection::to_dto));
out
}
}