Skip to main content

serenade_form/
kind.rs

1//! Field kinds and typed option structs.
2
3use crate::file::FileStorage;
4
5/// Default per-file size limit enforced when [`FileOptions::max_size`] is unset (2 MiB).
6pub const DEFAULT_MAX_FILE_SIZE: u64 = 2 * 1024 * 1024;
7
8/// Default multipart body size limit for [`crate::parse_multipart`] (8 MiB).
9pub const DEFAULT_MAX_MULTIPART_BODY: u64 = 8 * 1024 * 1024;
10
11/// HTML-oriented field kind used for bind and render.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum FieldKind {
14    /// Single-line text (`input type="text"`).
15    Text,
16    /// Multi-line text (`textarea`).
17    Textarea,
18    /// Hidden input.
19    Hidden,
20    /// Password input (value still stored as a string).
21    Password,
22    /// Checkbox (`input type="checkbox"`).
23    Checkbox,
24    /// Single or multi select / radio group (see [`ChoiceOptions::multiple`]).
25    Choice,
26    /// Numeric input (`input type="number"`).
27    Number,
28    /// File upload (`input type="file"`); bind via multipart only.
29    File,
30}
31
32/// Options for [`FieldKind::Text`].
33#[derive(Debug, Clone, Default, PartialEq, Eq)]
34pub struct TextOptions {
35    /// Optional `placeholder` attribute.
36    pub placeholder: Option<String>,
37}
38
39/// Options for [`FieldKind::Textarea`].
40#[derive(Debug, Clone, Default, PartialEq, Eq)]
41pub struct TextareaOptions {
42    /// Optional `placeholder` attribute.
43    pub placeholder: Option<String>,
44    /// Optional `rows` attribute.
45    pub rows: Option<u32>,
46}
47
48/// Options for [`FieldKind::Hidden`].
49#[derive(Debug, Clone, Default, PartialEq, Eq)]
50pub struct HiddenOptions {}
51
52/// Options for [`FieldKind::Password`].
53#[derive(Debug, Clone, Default, PartialEq, Eq)]
54pub struct PasswordOptions {
55    /// Optional `placeholder` attribute.
56    pub placeholder: Option<String>,
57}
58
59/// Options for [`FieldKind::Checkbox`].
60#[derive(Debug, Clone, PartialEq, Eq)]
61pub struct CheckboxOptions {
62    /// Submitted value when the box is checked (default `"1"`).
63    pub checked_value: String,
64}
65
66impl Default for CheckboxOptions {
67    fn default() -> Self {
68        Self {
69            checked_value: String::from("1"),
70        }
71    }
72}
73
74/// One choice entry: submitted value and display label.
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct ChoiceEntry {
77    /// Value posted when this choice is selected.
78    pub value: String,
79    /// Human-readable label.
80    pub label: String,
81}
82
83impl ChoiceEntry {
84    /// Builds a choice with the same string for value and label.
85    #[must_use]
86    pub fn new(value: impl Into<String>) -> Self {
87        let value = value.into();
88        Self {
89            label: value.clone(),
90            value,
91        }
92    }
93
94    /// Builds a choice with distinct value and label.
95    #[must_use]
96    pub fn with_label(value: impl Into<String>, label: impl Into<String>) -> Self {
97        Self {
98            value: value.into(),
99            label: label.into(),
100        }
101    }
102}
103
104/// Options for [`FieldKind::Choice`].
105#[derive(Debug, Clone, Default, PartialEq, Eq)]
106pub struct ChoiceOptions {
107    /// Allowed choices.
108    pub choices: Vec<ChoiceEntry>,
109    /// When `true`, multiple values may be selected (stored space-separated in [`crate::Field::value`]).
110    pub multiple: bool,
111    /// When `true`, prefer expanded widgets (radio/checkbox list) over `<select>` in render.
112    pub expanded: bool,
113}
114
115/// Options for [`FieldKind::Number`].
116#[derive(Debug, Clone, Default, PartialEq, Eq)]
117pub struct NumberOptions {
118    /// Optional HTML `min`.
119    pub min: Option<i64>,
120    /// Optional HTML `max`.
121    pub max: Option<i64>,
122    /// Optional HTML `step` (integer step).
123    pub step: Option<i64>,
124}
125
126/// Options for [`FieldKind::File`].
127#[derive(Debug, Clone, PartialEq, Eq)]
128pub struct FileOptions {
129    /// Optional HTML `accept` attribute (for example `image/*`).
130    pub accept: Option<String>,
131    /// Maximum upload size in bytes (default [`DEFAULT_MAX_FILE_SIZE`]).
132    pub max_size: u64,
133    /// Allowed MIME types; empty means any type is accepted.
134    pub mime_types: Vec<String>,
135    /// Whether the payload stays in memory or spills to a tempfile.
136    pub storage: FileStorage,
137}
138
139impl Default for FileOptions {
140    fn default() -> Self {
141        Self {
142            accept: None,
143            max_size: DEFAULT_MAX_FILE_SIZE,
144            mime_types: Vec::new(),
145            storage: FileStorage::Memory,
146        }
147    }
148}
149
150/// Typed configuration for a field (kind + options).
151#[derive(Debug, Clone, PartialEq, Eq)]
152pub enum FieldConfig {
153    /// [`FieldKind::Text`].
154    Text(TextOptions),
155    /// [`FieldKind::Textarea`].
156    Textarea(TextareaOptions),
157    /// [`FieldKind::Hidden`].
158    Hidden(HiddenOptions),
159    /// [`FieldKind::Password`].
160    Password(PasswordOptions),
161    /// [`FieldKind::Checkbox`].
162    Checkbox(CheckboxOptions),
163    /// [`FieldKind::Choice`].
164    Choice(ChoiceOptions),
165    /// [`FieldKind::Number`].
166    Number(NumberOptions),
167    /// [`FieldKind::File`].
168    File(FileOptions),
169}
170
171impl Default for FieldConfig {
172    fn default() -> Self {
173        Self::Text(TextOptions::default())
174    }
175}
176
177impl FieldConfig {
178    /// Returns the [`FieldKind`] for this config.
179    #[must_use]
180    pub const fn kind(&self) -> FieldKind {
181        match self {
182            Self::Text(_) => FieldKind::Text,
183            Self::Textarea(_) => FieldKind::Textarea,
184            Self::Hidden(_) => FieldKind::Hidden,
185            Self::Password(_) => FieldKind::Password,
186            Self::Checkbox(_) => FieldKind::Checkbox,
187            Self::Choice(_) => FieldKind::Choice,
188            Self::Number(_) => FieldKind::Number,
189            Self::File(_) => FieldKind::File,
190        }
191    }
192
193    /// Returns choice options when this is a choice field.
194    #[must_use]
195    pub const fn as_choice(&self) -> Option<&ChoiceOptions> {
196        match self {
197            Self::Choice(options) => Some(options),
198            _ => None,
199        }
200    }
201
202    /// Returns checkbox options when this is a checkbox field.
203    #[must_use]
204    pub const fn as_checkbox(&self) -> Option<&CheckboxOptions> {
205        match self {
206            Self::Checkbox(options) => Some(options),
207            _ => None,
208        }
209    }
210
211    /// Returns number options when this is a number field.
212    #[must_use]
213    pub const fn as_number(&self) -> Option<&NumberOptions> {
214        match self {
215            Self::Number(options) => Some(options),
216            _ => None,
217        }
218    }
219
220    /// Returns file options when this is a file field.
221    #[must_use]
222    pub const fn as_file(&self) -> Option<&FileOptions> {
223        match self {
224            Self::File(options) => Some(options),
225            _ => None,
226        }
227    }
228}