1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Module that implements the decode_cf method for `i32`, `i64`, `f32`, `f64`,  `Vec<i32>`, `Vec<i64>`, `Vec<f32>` and `Vec<f64>`.

use crate::utils::get_datetime_and_unit_from_units;
use crate::{calendars::Calendar, datetime::CFDatetime};

/// Trait for decoding CFDatetime from units and calendar
pub trait CFDecoder {
    /// Decodes the given units and calendar into a CFDatetime.
    ///
    /// # Arguments
    ///
    /// * `units` - The units to decode.
    /// * `calendar` - The calendar to use for decoding.
    ///
    /// # Returns
    ///
    /// A Result containing the decoded CFDatetime if successful, or an Error if decoding fails.
    fn decode_cf(
        &self,
        units: &str,
        calendar: Calendar,
    ) -> Result<CFDatetime, crate::errors::Error>;
}

macro_rules! impl_cf_decoder {
    ($type:ty) => {
        impl CFDecoder for $type {
            fn decode_cf(
                &self,
                units: &str,
                calendar: Calendar,
            ) -> Result<CFDatetime, crate::errors::Error> {
                let (cf_datetime, unit) = get_datetime_and_unit_from_units(units, calendar)?;
                let duration = unit.to_duration(calendar);
                let result = (&cf_datetime + (&duration * *self))?;

                Ok(result)
            }
        }
    };
}

impl_cf_decoder!(i64);
impl_cf_decoder!(i32);
impl_cf_decoder!(f32);
impl_cf_decoder!(f64);

pub trait VecCFDecoder {
    fn decode_cf(
        &self,
        units: &str,
        calendar: Calendar,
    ) -> Result<Vec<CFDatetime>, crate::errors::Error>;
}

macro_rules! impl_vec_cf_decoder {
    ($type:ty) => {
        impl VecCFDecoder for Vec<$type> {
            fn decode_cf(
                &self,
                units: &str,
                calendar: Calendar,
            ) -> Result<Vec<CFDatetime>, crate::errors::Error> {
                let (cf_datetime, unit) = get_datetime_and_unit_from_units(units, calendar)?;
                let duration = unit.to_duration(calendar);
                let mut datetimes = Vec::with_capacity(self.len());
                for value in self {
                    let new_datetime = &cf_datetime + (&duration * *value);
                    datetimes.push(new_datetime?);
                }

                Ok(datetimes)
            }
        }
    };
}

impl_vec_cf_decoder!(i64);
impl_vec_cf_decoder!(i32);
impl_vec_cf_decoder!(f32);
impl_vec_cf_decoder!(f64);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decode_i64_cf_with_hms() {
        let to_decode = 2;
        let units = "days since 2000-01-01 00:00:00";
        let calendar = Calendar::Standard;

        let result = to_decode.decode_cf(units, calendar);

        // Assert
        assert!(result.is_ok());
        let cf_datetime = result.unwrap();
        let (year, month, day, hour, minute, second) = cf_datetime.ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 3, 0, 0, 0)
        );
    }
    #[test]
    fn test_decode_f64_cf_with_hms() {
        let to_decode: f64 = 2.0;
        let units = "hours since 2000-01-01 00:00:00";
        let calendar = Calendar::Standard;

        let result = to_decode.decode_cf(units, calendar);

        // Assert
        assert!(result.is_ok());
        let cf_datetime = result.unwrap();
        let (year, month, day, hour, minute, second) = cf_datetime.ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 2, 0, 0)
        );
    }

    #[test]
    fn test_decode_i32_vec_cf_with_hms() {
        let to_decode = vec![0, 1, 2];
        let units = "days since 2000-01-01 00:00:00";
        let calendar = Calendar::Standard;

        let datetimes_result = to_decode.decode_cf(units, calendar);

        // Assert
        assert!(datetimes_result.is_ok());
        let datetimes = datetimes_result.unwrap();
        for (i, datetime) in datetimes.iter().enumerate() {
            let (year, month, day, hour, minute, second) = datetime.ymd_hms().unwrap();
            assert_eq!(
                (year, month, day, hour, minute, second),
                (2000, 1, (i + 1) as u8, 0, 0, 0)
            );
        }
    }
    #[test]
    fn test_decode_f64_vec_cf_with_hms() {
        let to_decode: Vec<f64> = vec![1.0, 1.25, 1.50, 1.75, 2.0];
        let units = "hours since 2000-01-01 00:00:00";
        let calendar = Calendar::Standard;

        let datetimes_result = to_decode.decode_cf(units, calendar);

        // Assert
        assert!(datetimes_result.is_ok());
        let datetimes = datetimes_result.unwrap();
        let (year, month, day, hour, minute, second) = datetimes[0].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 1, 0, 0)
        );
        let (year, month, day, hour, minute, second) = datetimes[1].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 1, 15, 0)
        );
        let (year, month, day, hour, minute, second) = datetimes[2].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 1, 30, 0)
        );
        let (year, month, day, hour, minute, second) = datetimes[3].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 1, 45, 0)
        );
        let (year, month, day, hour, minute, second) = datetimes[4].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2000, 1, 1, 2, 0, 0)
        );
    }
    #[test]
    fn test_decode_95795_from_days() {
        // This error originates from Python bindings
        // From 95795.0 it gives (2232, 4, 11, 23, 57, 52) instead of (2232, 4, 12, 0, 0, 0)
        // But this worked for int
        let to_decode: Vec<f32> = vec![95795.0];
        let units = "days since 1970-01-01";
        let calendar = Calendar::Standard;

        let datetimes_result = to_decode.decode_cf(units, calendar);
        let datetimes = datetimes_result.unwrap();
        let (year, month, day, hour, minute, second) = datetimes[0].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2232, 4, 12, 0, 0, 0)
        );

        let to_decode: Vec<f64> = vec![95795.0];
        let units = "days since 1970-01-01";
        let calendar = Calendar::Standard;

        let datetimes_result = to_decode.decode_cf(units, calendar);
        let datetimes = datetimes_result.unwrap();
        let (year, month, day, hour, minute, second) = datetimes[0].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2232, 4, 12, 0, 0, 0)
        );
        let to_decode: Vec<i64> = vec![95795];
        let units = "days since 1970-01-01";
        let calendar = Calendar::Standard;

        let datetimes_result = to_decode.decode_cf(units, calendar);
        let datetimes = datetimes_result.unwrap();
        let (year, month, day, hour, minute, second) = datetimes[0].ymd_hms().unwrap();
        assert_eq!(
            (year, month, day, hour, minute, second),
            (2232, 4, 12, 0, 0, 0)
        );
    }
    // Add more test cases for other scenarios as needed
}