MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / from_directive

Method from_directive

aiscript-directive/src/validator/date.rs:70–108  ·  view source on GitHub ↗
(directive: Directive)

Source from the content-addressed store, hash-verified

68
69impl FromDirective for DateValidator {
70 fn from_directive(directive: Directive) -> Result<Self, String> {
71 match directive.params {
72 DirectiveParams::KeyValue(params) => {
73 let format = params
74 .get("format")
75 .and_then(|v| v.as_str().map(ToString::to_string));
76 let min = params
77 .get("min")
78 .and_then(|v| v.as_str().map(ToString::to_string));
79 let max = params
80 .get("max")
81 .and_then(|v| v.as_str().map(ToString::to_string));
82
83 // Validate that min and max follow the format if provided
84 if let (Some(format_str), Some(min_str)) = (&format, &min) {
85 let chrono_format = convert_format(format_str);
86 if NaiveDate::parse_from_str(min_str, &chrono_format).is_err() {
87 return Err(format!(
88 "min date '{}' doesn't match format '{}'",
89 min_str, format_str
90 ));
91 }
92 }
93
94 if let (Some(format_str), Some(max_str)) = (&format, &max) {
95 let chrono_format = convert_format(format_str);
96 if NaiveDate::parse_from_str(max_str, &chrono_format).is_err() {
97 return Err(format!(
98 "max date '{}' doesn't match format '{}'",
99 max_str, format_str
100 ));
101 }
102 }
103
104 Ok(Self { format, min, max })
105 }
106 _ => Err("Invalid params for @date directive".into()),
107 }
108 }
109}
110
111fn convert_format(user_format: &str) -> String {

Callers

nothing calls this directly

Calls 3

convert_formatFunction · 0.85
getMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected