pub mod registry;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WidgetKind {
Text,
Textarea,
Markdown,
RichText,
Email,
Url,
Tel,
Password,
Hidden,
Number,
Integer,
Float,
Range,
Boolean,
Date,
Time,
Datetime,
DatetimeTz,
Color,
File,
MediaPicker,
PageChooser,
SnippetChooser,
SnippetM2M,
DocumentChooser,
ModelChooser,
Radio,
Select,
Checkboxes,
MultiSelect,
Stream,
Custom,
}
impl WidgetKind {
#[must_use]
pub fn as_tag(&self) -> &'static str {
match self {
Self::Text => "text",
Self::Textarea => "textarea",
Self::Markdown => "markdown",
Self::RichText => "richtext",
Self::Email => "email",
Self::Url => "url",
Self::Tel => "tel",
Self::Password => "password",
Self::Hidden => "hidden",
Self::Number => "number",
Self::Integer => "integer",
Self::Float => "float",
Self::Range => "range",
Self::Boolean => "boolean",
Self::Date => "date",
Self::Time => "time",
Self::Datetime => "datetime",
Self::DatetimeTz => "datetimetz",
Self::Color => "color",
Self::File => "file",
Self::MediaPicker => "mediapicker",
Self::PageChooser => "pagechooser",
Self::SnippetChooser => "snippetchooser",
Self::SnippetM2M => "snippetm2m",
Self::DocumentChooser => "documentchooser",
Self::ModelChooser => "modelchooser",
Self::Radio => "radio",
Self::Select => "select",
Self::Checkboxes => "checkboxes",
Self::MultiSelect => "multiselect",
Self::Stream => "stream",
Self::Custom => "custom",
}
}
#[must_use]
pub fn is_multi_value(&self) -> bool {
matches!(
self,
Self::Checkboxes | Self::MultiSelect | Self::Stream | Self::SnippetM2M,
)
}
#[must_use]
pub fn is_translatable_text(&self) -> bool {
matches!(
self,
Self::Text | Self::Textarea | Self::Markdown | Self::RichText,
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Widget {
pub name: String,
pub kind: WidgetKind,
#[serde(default)]
pub custom_name: String,
#[serde(default)]
pub custom_html: String,
pub label: String,
#[serde(default)]
pub value: String,
#[serde(default)]
pub options: Vec<(String, String)>,
#[serde(default)]
pub help: String,
#[serde(default)]
pub placeholder: String,
#[serde(default)]
pub required: bool,
#[serde(default)]
pub min: Option<f64>,
#[serde(default)]
pub max: Option<f64>,
#[serde(default)]
pub step: Option<String>,
#[serde(default)]
pub max_length: Option<u32>,
#[serde(default)]
pub min_length: Option<u32>,
#[serde(default)]
pub pattern: String,
#[serde(default)]
pub read_only: bool,
#[serde(default)]
pub allowed: Vec<String>,
#[serde(default)]
pub variant: String,
}
impl Widget {
#[must_use]
pub fn new(kind: WidgetKind, name: impl Into<String>, label: impl Into<String>) -> Self {
Self {
name: name.into(),
kind,
custom_name: String::new(),
custom_html: String::new(),
label: label.into(),
value: String::new(),
options: Vec::new(),
help: String::new(),
placeholder: String::new(),
required: false,
min: None,
max: None,
step: None,
max_length: None,
min_length: None,
pattern: String::new(),
read_only: false,
allowed: Vec::new(),
variant: String::new(),
}
}
#[must_use]
pub fn with_min_length(mut self, n: u32) -> Self {
self.min_length = Some(n);
self
}
#[must_use]
pub fn with_pattern(mut self, p: impl Into<String>) -> Self {
self.pattern = p.into();
self
}
#[must_use]
pub fn with_variant(mut self, variant: impl Into<String>) -> Self {
self.variant = variant.into();
self
}
#[must_use]
pub fn stream<S: Into<String>>(
name: impl Into<String>,
label: impl Into<String>,
allowed: impl IntoIterator<Item = S>,
) -> Self {
Self {
allowed: allowed.into_iter().map(Into::into).collect(),
..Self::new(WidgetKind::Stream, name, label)
}
}
#[must_use]
pub fn custom(
custom_name: impl Into<String>,
name: impl Into<String>,
label: impl Into<String>,
) -> Self {
Self {
custom_name: custom_name.into(),
..Self::new(WidgetKind::Custom, name, label)
}
}
#[must_use]
pub fn with_value(mut self, v: impl Into<String>) -> Self {
self.value = v.into();
self
}
#[must_use]
pub fn with_help(mut self, h: impl Into<String>) -> Self {
self.help = h.into();
self
}
#[must_use]
pub fn with_placeholder(mut self, p: impl Into<String>) -> Self {
self.placeholder = p.into();
self
}
#[must_use]
pub fn required(mut self) -> Self {
self.required = true;
self
}
#[must_use]
pub fn read_only(mut self) -> Self {
self.read_only = true;
self
}
#[must_use]
pub fn with_options<I, V, L>(mut self, options: I) -> Self
where
I: IntoIterator<Item = (V, L)>,
V: Into<String>,
L: Into<String>,
{
self.options = options
.into_iter()
.map(|(v, l)| (v.into(), l.into()))
.collect();
self
}
#[must_use]
pub fn snippet_m2m(
relation_name: impl Into<String>,
label: impl Into<String>,
snippet_type_name: impl Into<String>,
) -> Self {
let mut w = Self::new(WidgetKind::SnippetM2M, relation_name, label);
w.custom_name = snippet_type_name.into();
w
}
#[must_use]
pub fn model_chooser(
field_name: impl Into<String>,
label: impl Into<String>,
chooser_slug: impl Into<String>,
) -> Self {
let mut w = Self::new(WidgetKind::ModelChooser, field_name, label);
w.custom_name = chooser_slug.into();
w
}
#[must_use]
pub fn with_min(mut self, m: f64) -> Self {
self.min = Some(m);
self
}
#[must_use]
pub fn with_max(mut self, m: f64) -> Self {
self.max = Some(m);
self
}
#[must_use]
pub fn with_step(mut self, s: impl Into<String>) -> Self {
self.step = Some(s.into());
self
}
#[must_use]
pub fn with_max_length(mut self, n: u32) -> Self {
self.max_length = Some(n);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snippet_m2m_kind_round_trips_as_tag() {
assert_eq!(WidgetKind::SnippetM2M.as_tag(), "snippetm2m");
}
#[test]
fn snippet_m2m_is_multi_value() {
assert!(WidgetKind::SnippetM2M.is_multi_value());
assert!(WidgetKind::Stream.is_multi_value());
assert!(WidgetKind::MultiSelect.is_multi_value());
assert!(WidgetKind::Checkboxes.is_multi_value());
assert!(!WidgetKind::Text.is_multi_value());
assert!(!WidgetKind::SnippetChooser.is_multi_value());
}
#[test]
fn snippet_m2m_constructor_sets_custom_name_to_type() {
let w = Widget::snippet_m2m("categories", "Categories", "Category");
assert_eq!(w.kind, WidgetKind::SnippetM2M);
assert_eq!(w.name, "categories");
assert_eq!(w.label, "Categories");
assert_eq!(w.custom_name, "Category");
}
}