1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Character field for text input
use crate::field::{FieldError, FieldResult, FormField, Widget};
/// Character field with length validation
#[derive(Debug, Clone)]
pub struct CharField {
/// The field name used as the form data key.
pub name: String,
/// Optional human-readable label for display.
pub label: Option<String>,
/// Whether this field must be filled in.
pub required: bool,
/// Optional help text displayed alongside the field.
pub help_text: Option<String>,
/// The widget type used for rendering this field.
pub widget: Widget,
/// Optional initial (default) value for the field.
pub initial: Option<serde_json::Value>,
/// Maximum allowed character count.
pub max_length: Option<usize>,
/// Minimum required character count.
pub min_length: Option<usize>,
/// Whether to strip leading and trailing whitespace.
pub strip: bool,
/// Value to use when the input is empty.
pub empty_value: Option<String>,
}
impl CharField {
/// Create a new CharField with the given name
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string());
/// assert_eq!(field.name, "username");
/// assert!(!field.required);
/// assert_eq!(field.max_length, None);
/// ```
pub fn new(name: String) -> Self {
Self {
name,
label: None,
required: false,
help_text: None,
widget: Widget::TextInput,
initial: None,
max_length: None,
min_length: None,
strip: true,
empty_value: None,
}
}
/// Set the field as required
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).required();
/// assert!(field.required);
/// ```
pub fn required(mut self) -> Self {
self.required = true;
self
}
/// Set the maximum length for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).with_max_length(100);
/// assert_eq!(field.max_length, Some(100));
/// ```
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = Some(max_length);
self
}
/// Set the minimum length for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).with_min_length(5);
/// assert_eq!(field.min_length, Some(5));
/// ```
pub fn with_min_length(mut self, min_length: usize) -> Self {
self.min_length = Some(min_length);
self
}
/// Set the label for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).with_label("Username");
/// assert_eq!(field.label, Some("Username".to_string()));
/// ```
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
/// Set the help text for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).with_help_text("Enter your username");
/// assert_eq!(field.help_text, Some("Enter your username".to_string()));
/// ```
pub fn with_help_text(mut self, help_text: impl Into<String>) -> Self {
self.help_text = Some(help_text.into());
self
}
/// Set the initial value for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("username".to_string()).with_initial("default value");
/// assert_eq!(field.initial, Some(serde_json::json!("default value")));
/// ```
pub fn with_initial(mut self, initial: impl Into<String>) -> Self {
self.initial = Some(serde_json::json!(initial.into()));
self
}
/// Disable whitespace stripping for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
///
/// let field = CharField::new("description".to_string()).no_strip();
/// assert!(!field.strip);
/// ```
pub fn no_strip(mut self) -> Self {
self.strip = false;
self
}
/// Set the widget for the field
///
/// # Examples
///
/// ```
/// use reinhardt_forms::fields::CharField;
/// use reinhardt_forms::field::Widget;
///
/// let field = CharField::new("bio".to_string()).with_widget(Widget::TextArea);
/// ```
pub fn with_widget(mut self, widget: Widget) -> Self {
self.widget = widget;
self
}
}
// Note: Default trait is not implemented because CharField requires a name
impl FormField for CharField {
fn name(&self) -> &str {
&self.name
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn required(&self) -> bool {
self.required
}
fn help_text(&self) -> Option<&str> {
self.help_text.as_deref()
}
fn widget(&self) -> &Widget {
&self.widget
}
fn initial(&self) -> Option<&serde_json::Value> {
self.initial.as_ref()
}
fn clean(&self, value: Option<&serde_json::Value>) -> FieldResult<serde_json::Value> {
// Convert JSON value to string
let str_value = match value {
Some(v) => {
if v.is_null() {
None
} else {
Some(v.as_str().ok_or_else(|| {
FieldError::Validation("Value must be a string".to_string())
})?)
}
}
None => None,
};
// Process string value
let processed_value = match str_value {
Some(v) => {
let v = if self.strip { v.trim() } else { v };
if v.is_empty() {
if self.required {
return Err(FieldError::Required(self.name.clone()));
}
return Ok(serde_json::Value::String(
self.empty_value.clone().unwrap_or_default(),
));
}
v.to_string()
}
None => {
if self.required {
return Err(FieldError::Required(self.name.clone()));
}
return Ok(serde_json::Value::String(
self.empty_value.clone().unwrap_or_default(),
));
}
};
// Validate length using character count (not byte count) for correct
// multi-byte character handling (CJK, emoji, accented characters)
let char_count = processed_value.chars().count();
if let Some(max_length) = self.max_length
&& char_count > max_length
{
return Err(FieldError::Validation(format!(
"Ensure this value has at most {} characters (it has {})",
max_length, char_count
)));
}
if let Some(min_length) = self.min_length
&& char_count < min_length
{
return Err(FieldError::Validation(format!(
"Ensure this value has at least {} characters (it has {})",
min_length, char_count
)));
}
Ok(serde_json::Value::String(processed_value))
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
use serde_json::json;
#[rstest]
fn test_char_field_required() {
// Arrange
let field = CharField::new("test".to_string()).required();
// Act & Assert
assert!(field.clean(None).is_err());
assert!(field.clean(Some(&json!(""))).is_err());
assert!(field.clean(Some(&json!(" "))).is_err());
}
#[rstest]
fn test_char_field_max_length() {
// Arrange
let field = CharField::new("test".to_string()).with_max_length(5);
// Act & Assert
assert!(field.clean(Some(&json!("12345"))).is_ok());
assert!(field.clean(Some(&json!("123456"))).is_err());
}
#[rstest]
fn test_char_field_min_length() {
// Arrange
let field = CharField::new("test".to_string()).with_min_length(3);
// Act & Assert
assert!(field.clean(Some(&json!("123"))).is_ok());
assert!(field.clean(Some(&json!("12"))).is_err());
}
#[rstest]
fn test_char_field_length_uses_char_count_not_bytes() {
// Arrange: max_length=10 should allow 10 characters regardless of byte size
let field = CharField::new("test".to_string()).with_max_length(10);
// Act & Assert: CJK characters (3 bytes each in UTF-8, but 1 character each)
// 5 Japanese chars = 5 characters (15 bytes) - should pass
assert!(field.clean(Some(&json!("こんにちは"))).is_ok());
// 10 Japanese chars = 10 characters (30 bytes) - should pass (at limit)
assert!(field.clean(Some(&json!("こんにちはこんにちは"))).is_ok());
// 11 Japanese chars = 11 characters - should fail
assert!(field.clean(Some(&json!("こんにちはこんにちはX"))).is_err());
}
#[rstest]
fn test_char_field_length_with_emoji() {
// Arrange
let field = CharField::new("test".to_string()).with_max_length(5);
// Act & Assert: emoji characters (4 bytes each in UTF-8, but 1 character each)
// 5 emoji = 5 characters - should pass (at limit)
assert!(field.clean(Some(&json!("🎉🎊🎈🎁🎄"))).is_ok());
// 6 emoji = 6 characters - should fail
assert!(field.clean(Some(&json!("🎉🎊🎈🎁🎄🎃"))).is_err());
}
#[rstest]
fn test_char_field_min_length_with_multibyte() {
// Arrange
let field = CharField::new("test".to_string()).with_min_length(3);
// Act & Assert: 3 CJK characters should satisfy min_length=3
assert!(field.clean(Some(&json!("あいう"))).is_ok());
// 2 CJK characters should fail
assert!(field.clean(Some(&json!("あい"))).is_err());
}
}