foundry_mcp/utils/
validation.rs1pub fn single_error<T: ToString>(message: T) -> Vec<String> {
5 vec![message.to_string()]
6}
7
8pub fn single_suggestion<T: ToString>(message: T) -> Vec<String> {
10 vec![message.to_string()]
11}
12
13pub fn conditional_error<T: ToString>(condition: bool, message: T) -> Vec<String> {
15 if condition {
16 single_error(message)
17 } else {
18 Vec::new()
19 }
20}
21
22pub fn conditional_suggestion<T: ToString>(condition: bool, message: T) -> Vec<String> {
24 if condition {
25 single_suggestion(message)
26 } else {
27 Vec::new()
28 }
29}
30
31pub fn conditional_suggestions(conditions_and_messages: &[(bool, &str)]) -> Vec<String> {
33 conditions_and_messages
34 .iter()
35 .filter_map(|(condition, message)| {
36 if *condition {
37 Some(message.to_string())
38 } else {
39 None
40 }
41 })
42 .collect()
43}
44
45pub fn format_validation_error(content_type: &str, error: &str) -> String {
47 format!("{}: {}", content_type, error)
48}
49
50pub fn format_validation_suggestion(content_type: &str, suggestion: &str) -> String {
52 format!("{}: {}", content_type, suggestion)
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_conditional_error_true() {
61 let errors = conditional_error(true, "This should appear");
62 assert_eq!(errors, vec!["This should appear".to_string()]);
63 }
64
65 #[test]
66 fn test_conditional_error_false() {
67 let errors = conditional_error(false, "This should not appear");
68 assert_eq!(errors, Vec::<String>::new());
69 }
70
71 #[test]
72 fn test_conditional_suggestions_mixed() {
73 let conditions = &[
74 (true, "Should appear"),
75 (false, "Should not appear"),
76 (true, "Should also appear"),
77 ];
78
79 let suggestions = conditional_suggestions(conditions);
80 assert_eq!(suggestions.len(), 2);
81 assert!(suggestions.contains(&"Should appear".to_string()));
82 assert!(suggestions.contains(&"Should also appear".to_string()));
83 }
84
85 #[test]
86 fn test_format_validation_error() {
87 let error = format_validation_error("Vision", "Content too short");
88 assert_eq!(error, "Vision: Content too short");
89 }
90}