Skip to main content

rustledger_plugin/native/plugins/
utils.rs

1//! Shared utility functions for native plugins.
2
3/// Increment a date string by one day.
4/// Returns None if the date format is invalid.
5pub fn increment_date(date: &str) -> Option<String> {
6    let parts: Vec<&str> = date.split('-').collect();
7    if parts.len() != 3 {
8        return None;
9    }
10
11    let year: i32 = parts[0].parse().ok()?;
12    let month: u32 = parts[1].parse().ok()?;
13    let day: u32 = parts[2].parse().ok()?;
14
15    // Simple date increment (handles month/year rollovers)
16    let days_in_month = match month {
17        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
18        4 | 6 | 9 | 11 => 30,
19        2 => {
20            if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
21                29
22            } else {
23                28
24            }
25        }
26        _ => return None,
27    };
28
29    let (new_year, new_month, new_day) = if day < days_in_month {
30        (year, month, day + 1)
31    } else if month < 12 {
32        (year, month + 1, 1)
33    } else {
34        (year + 1, 1, 1)
35    };
36
37    Some(format!("{new_year:04}-{new_month:02}-{new_day:02}"))
38}