summaryrefslogtreecommitdiffstats
path: root/src/property.rs
blob: 63e754014f13a6fd3ab51acfbc49e8b097249aed (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
use chrono::NaiveDate;
use std::ffi::CStr;
use std::fmt;

use super::component::IcalComponent;
use crate::ical;

/// A property in the ical data
///
/// This type represents a single property (name + value).
pub struct IcalProperty<'a> {
    pub ptr: *mut ical::icalproperty,
    _parent: &'a dyn IcalComponent,
}

impl<'a> Drop for IcalProperty<'a> {
    fn drop(&mut self) {
        unsafe {
            ical::icalproperty_free(self.ptr);
        }
    }
}

impl<'a> fmt::Debug for IcalProperty<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_ical_string())
    }
}

impl<'a> IcalProperty<'a> {
    pub fn from_ptr(ptr: *mut ical::icalproperty, parent: &'a dyn IcalComponent) -> Self {
        IcalProperty {
            ptr,
            _parent: parent,
        }
    }

    pub fn get_name(&self) -> String {
        unsafe {
            let cstr = CStr::from_ptr(ical::icalproperty_get_property_name(self.ptr));
            cstr.to_string_lossy().into_owned()
        }
    }

    pub fn get_value(&self) -> String {
        unsafe {
            let cstr = CStr::from_ptr(ical::icalproperty_get_value_as_string(self.ptr));
            cstr.to_string_lossy().into_owned()
        }
    }

    pub fn as_ical_string(&self) -> String {
        unsafe {
            let cstr = CStr::from_ptr(ical::icalproperty_as_ical_string(self.ptr));
            cstr.to_string_lossy().trim().to_owned()
        }
    }

    pub fn get_value_as_date(&self) -> Option<NaiveDate> {
        unsafe {
            let date = ical::icaltime_from_string(ical::icalproperty_get_value_as_string(self.ptr));
            NaiveDate::from_ymd_opt(date.year, date.month as u32, date.day as u32)
        }
    }
}