mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 02:22:21 +00:00
@@ -62,14 +62,14 @@ impl AddressObject {
|
|||||||
&self.vcf
|
&self.vcf
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_anniversary(&self) -> Option<CalDateTime> {
|
pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> {
|
||||||
let prop = self.vcard.get_property("ANNIVERSARY")?;
|
let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?;
|
||||||
CalDateTime::parse_prop(prop, &HashMap::default()).ok()
|
CalDateTime::parse_vcard(prop).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_birthday(&self) -> Option<CalDateTime> {
|
pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> {
|
||||||
let prop = self.vcard.get_property("BDAY")?;
|
let prop = self.vcard.get_property("BDAY")?.value.as_deref()?;
|
||||||
CalDateTime::parse_prop(prop, &HashMap::default()).ok()
|
CalDateTime::parse_vcard(prop).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_full_name(&self) -> Option<&str> {
|
pub fn get_full_name(&self) -> Option<&str> {
|
||||||
@@ -78,25 +78,27 @@ impl AddressObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> {
|
pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> {
|
||||||
Ok(if let Some(anniversary) = self.get_anniversary() {
|
Ok(
|
||||||
let fullname = if let Some(name) = self.get_full_name() {
|
if let Some((anniversary, contains_year)) = self.get_anniversary() {
|
||||||
name
|
let fullname = if let Some(name) = self.get_full_name() {
|
||||||
} else {
|
name
|
||||||
return Ok(None);
|
} else {
|
||||||
};
|
return Ok(None);
|
||||||
let anniversary = anniversary.date();
|
};
|
||||||
let year = anniversary.year();
|
let anniversary = anniversary.date();
|
||||||
let anniversary_start = anniversary.format(LOCAL_DATE);
|
let year = contains_year.then_some(anniversary.year());
|
||||||
let anniversary_end = anniversary
|
let anniversary_start = anniversary.format(LOCAL_DATE);
|
||||||
.succ_opt()
|
let anniversary_end = anniversary
|
||||||
.unwrap_or(anniversary)
|
.succ_opt()
|
||||||
.format(LOCAL_DATE);
|
.unwrap_or(anniversary)
|
||||||
let uid = format!("{}-anniversary", self.get_id());
|
.format(LOCAL_DATE);
|
||||||
|
let uid = format!("{}-anniversary", self.get_id());
|
||||||
|
|
||||||
Some(CalendarObject::from_ics(
|
let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default();
|
||||||
uid.clone(),
|
Some(CalendarObject::from_ics(
|
||||||
format!(
|
uid.clone(),
|
||||||
r#"BEGIN:VCALENDAR
|
format!(
|
||||||
|
r#"BEGIN:VCALENDAR
|
||||||
VERSION:2.0
|
VERSION:2.0
|
||||||
CALSCALE:GREGORIAN
|
CALSCALE:GREGORIAN
|
||||||
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
|
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
|
||||||
@@ -105,39 +107,42 @@ DTSTART;VALUE=DATE:{anniversary_start}
|
|||||||
DTEND;VALUE=DATE:{anniversary_end}
|
DTEND;VALUE=DATE:{anniversary_end}
|
||||||
UID:{uid}
|
UID:{uid}
|
||||||
RRULE:FREQ=YEARLY
|
RRULE:FREQ=YEARLY
|
||||||
SUMMARY:💍 {fullname} ({year})
|
SUMMARY:💍 {fullname}{year_suffix}
|
||||||
TRANSP:TRANSPARENT
|
TRANSP:TRANSPARENT
|
||||||
BEGIN:VALARM
|
BEGIN:VALARM
|
||||||
TRIGGER;VALUE=DURATION:-PT0M
|
TRIGGER;VALUE=DURATION:-PT0M
|
||||||
ACTION:DISPLAY
|
ACTION:DISPLAY
|
||||||
DESCRIPTION:💍 {fullname} ({year})
|
DESCRIPTION:💍 {fullname}{year_suffix}
|
||||||
END:VALARM
|
END:VALARM
|
||||||
END:VEVENT
|
END:VEVENT
|
||||||
END:VCALENDAR"#,
|
END:VCALENDAR"#,
|
||||||
),
|
),
|
||||||
)?)
|
)?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_birthday_object(&self) -> Result<Option<CalendarObject>, Error> {
|
pub fn get_birthday_object(&self) -> Result<Option<CalendarObject>, Error> {
|
||||||
Ok(if let Some(birthday) = self.get_birthday() {
|
Ok(
|
||||||
let fullname = if let Some(name) = self.get_full_name() {
|
if let Some((birthday, contains_year)) = self.get_birthday() {
|
||||||
name
|
let fullname = if let Some(name) = self.get_full_name() {
|
||||||
} else {
|
name
|
||||||
return Ok(None);
|
} else {
|
||||||
};
|
return Ok(None);
|
||||||
let birthday = birthday.date();
|
};
|
||||||
let year = birthday.year();
|
let birthday = birthday.date();
|
||||||
let birthday_start = birthday.format(LOCAL_DATE);
|
let year = contains_year.then_some(birthday.year());
|
||||||
let birthday_end = birthday.succ_opt().unwrap_or(birthday).format(LOCAL_DATE);
|
let birthday_start = birthday.format(LOCAL_DATE);
|
||||||
let uid = format!("{}-birthday", self.get_id());
|
let birthday_end = birthday.succ_opt().unwrap_or(birthday).format(LOCAL_DATE);
|
||||||
|
let uid = format!("{}-birthday", self.get_id());
|
||||||
|
|
||||||
Some(CalendarObject::from_ics(
|
let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default();
|
||||||
uid.clone(),
|
Some(CalendarObject::from_ics(
|
||||||
format!(
|
uid.clone(),
|
||||||
r#"BEGIN:VCALENDAR
|
format!(
|
||||||
|
r#"BEGIN:VCALENDAR
|
||||||
VERSION:2.0
|
VERSION:2.0
|
||||||
CALSCALE:GREGORIAN
|
CALSCALE:GREGORIAN
|
||||||
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
|
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
|
||||||
@@ -146,20 +151,21 @@ DTSTART;VALUE=DATE:{birthday_start}
|
|||||||
DTEND;VALUE=DATE:{birthday_end}
|
DTEND;VALUE=DATE:{birthday_end}
|
||||||
UID:{uid}
|
UID:{uid}
|
||||||
RRULE:FREQ=YEARLY
|
RRULE:FREQ=YEARLY
|
||||||
SUMMARY:🎂 {fullname} ({year})
|
SUMMARY:🎂 {fullname}{year_suffix}
|
||||||
TRANSP:TRANSPARENT
|
TRANSP:TRANSPARENT
|
||||||
BEGIN:VALARM
|
BEGIN:VALARM
|
||||||
TRIGGER;VALUE=DURATION:-PT0M
|
TRIGGER;VALUE=DURATION:-PT0M
|
||||||
ACTION:DISPLAY
|
ACTION:DISPLAY
|
||||||
DESCRIPTION:🎂 {fullname} ({year})
|
DESCRIPTION:🎂 {fullname}{year_suffix}
|
||||||
END:VALARM
|
END:VALARM
|
||||||
END:VEVENT
|
END:VEVENT
|
||||||
END:VCALENDAR"#,
|
END:VCALENDAR"#,
|
||||||
),
|
),
|
||||||
)?)
|
)?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get significant dates associated with this address object
|
/// Get significant dates associated with this address object
|
||||||
|
|||||||
@@ -254,6 +254,16 @@ impl CalDateTime {
|
|||||||
if let Ok(date) = NaiveDate::parse_from_str(value, "%Y%m%d") {
|
if let Ok(date) = NaiveDate::parse_from_str(value, "%Y%m%d") {
|
||||||
return Ok(CalDateTime::Date(date, timezone));
|
return Ok(CalDateTime::Date(date, timezone));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also returns whether the date contains a year
|
||||||
|
pub fn parse_vcard(value: &str) -> Result<(Self, bool), CalDateTimeError> {
|
||||||
|
if let Ok(datetime) = Self::parse(value, None) {
|
||||||
|
return Ok((datetime, true));
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(captures) = RE_VCARD_DATE_MM_DD.captures(value) {
|
if let Some(captures) = RE_VCARD_DATE_MM_DD.captures(value) {
|
||||||
// Because 1972 is a leap year
|
// Because 1972 is a leap year
|
||||||
let year = 1972;
|
let year = 1972;
|
||||||
@@ -261,13 +271,15 @@ impl CalDateTime {
|
|||||||
let month = captures.name("m").unwrap().as_str().parse().ok().unwrap();
|
let month = captures.name("m").unwrap().as_str().parse().ok().unwrap();
|
||||||
let day = captures.name("d").unwrap().as_str().parse().ok().unwrap();
|
let day = captures.name("d").unwrap().as_str().parse().ok().unwrap();
|
||||||
|
|
||||||
return Ok(CalDateTime::Date(
|
return Ok((
|
||||||
NaiveDate::from_ymd_opt(year, month, day)
|
CalDateTime::Date(
|
||||||
.ok_or(CalDateTimeError::ParseError(value.to_string()))?,
|
NaiveDate::from_ymd_opt(year, month, day)
|
||||||
timezone,
|
.ok_or(CalDateTimeError::ParseError(value.to_string()))?,
|
||||||
|
CalTimezone::Local,
|
||||||
|
),
|
||||||
|
false,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
|
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user