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
use integer_encoding::VarInt;
use std::io::{Cursor, Write};
use thiserror::Error;

use crate::frame::{Serialize, Version};

/// Possible `Duration` creation error.
#[derive(Debug, Error, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum DurationCreationError {
    #[error(
        "All values must be either negative or positive, got {months} months, {days} days, {nanoseconds} nanoseconds"
    )]
    MixedPositiveAndNegative {
        months: i32,
        days: i32,
        nanoseconds: i64,
    },
}

/// Cassandra Duration type. A duration stores separately months, days, and seconds due to the fact
/// that the number of days in a month varies, and a day can have 23 or 25 hours if a daylight
/// saving is involved.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Duration {
    months: i32,
    days: i32,
    nanoseconds: i64,
}

impl Duration {
    pub fn new(months: i32, days: i32, nanoseconds: i64) -> Result<Self, DurationCreationError> {
        if (months < 0 || days < 0 || nanoseconds < 0)
            && (months > 0 || days > 0 || nanoseconds > 0)
        {
            Err(DurationCreationError::MixedPositiveAndNegative {
                months,
                days,
                nanoseconds,
            })
        } else {
            Ok(Self {
                months,
                days,
                nanoseconds,
            })
        }
    }

    pub fn months(&self) -> i32 {
        self.months
    }

    pub fn days(&self) -> i32 {
        self.days
    }

    pub fn nanoseconds(&self) -> i64 {
        self.nanoseconds
    }
}

impl Serialize for Duration {
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
        let month_space = self.months.required_space();
        let day_space = self.days.required_space();

        let mut buffer = vec![0u8; month_space + day_space + self.nanoseconds.required_space()];

        self.months.encode_var(&mut buffer);
        self.days.encode_var(&mut buffer[month_space..]);
        self.nanoseconds
            .encode_var(&mut buffer[(month_space + day_space)..]);

        let _ = cursor.write(&buffer);
    }
}

#[cfg(test)]
mod tests {
    use crate::frame::{Serialize, Version};
    use crate::types::duration::Duration;

    #[test]
    fn should_serialize_duration() {
        let duration = Duration::new(100, 200, 300).unwrap();
        assert_eq!(
            duration.serialize_to_vec(Version::V5),
            vec![200, 1, 144, 3, 216, 4]
        );
    }
}