foundry_mcp/utils/
validation.rs

1//! Validation utilities to eliminate code duplication
2
3/// Create a single validation error message
4pub fn single_error<T: ToString>(message: T) -> Vec<String> {
5    vec![message.to_string()]
6}
7
8/// Create a single validation suggestion message
9pub fn single_suggestion<T: ToString>(message: T) -> Vec<String> {
10    vec![message.to_string()]
11}
12
13/// Create validation error messages from a condition and message
14pub 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
22/// Create validation suggestion messages from a condition and message
23pub 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
31/// Create multiple validation suggestions from conditions and messages
32pub 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
45/// Format validation error with content type prefix
46pub fn format_validation_error(content_type: &str, error: &str) -> String {
47    format!("{}: {}", content_type, error)
48}
49
50/// Format validation suggestion with content type prefix
51pub 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}