summaryrefslogtreecommitdiffstats
path: root/src/duration.rs
blob: 3e0adfdda137f8bc0d95c4f9d31f97a40a2bccff (plain)
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
use crate::ical;
use std::cmp::Ordering;
use std::ffi::{CStr, CString};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Add, Deref};
use std::str::FromStr;

/// Duration type
///
/// A type that represents a duration of time
#[derive(Clone, Debug)]
pub struct IcalDuration {
    duration: ical::icaldurationtype,
}

impl IcalDuration {

    /// Convert a number of seconds to an IcalDuration
    pub fn from_seconds(seconds: i32) -> IcalDuration {
        let duration = unsafe { ical::icaldurationtype_from_int(seconds) };
        IcalDuration { duration }
    }

    /// Get the number of seconds the IcalDuration represents
    pub fn to_seconds(&self) -> i32 {
        unsafe { ical::icaldurationtype_as_int(self.duration) }
    }

    pub fn as_string(&self) -> String {
        let cstr = unsafe { CStr::from_ptr(ical::icaldurationtype_as_ical_string(self.duration)) };
        cstr.to_string_lossy().to_string()
    }
}

impl Deref for IcalDuration {
    type Target = ical::icaldurationtype;

    fn deref(&self) -> &ical::icaldurationtype {
        &self.duration
    }
}

impl Display for IcalDuration {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let cstr = unsafe { CStr::from_ptr(ical::icaldurationtype_as_ical_string(self.duration)) };
        let string = cstr.to_string_lossy();
        write!(f, "{}", string)
    }
}

impl FromStr for IcalDuration {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let c_str = CString::new(s).unwrap();
        let duration = unsafe {
            let duration = ical::icaldurationtype_from_string(c_str.as_ptr());
            if ical::icaldurationtype_is_null_duration(duration) == 0 {
                Some(duration)
            } else {
                None
            }
        };
        if let Some(duration) = duration {
            Ok(IcalDuration { duration })
        } else {
            return Err(format!("Could not parse duration {}", s));
        }
    }
}

impl PartialEq<IcalDuration> for IcalDuration {
    fn eq(&self, rhs: &IcalDuration) -> bool {
        self.to_seconds() == rhs.to_seconds()
    }
}

impl Eq for IcalDuration {}

impl PartialOrd for IcalDuration {
    fn partial_cmp(&self, rhs: &IcalDuration) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}

impl Ord for IcalDuration {
    fn cmp(&self, rhs: &IcalDuration) -> Ordering {
        let left = self.to_seconds();
        let right = rhs.to_seconds();
        if left == right {
            Ordering::Equal
        } else if left < right {
            Ordering::Less
        } else {
            Ordering::Greater
        }
    }
}

impl From<ical::icaldurationtype> for IcalDuration {
    fn from(duration: ical::icaldurationtype) -> IcalDuration {
        IcalDuration { duration }
    }
}

impl From<IcalDuration> for chrono::Duration {
    fn from(duration: IcalDuration) -> chrono::Duration {
        chrono::Duration::seconds(i64::from(duration.to_seconds()))
    }
}

impl From<chrono::Duration> for IcalDuration {
    fn from(duration: chrono::Duration) -> IcalDuration {
        IcalDuration::from_seconds(duration.num_seconds() as i32)
    }
}
impl Add for IcalDuration {
    type Output = IcalDuration;

    fn add(self, other: IcalDuration) -> IcalDuration {
        let seconds = self.to_seconds() + other.to_seconds();
        IcalDuration::from_seconds(seconds)
    }
}

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

    #[test]
    fn test_parse() {
        let duration = "PT86400S".parse::<IcalDuration>().unwrap();
        assert_eq!(IcalDuration::from_seconds(24 * 60 * 60), duration);
        assert_eq!(86400, duration.to_seconds());
    }

    #[test]
    fn test_parse_fail() {
        let duration = "swag".parse::<IcalDuration>();
        assert!(duration.is_err());
    }

    #[test]
    fn test_display() {
        let duration = IcalDuration::from_seconds(5 * 24 * 60 * 60 + 22 * 60 * 60 + 33 * 60 + 33);
        assert_eq!("P5DT22H33M33S", duration.to_string());
    }

    #[test]
    fn test_to_chrono() {
        let from_duration =
            IcalDuration::from_seconds(5 * 24 * 60 * 60 + 22 * 60 * 60 + 33 * 60 + 33);
        let duration: chrono::Duration = from_duration.into();
        assert_eq!(
            chrono::Duration::seconds(5 * 24 * 60 * 60 + 22 * 60 * 60 + 33 * 60 + 33),
            duration
        );
    }

    #[test]
    fn test_from_chrono() {
        let from_duration =
            chrono::Duration::seconds(5 * 24 * 60 * 60 + 22 * 60 * 60 + 33 * 60 + 33);
        let duration: IcalDuration = from_duration.into();
        assert_eq!(
            IcalDuration::from_seconds(5 * 24 * 60 * 60 + 22 * 60 * 60 + 33 * 60 + 33),
            duration
        );
    }

    #[test]
    fn test_add() {
        let fst = IcalDuration::from_seconds(123);
        let snd = IcalDuration::from_seconds(4567);

        let sum = fst + snd;

        assert_eq!(IcalDuration::from_seconds(123 + 4567), sum);
    }

    #[test]
    fn test_cmp() {
        let more = IcalDuration::from_seconds(49128);
        let less = IcalDuration::from_seconds(5);

        assert!(less == less);
        assert!(more == more);
        assert!(less < more);
        assert!(!(more < less));
        assert!(!(more == less));
    }
}