gitql_ast/
format_checker.rs

1/// Check if String literal is matching SQL time format: HH:MM:SS or HH:MM:SS.SSS
2pub fn is_valid_time_format(time_str: &str) -> bool {
3    // Check length of the string
4    if !(8..=12).contains(&time_str.len()) {
5        return false;
6    }
7
8    // Split the string into hours, minutes, seconds, and optional milliseconds
9    let parts: Vec<&str> = time_str.split(':').collect();
10    if parts.len() < 3 || parts.len() > 4 {
11        return false;
12    }
13
14    // Extract hours, minutes, seconds, and optionally milliseconds
15    let hours = parts[0].parse::<u32>().ok();
16    let minutes = parts[1].parse::<u32>().ok();
17    let seconds_parts: Vec<&str> = parts[2].split('.').collect();
18    let seconds = seconds_parts[0].parse::<u32>().ok();
19    let milliseconds = if seconds_parts.len() == 2 {
20        seconds_parts[1].parse::<u32>().ok()
21    } else {
22        Some(0)
23    };
24
25    // Validate the parsed values
26    hours.is_some()
27        && minutes.is_some()
28        && seconds.is_some()
29        && milliseconds.is_some()
30        && hours.unwrap() < 24
31        && minutes.unwrap() < 60
32        && seconds.unwrap() < 60
33        && milliseconds.unwrap() < 1000
34}
35
36/// Check if String literal is matching SQL Date format: YYYY-MM-DD
37pub fn is_valid_date_format(date_str: &str) -> bool {
38    // Check length of the string
39    if date_str.len() != 10 {
40        return false;
41    }
42
43    // Split the string into year, month, and day
44    let parts: Vec<&str> = date_str.split('-').collect();
45    if parts.len() != 3 {
46        return false;
47    }
48
49    // Extract year, month, and day
50    let year = parts[0].parse::<u32>().ok();
51    let month = parts[1].parse::<u32>().ok();
52    let day = parts[2].parse::<u32>().ok();
53
54    // Validate the parsed values
55    year.is_some()
56        && month.is_some()
57        && day.is_some()
58        && year.unwrap() >= 1
59        && month.unwrap() >= 1
60        && month.unwrap() <= 12
61        && day.unwrap() >= 1
62        && day.unwrap() <= 31
63}
64
65/// Check if String literal is matching SQL Date format: YYYY-MM-DD HH:MM:SS or YYYY-MM-DD HH:MM:SS.SSS
66pub fn is_valid_datetime_format(datetime_str: &str) -> bool {
67    // Check length of the string
68    if !(19..=23).contains(&datetime_str.len()) {
69        return false;
70    }
71
72    // Split the string into date and time components
73    let parts: Vec<&str> = datetime_str.split_whitespace().collect();
74    if parts.len() != 2 {
75        return false;
76    }
77
78    // Check the validity of date and time components
79    is_valid_date_format(parts[0]) && is_valid_time_format(parts[1])
80}