use gpui::{prelude::FluentBuilder as _, *};
use std::sync::Arc;
use crate::capabilities::foundation::theme::ThemeTokens;
use crate::capabilities::layout::HStack;
pub type ValidationCallback = Arc<dyn Fn(&str) -> Result<(), ValidationError>>;
pub type TextTransformer = Arc<dyn Fn(&str) -> String>;
#[derive(Debug, Clone)]
pub struct ValidationError {
pub message: SharedString,
pub field_name: Option<SharedString>,
}
#[derive(Clone, Default)]
pub struct ValidationRules {
pub min_length: Option<usize>,
pub max_length: Option<usize>,
pub min_value: Option<f64>,
pub max_value: Option<f64>,
pub pattern: Option<String>,
pub custom_validator: Option<ValidationCallback>,
pub custom_filter: Option<TextTransformer>,
pub custom_formatter: Option<TextTransformer>,
pub required: bool,
}
impl std::fmt::Debug for ValidationRules {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ValidationRules")
.field("min_length", &self.min_length)
.field("max_length", &self.max_length)
.field("min_value", &self.min_value)
.field("max_value", &self.max_value)
.field("pattern", &self.pattern)
.field("custom_validator", &self.custom_validator.is_some())
.field("custom_filter", &self.custom_filter.is_some())
.field("custom_formatter", &self.custom_formatter.is_some())
.field("required", &self.required)
.finish()
}
}
pub(crate) fn check_required_empty(
required: bool,
value: &str,
field_name: Option<SharedString>,
) -> Option<Result<(), ValidationError>> {
if required && value.trim().is_empty() {
return Some(Err(ValidationError {
message: "This field is required".into(),
field_name,
}));
}
if !required && value.trim().is_empty() {
return Some(Ok(()));
}
None
}
pub(crate) fn check_constraints(
rules: &ValidationRules,
value: &str,
field_name: Option<SharedString>,
) -> Result<(), ValidationError> {
if let Some(min_length) = rules.min_length {
if value.chars().count() < min_length {
return Err(ValidationError {
message: format!("Must be at least {} characters", min_length).into(),
field_name,
});
}
}
if let Some(max_length) = rules.max_length {
if value.chars().count() > max_length {
return Err(ValidationError {
message: format!("Must be no more than {} characters", max_length).into(),
field_name,
});
}
}
if let Some(pattern) = rules.pattern.as_deref() {
match regex::Regex::new(pattern) {
Ok(regex) if !regex.is_match(value) => {
return Err(ValidationError {
message: "Value does not match the required pattern".into(),
field_name,
});
}
Err(_) => {
return Err(ValidationError {
message: "Invalid validation pattern".into(),
field_name,
});
}
Ok(_) => {}
}
}
if let Some(ref validator) = rules.custom_validator {
validator(value)?;
}
Ok(())
}
pub(crate) struct CharacterCount {
pub committed: usize,
pub composing: usize,
pub max: Option<usize>,
}
pub(crate) fn format_character_count(
committed: usize,
composing: usize,
max: Option<usize>,
) -> (String, bool) {
let mut text = committed.to_string();
if composing > 0 {
text.push('+');
text.push_str(&composing.to_string());
}
if let Some(max) = max {
text.push('/');
text.push_str(&max.to_string());
}
let over = max.is_some_and(|max| committed >= max || committed + composing > max);
(text, over)
}
pub(crate) enum FooterMessage<'a> {
Error(&'a ValidationError),
Success(&'a SharedString),
Helper(&'a SharedString),
Blank,
}
pub(crate) fn validation_footer(
tokens: &ThemeTokens,
message: FooterMessage,
character_count: Option<CharacterCount>,
) -> impl IntoElement {
let message_element = match message {
FooterMessage::Error(error) => div()
.text_color(tokens.destructive)
.child(error.message.clone()),
FooterMessage::Success(success) => div().text_color(tokens.primary).child(success.clone()),
FooterMessage::Helper(helper) => div()
.text_color(tokens.muted_foreground)
.child(helper.clone()),
FooterMessage::Blank => div(),
};
HStack::new()
.w_full()
.px(px(2.0))
.child(
div()
.flex_1()
.text_size(tokens.text_xs)
.font_family(tokens.font_family.clone())
.child(message_element),
)
.when_some(character_count, |h, count| {
let (text, over) = format_character_count(count.committed, count.composing, count.max);
h.child(
div()
.text_size(tokens.text_xs)
.font_family(tokens.font_family.clone())
.text_color(if over {
tokens.destructive
} else {
tokens.muted_foreground
})
.child(text),
)
})
}
#[cfg(test)]
mod tests {
use super::{
check_constraints, check_required_empty, format_character_count, CharacterCount,
ValidationError, ValidationRules,
};
use std::sync::Arc;
#[test]
fn required_empty_fails() {
let result = check_required_empty(true, " ", None);
assert!(matches!(result, Some(Err(_))));
}
#[test]
fn optional_empty_passes_without_constraint_checks() {
let rules = ValidationRules {
min_length: Some(3),
..Default::default()
};
assert!(matches!(
check_required_empty(false, "", None),
Some(Ok(()))
));
assert!(check_required_empty(rules.required, "", None).is_some());
}
#[test]
fn constraints_enforce_lengths() {
let rules = ValidationRules {
min_length: Some(2),
max_length: Some(4),
..Default::default()
};
assert!(check_constraints(&rules, "a", None).is_err());
assert!(check_constraints(&rules, "abcde", None).is_err());
assert!(check_constraints(&rules, "abc", None).is_ok());
}
#[test]
fn constraints_count_chars_not_bytes() {
let rules = ValidationRules {
max_length: Some(2),
..Default::default()
};
assert!(check_constraints(&rules, "你好", None).is_ok());
assert!(check_constraints(&rules, "你好呀", None).is_err());
}
#[test]
fn constraints_apply_pattern_and_custom_validator() {
let rules = ValidationRules {
pattern: Some(r"^\d+$".to_string()),
..Default::default()
};
assert!(check_constraints(&rules, "123", None).is_ok());
assert!(check_constraints(&rules, "12a", None).is_err());
let rules = ValidationRules {
custom_validator: Some(Arc::new(|value| {
if value.contains('x') {
Ok(())
} else {
Err(ValidationError {
message: "needs x".into(),
field_name: None,
})
}
})),
..Default::default()
};
assert!(check_constraints(&rules, "oxo", None).is_ok());
assert!(check_constraints(&rules, "ooo", None).is_err());
}
#[test]
fn character_count_formats_plain_and_max() {
assert_eq!(
format_character_count(12, 0, None),
("12".to_string(), false)
);
assert_eq!(
format_character_count(12, 0, Some(140)),
("12/140".to_string(), false)
);
}
#[test]
fn character_count_shows_composition_separately() {
assert_eq!(
format_character_count(12, 3, Some(140)),
("12+3/140".to_string(), false)
);
}
#[test]
fn character_count_flags_full_and_impending_overflow() {
assert_eq!(
format_character_count(140, 0, Some(140)),
("140/140".to_string(), true)
);
assert_eq!(
format_character_count(138, 5, Some(140)),
("138+5/140".to_string(), true)
);
assert_eq!(
format_character_count(138, 2, Some(140)),
("138+2/140".to_string(), false)
);
assert_eq!(
format_character_count(10, 50, None),
("10+50".to_string(), false)
);
}
#[test]
fn character_count_struct_is_what_footer_consumes() {
let count = CharacterCount {
committed: 3,
composing: 2,
max: Some(4),
};
let (_, over) = format_character_count(count.committed, count.composing, count.max);
assert!(over);
}
}