1use crate::file::FileStorage;
4
5pub const DEFAULT_MAX_FILE_SIZE: u64 = 2 * 1024 * 1024;
7
8pub const DEFAULT_MAX_MULTIPART_BODY: u64 = 8 * 1024 * 1024;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum FieldKind {
14 Text,
16 Textarea,
18 Hidden,
20 Password,
22 Checkbox,
24 Choice,
26 Number,
28 File,
30}
31
32#[derive(Debug, Clone, Default, PartialEq, Eq)]
34pub struct TextOptions {
35 pub placeholder: Option<String>,
37}
38
39#[derive(Debug, Clone, Default, PartialEq, Eq)]
41pub struct TextareaOptions {
42 pub placeholder: Option<String>,
44 pub rows: Option<u32>,
46}
47
48#[derive(Debug, Clone, Default, PartialEq, Eq)]
50pub struct HiddenOptions {}
51
52#[derive(Debug, Clone, Default, PartialEq, Eq)]
54pub struct PasswordOptions {
55 pub placeholder: Option<String>,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
61pub struct CheckboxOptions {
62 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#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct ChoiceEntry {
77 pub value: String,
79 pub label: String,
81}
82
83impl ChoiceEntry {
84 #[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 #[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#[derive(Debug, Clone, Default, PartialEq, Eq)]
106pub struct ChoiceOptions {
107 pub choices: Vec<ChoiceEntry>,
109 pub multiple: bool,
111 pub expanded: bool,
113}
114
115#[derive(Debug, Clone, Default, PartialEq, Eq)]
117pub struct NumberOptions {
118 pub min: Option<i64>,
120 pub max: Option<i64>,
122 pub step: Option<i64>,
124}
125
126#[derive(Debug, Clone, PartialEq, Eq)]
128pub struct FileOptions {
129 pub accept: Option<String>,
131 pub max_size: u64,
133 pub mime_types: Vec<String>,
135 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#[derive(Debug, Clone, PartialEq, Eq)]
152pub enum FieldConfig {
153 Text(TextOptions),
155 Textarea(TextareaOptions),
157 Hidden(HiddenOptions),
159 Password(PasswordOptions),
161 Checkbox(CheckboxOptions),
163 Choice(ChoiceOptions),
165 Number(NumberOptions),
167 File(FileOptions),
169}
170
171impl Default for FieldConfig {
172 fn default() -> Self {
173 Self::Text(TextOptions::default())
174 }
175}
176
177impl FieldConfig {
178 #[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 #[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 #[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 #[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 #[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}