diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-10-08 19:33:50 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-10-08 19:33:50 +0200 |
commit | 30b3b597669a15d8814f21b33bf11e071c87dc0d (patch) | |
tree | 7e599cc711cc855c43c897a95a3f24789f540929 | |
parent | 0e667b788c3eeb5003d83407ef87b030874a470d (diff) |
Run cargo fmt
-rw-r--r-- | src/component.rs | 250 | ||||
-rw-r--r-- | src/duration.rs | 262 | ||||
-rw-r--r-- | src/errors.rs | 106 | ||||
-rw-r--r-- | src/lib.rs | 25 | ||||
-rw-r--r-- | src/property.rs | 70 | ||||
-rw-r--r-- | src/testdata.rs | 102 | ||||
-rw-r--r-- | src/testutils.rs | 14 | ||||
-rw-r--r-- | src/time.rs | 586 | ||||
-rw-r--r-- | src/timezone.rs | 218 | ||||
-rw-r--r-- | src/utils/dateutil.rs | 250 | ||||
-rw-r--r-- | src/utils/fileutil.rs | 73 | ||||
-rw-r--r-- | src/utils/lock.rs | 66 | ||||
-rw-r--r-- | src/utils/misc.rs | 68 | ||||
-rw-r--r-- | src/vcalendar.rs | 1125 | ||||
-rw-r--r-- | src/vevent.rs | 659 |
15 files changed, 1980 insertions, 1894 deletions
diff --git a/src/component.rs b/src/component.rs index 11edc58..7de364c 100644 --- a/src/component.rs +++ b/src/component.rs @@ -3,128 +3,146 @@ use std::ffi::CString; use super::IcalProperty; pub trait IcalComponent { - fn get_ptr(&self) -> *mut ical::icalcomponent; - fn as_component(&self) -> &dyn IcalComponent; - - fn get_property(&self, property_kind: ical::icalproperty_kind) -> Option<IcalProperty<'_>> { - let property = unsafe { - ical::icalcomponent_get_first_property(self.get_ptr(), property_kind) - }; - if !property.is_null() { - Some(IcalProperty::from_ptr(property, self.as_component())) - } else { - None + fn get_ptr(&self) -> *mut ical::icalcomponent; + fn as_component(&self) -> &dyn IcalComponent; + + fn get_property(&self, property_kind: ical::icalproperty_kind) -> Option<IcalProperty<'_>> { + let property = + unsafe { ical::icalcomponent_get_first_property(self.get_ptr(), property_kind) }; + if !property.is_null() { + Some(IcalProperty::from_ptr(property, self.as_component())) + } else { + None + } } - } - - fn get_properties(self: &Self, property_kind: ical::icalproperty_kind) -> Vec<IcalProperty<'_>> { - let mut properties = Vec::new(); - unsafe { - let mut property_ptr = ical::icalcomponent_get_first_property(self.get_ptr(), property_kind); - while !property_ptr.is_null() { - let property = IcalProperty::from_ptr(property_ptr, self.as_component()); - properties.push(property); - property_ptr = ical::icalcomponent_get_next_property(self.get_ptr(), property_kind); - } + + fn get_properties( + self: &Self, + property_kind: ical::icalproperty_kind, + ) -> Vec<IcalProperty<'_>> { + let mut properties = Vec::new(); + unsafe { + let mut property_ptr = + ical::icalcomponent_get_first_property(self.get_ptr(), property_kind); + while !property_ptr.is_null() { + let property = IcalProperty::from_ptr(property_ptr, self.as_component()); + properties.push(property); + property_ptr = ical::icalcomponent_get_next_property(self.get_ptr(), property_kind); + } + } + properties + } + + fn get_properties_all(&self) -> Vec<IcalProperty<'_>> { + self.get_properties(ical::icalproperty_kind_ICAL_ANY_PROPERTY) + } + + fn get_properties_by_name(&self, property_name: &str) -> Vec<IcalProperty> { + let property_kind = unsafe { + let c_str = CString::new(property_name).unwrap(); + ical::icalproperty_string_to_kind(c_str.as_ptr()) + }; + self.get_properties(property_kind) } - properties - } - - fn get_properties_all(&self) -> Vec<IcalProperty<'_>> { - self.get_properties(ical::icalproperty_kind_ICAL_ANY_PROPERTY) - } - - fn get_properties_by_name(&self, property_name: &str) -> Vec<IcalProperty> { - let property_kind = unsafe { - let c_str = CString::new(property_name).unwrap(); - ical::icalproperty_string_to_kind(c_str.as_ptr()) - }; - self.get_properties(property_kind) - } - - fn get_property_by_name(&self, property_name: &str) -> Option<IcalProperty> { - let property_kind = unsafe { - let c_str = CString::new(property_name).unwrap(); - ical::icalproperty_string_to_kind(c_str.as_ptr()) - }; - self.get_property(property_kind) - } - - unsafe fn remove_property_all(&self, kind: ical::icalproperty_kind) -> usize { - - unsafe fn remove_property_inner(comp: *mut ical::icalcomponent, kind: ical::icalproperty_kind) -> usize { - let mut count = 0; - let mut prop = ical::icalcomponent_get_first_property(comp, kind); - while !prop.is_null() { - ical::icalcomponent_remove_property(comp, prop); - count += 1; - prop = ical::icalcomponent_get_current_property(comp); - } - let mut inner_comp = ical::icalcomponent_get_first_component(comp, ical::icalcomponent_kind_ICAL_ANY_COMPONENT); - while !inner_comp.is_null() { - count += remove_property_inner(inner_comp, kind); - inner_comp = ical::icalcomponent_get_next_component(comp, ical::icalcomponent_kind_ICAL_ANY_COMPONENT) - } - count + + fn get_property_by_name(&self, property_name: &str) -> Option<IcalProperty> { + let property_kind = unsafe { + let c_str = CString::new(property_name).unwrap(); + ical::icalproperty_string_to_kind(c_str.as_ptr()) + }; + self.get_property(property_kind) } - let comp = self.get_ptr(); - remove_property_inner(comp, kind) - } + unsafe fn remove_property_all(&self, kind: ical::icalproperty_kind) -> usize { + unsafe fn remove_property_inner( + comp: *mut ical::icalcomponent, + kind: ical::icalproperty_kind, + ) -> usize { + let mut count = 0; + let mut prop = ical::icalcomponent_get_first_property(comp, kind); + while !prop.is_null() { + ical::icalcomponent_remove_property(comp, prop); + count += 1; + prop = ical::icalcomponent_get_current_property(comp); + } + let mut inner_comp = ical::icalcomponent_get_first_component( + comp, + ical::icalcomponent_kind_ICAL_ANY_COMPONENT, + ); + while !inner_comp.is_null() { + count += remove_property_inner(inner_comp, kind); + inner_comp = ical::icalcomponent_get_next_component( + comp, + ical::icalcomponent_kind_ICAL_ANY_COMPONENT, + ) + } + count + } + + let comp = self.get_ptr(); + remove_property_inner(comp, kind) + } } #[cfg(test)] mod tests { - use super::*; - use crate::testdata; - use crate::IcalVCalendar; - - #[test] - fn get_property_test() { - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); - let event = cal.get_principal_event(); - let prop_name = "SUMMARY"; - let prop_value: String = event.get_property_by_name(prop_name).unwrap().get_value(); - - assert_eq!("Festival International de Jazz de Montreal".to_string(), prop_value); - } - - #[test] - fn get_property_test_lastmodified() { - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_LASTMODIFIED, None).unwrap(); - let event = cal.get_principal_event(); - let prop_name = "LAST-MODIFIED"; - let prop_value: String = event.get_property_by_name(prop_name).unwrap().get_value(); - - assert_eq!("20070423T123432Z".to_string(), prop_value); - } - - #[test] - fn get_property_test_cal() { - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); - let prop_name = "PRODID"; - let prop_value: String = cal.get_property_by_name(prop_name).unwrap().get_value(); - - assert_eq!("-//ABC Corporation//NONSGML My Product//EN".to_string(), prop_value); - } - - #[test] - fn get_property_test_negative() { - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); - let event = cal.get_principal_event(); - let prop_name = "DESCRIPTION"; - let prop = event.get_property_by_name(prop_name); - - assert!(prop.is_none()); - } - - #[test] - fn get_property_by_name_test() { - let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); - let event = cal.get_principal_event(); - let prop_name = "NONSENSE"; - let prop = event.get_property_by_name(prop_name); - - assert!(prop.is_none()); - } + use super::*; + use crate::testdata; + use crate::IcalVCalendar; + + #[test] + fn get_property_test() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); + let event = cal.get_principal_event(); + let prop_name = "SUMMARY"; + let prop_value: String = event.get_property_by_name(prop_name).unwrap().get_value(); + + assert_eq!( + "Festival International de Jazz de Montreal".to_string(), + prop_value + ); + } + + #[test] + fn get_property_test_lastmodified() { + let cal = + IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY_LASTMODIFIED, None).unwrap(); + let event = cal.get_principal_event(); + let prop_name = "LAST-MODIFIED"; + let prop_value: String = event.get_property_by_name(prop_name).unwrap().get_value(); + + assert_eq!("20070423T123432Z".to_string(), prop_value); + } + + #[test] + fn get_property_test_cal() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); + let prop_name = "PRODID"; + let prop_value: String = cal.get_property_by_name(prop_name).unwrap().get_value(); + + assert_eq!( + "-//ABC Corporation//NONSGML My Product//EN".to_string(), + prop_value + ); + } + + #[test] + fn get_property_test_negative() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); + let event = cal.get_principal_event(); + let prop_name = "DESCRIPTION"; + let prop = event.get_property_by_name(prop_name); + + assert!(prop.is_none()); + } + + #[test] + fn get_property_by_name_test() { + let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); + let event = cal.get_principal_event(); + let prop_name = "NONSENSE"; + let prop = event.get_property_by_name(prop_name); + + assert!(prop.is_none()); + } } diff --git a/src/duration.rs b/src/duration.rs index 7ad1d13..d080584 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -1,177 +1,181 @@ -use std::ops::{Deref,Add}; -use std::ffi::{CStr,CString}; use crate::ical; -use std::fmt::{Error,Display,Formatter}; -use std::str::FromStr; use std::cmp::Ordering; +use std::ffi::{CStr, CString}; +use std::fmt::{Display, Error, Formatter}; +use std::ops::{Add, Deref}; +use std::str::FromStr; - -#[derive(Clone,Debug)] +#[derive(Clone, Debug)] pub struct IcalDuration { - duration: ical::icaldurationtype, + duration: ical::icaldurationtype, } impl IcalDuration { - pub fn from_seconds(seconds: i32) -> IcalDuration { - let duration = unsafe { ical::icaldurationtype_from_int(seconds) }; - IcalDuration{ duration } - } - - pub fn to_seconds(&self) -> i32 { - unsafe { ical::icaldurationtype_as_int(self.duration) } - } + pub fn from_seconds(seconds: i32) -> IcalDuration { + let duration = unsafe { ical::icaldurationtype_from_int(seconds) }; + IcalDuration { duration } + } + + pub fn to_seconds(&self) -> i32 { + unsafe { ical::icaldurationtype_as_int(self.duration) } + } } impl Deref for IcalDuration { - type Target = ical::icaldurationtype; + type Target = ical::icaldurationtype; - fn deref(&self) -> &ical::icaldurationtype { - &self.duration - } + 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) - } + 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)); - } - } + 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() - } + 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)) - } + 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 - } - } + 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 } - } + 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())) - } + 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) - } + 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) + 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)); - } + 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)); + } } diff --git a/src/errors.rs b/src/errors.rs index a4c16b0..7b66319 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,80 +4,86 @@ use std::fmt; #[derive(Debug)] pub struct KhError { - msg: String, - backtrace: Backtrace, - cause: Option<Box<dyn Error>>, + msg: String, + backtrace: Backtrace, + cause: Option<Box<dyn Error>>, } impl KhError { - pub fn new(msg: &str, cause: Option<Box<dyn Error>>) -> KhError { - KhError { - msg: msg.to_string(), - #[cfg(debug_assertions)] - backtrace: backtrace_strip_foreign(Backtrace::new()), - #[cfg(not(debug_assertions))] - backtrace: Backtrace::new_unresolved(), - cause + pub fn new(msg: &str, cause: Option<Box<dyn Error>>) -> KhError { + KhError { + msg: msg.to_string(), + #[cfg(debug_assertions)] + backtrace: backtrace_strip_foreign(Backtrace::new()), + #[cfg(not(debug_assertions))] + backtrace: Backtrace::new_unresolved(), + cause, + } } - } } #[cfg(debug_assertions)] fn backtrace_strip_foreign(backtrace: Backtrace) -> Backtrace { - use backtrace::BacktraceFrame; - let backtrace: Vec<BacktraceFrame> = backtrace.into(); - backtrace - .into_iter() - .filter(|frame| { - frame.symbols().iter().map(|symbol| { - symbol.name() - .and_then(|name| name.as_str()) - .map_or(false, |name| name.contains("khaleesi")) - }).any(|x| x) - }) - .collect::<Vec<BacktraceFrame>>().into() + use backtrace::BacktraceFrame; + let backtrace: Vec<BacktraceFrame> = backtrace.into(); + backtrace + .into_iter() + .filter(|frame| { + frame + .symbols() + .iter() + .map(|symbol| { + symbol + .name() + .and_then(|name| name.as_str()) + .map_or(false, |name| name.contains("khaleesi")) + }) + .any(|x| x) + }) + .collect::<Vec<BacktraceFrame>>() + .into() } impl fmt::Display for KhError { - #[cfg(debug_assertions)] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}\n{:?}", self.msg, self.backtrace) - } - #[cfg(not(debug_assertions))] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f , "{}", self.msg) - } + #[cfg(debug_assertions)] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}\n{:?}", self.msg, self.backtrace) + } + #[cfg(not(debug_assertions))] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.msg) + } } impl Error for KhError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - // lol idunno - self.cause.as_ref().map(|cause| &**cause) - } + fn source(&self) -> Option<&(dyn Error + 'static)> { + // lol idunno + self.cause.as_ref().map(|cause| &**cause) + } } impl From<&str> for KhError { - fn from(e: &str) -> KhError { - KhError::new(e, None) - } + fn from(e: &str) -> KhError { + KhError::new(e, None) + } } impl From<String> for KhError { - fn from(e: String) -> KhError { - KhError::new(&e, None) - } + fn from(e: String) -> KhError { + KhError::new(&e, None) + } } impl From<std::path::StripPrefixError> for KhError { - fn from(e: std::path::StripPrefixError) -> KhError { - let description = e.to_string(); - KhError::new(&description, Some(Box::new(e))) - } + fn from(e: std::path::StripPrefixError) -> KhError { + let description = e.to_string(); + KhError::new(&description, Some(Box::new(e))) + } } impl From<::std::io::Error> for KhError { - fn from(e: ::std::io::Error) -> KhError { - let description = e.to_string(); - KhError::new(&description, Some(Box::new(e))) - } + fn from(e: ::std::io::Error) -> KhError { + let description = e.to_string(); + KhError::new(&description, Some(Box::new(e))) + } } @@ -1,12 +1,20 @@ #![allow(clippy::redundant_closure)] // disable "redundant closure" lint -#[cfg(test)] #[macro_use] extern crate maplit; -#[cfg(test)] #[macro_use] extern crate pretty_assertions; -#[cfg(test)] #[macro_use] extern crate indoc; +#[cfg(test)] +#[macro_use] +extern crate maplit; +#[cfg(test)] +#[macro_use] +extern crate pretty_assertions; +#[cfg(test)] +#[macro_use] +extern crate indoc; extern crate serde_derive; -#[macro_use] extern crate log; -#[macro_use] extern crate lazy_static; +#[macro_use] +extern crate log; +#[macro_use] +extern crate lazy_static; extern crate ical; // libical does some weird, non-threadsafe things in timezone methods, notably @@ -16,18 +24,18 @@ extern crate ical; // https://github.com/libical/libical/commit/0ebf2d9a7183be94991c2681c6e3f009c64cf7cc use std::sync::Mutex; lazy_static! { - static ref TZ_MUTEX: Mutex<i32> = Mutex::new(0); + static ref TZ_MUTEX: Mutex<i32> = Mutex::new(0); } -pub mod errors; pub mod component; pub mod duration; +pub mod errors; pub mod property; pub mod time; pub mod timezone; +pub mod utils; pub mod vcalendar; pub mod vevent; -pub mod utils; #[cfg(test)] pub mod testdata; @@ -43,4 +51,3 @@ pub use crate::timezone::IcalTimeZone; pub use crate::vcalendar::IcalEventIter; pub use crate::vcalendar::IcalVCalendar; pub use crate::vevent::IcalVEvent; - diff --git a/src/property.rs b/src/property.rs index 133f953..55b5914 100644 --- a/src/property.rs +++ b/src/property.rs @@ -6,55 +6,57 @@ use super::component::IcalComponent; use crate::ical; pub struct IcalProperty<'a> { - pub ptr: *mut ical::icalproperty, - _parent: &'a dyn IcalComponent, + 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); + 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()) - } + 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 from_ptr(ptr: *mut ical::icalproperty, parent: &'a dyn IcalComponent) -> Self { + IcalProperty { + ptr, + _parent: parent, + } } - } - 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 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 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(&self) -> String { + unsafe { + let cstr = CStr::from_ptr(ical::icalproperty_get_value_as_string(self.ptr)); + cstr.to_string_lossy().into_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) + 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) + } + } +} diff --git a/src/testdata.rs b/src/testdata.rs index bbc013e..2c7b902 100644 --- a/src/testdata.rs +++ b/src/testdata.rs @@ -1,5 +1,6 @@ // from https://tools.ietf.org/html/rfc5545#section-3.6.1 -pub static TEST_EVENT_MULTIDAY_ALLDAY: &str = indoc!(" +pub static TEST_EVENT_MULTIDAY_ALLDAY: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -13,9 +14,11 @@ pub static TEST_EVENT_MULTIDAY_ALLDAY: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_EVENT_MULTIDAY: &str = indoc!(" +pub static TEST_EVENT_MULTIDAY: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -29,9 +32,11 @@ pub static TEST_EVENT_MULTIDAY: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_EVENT_MULTIDAY_LASTMODIFIED: &str = indoc!(" +pub static TEST_EVENT_MULTIDAY_LASTMODIFIED: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -46,9 +51,11 @@ pub static TEST_EVENT_MULTIDAY_LASTMODIFIED: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_DTSTART_ONLY_DATE: &str = indoc!(" +pub static TEST_DTSTART_ONLY_DATE: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -61,9 +68,11 @@ pub static TEST_DTSTART_ONLY_DATE: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_DTSTART_ONLY_DATETIME: &str = indoc!(" +pub static TEST_DTSTART_ONLY_DATETIME: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -76,9 +85,11 @@ pub static TEST_DTSTART_ONLY_DATETIME: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_EVENT_EMPTY_SUMMARY: &str = indoc!(" +pub static TEST_EVENT_EMPTY_SUMMARY: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -92,9 +103,11 @@ pub static TEST_EVENT_EMPTY_SUMMARY: &str = indoc!(" TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR -"); +" +); -pub static TEST_EVENT_NO_UID: &str = indoc!(" +pub static TEST_EVENT_NO_UID: &str = indoc!( + " BEGIN:VCALENDAR VERSION:2.0 PRODID:-//ABC Corporation//NONSGML My Product//EN @@ -107,9 +120,11 @@ pub static T |