mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 18:52:26 +00:00
Lots of clippy appeasement
This commit is contained in:
@@ -22,7 +22,7 @@ impl TryFrom<VcardContact> for AddressObject {
|
||||
fn try_from(vcard: VcardContact) -> Result<Self, Self::Error> {
|
||||
let uid = vcard
|
||||
.get_uid()
|
||||
.ok_or(Error::InvalidData("missing UID".to_owned()))?
|
||||
.ok_or_else(|| Error::InvalidData("missing UID".to_owned()))?
|
||||
.to_owned();
|
||||
let vcf = vcard.generate();
|
||||
Ok(Self {
|
||||
@@ -45,32 +45,38 @@ impl AddressObject {
|
||||
Ok(Self { id, vcf, vcard })
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_id(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_etag(&self) -> String {
|
||||
#[must_use]
|
||||
pub fn get_etag(&self) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(self.get_id());
|
||||
hasher.update(self.get_vcf());
|
||||
format!("\"{:x}\"", hasher.finalize())
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_vcf(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_vcf(&self) -> &str {
|
||||
&self.vcf
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> {
|
||||
#[must_use]
|
||||
pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> {
|
||||
let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?;
|
||||
CalDateTime::parse_vcard(prop).ok()
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> {
|
||||
#[must_use]
|
||||
pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> {
|
||||
let prop = self.vcard.get_property("BDAY")?.value.as_deref()?;
|
||||
CalDateTime::parse_vcard(prop).ok()
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_full_name(&self) -> Option<&str> {
|
||||
#[must_use]
|
||||
pub fn get_full_name(&self) -> Option<&str> {
|
||||
let prop = self.vcard.get_property("FN")?;
|
||||
prop.value.as_deref()
|
||||
}
|
||||
@@ -78,9 +84,7 @@ impl AddressObject {
|
||||
pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> {
|
||||
Ok(
|
||||
if let Some((anniversary, contains_year)) = self.get_anniversary() {
|
||||
let fullname = if let Some(name) = self.get_full_name() {
|
||||
name
|
||||
} else {
|
||||
let Some(fullname) = self.get_full_name() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let anniversary = anniversary.date();
|
||||
@@ -122,9 +126,7 @@ END:VCALENDAR",
|
||||
pub fn get_birthday_object(&self) -> Result<Option<CalendarObject>, Error> {
|
||||
Ok(
|
||||
if let Some((birthday, contains_year)) = self.get_birthday() {
|
||||
let fullname = if let Some(name) = self.get_full_name() {
|
||||
name
|
||||
} else {
|
||||
let Some(fullname) = self.get_full_name() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let birthday = birthday.date();
|
||||
|
||||
@@ -24,10 +24,12 @@ pub enum Error {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
#[must_use] pub const fn status_code(&self) -> StatusCode {
|
||||
#[must_use]
|
||||
pub const fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::InvalidData(_) => StatusCode::BAD_REQUEST,
|
||||
Self::MissingCalendar | Self::MissingContact => StatusCode::BAD_REQUEST,
|
||||
Self::InvalidData(_) | Self::MissingCalendar | Self::MissingContact => {
|
||||
StatusCode::BAD_REQUEST
|
||||
}
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ pub struct EventObject {
|
||||
}
|
||||
|
||||
impl EventObject {
|
||||
#[must_use] pub fn get_uid(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_uid(&self) -> &str {
|
||||
self.event.get_uid()
|
||||
}
|
||||
|
||||
@@ -70,9 +71,9 @@ impl EventObject {
|
||||
for prop in &self.event.properties {
|
||||
rrule_set = match prop.name.as_str() {
|
||||
"RRULE" => {
|
||||
let rrule = RRule::from_str(prop.value.as_ref().ok_or(Error::RRuleError(
|
||||
rrule::ParseError::MissingDateGenerationRules.into(),
|
||||
))?)?
|
||||
let rrule = RRule::from_str(prop.value.as_ref().ok_or_else(|| {
|
||||
Error::RRuleError(rrule::ParseError::MissingDateGenerationRules.into())
|
||||
})?)?
|
||||
.validate(dtstart)
|
||||
.unwrap();
|
||||
rrule_set.rrule(rrule)
|
||||
@@ -120,8 +121,8 @@ impl EventObject {
|
||||
date.format()
|
||||
};
|
||||
|
||||
for _override in overrides {
|
||||
if let Some(override_id) = &_override
|
||||
for ev_override in overrides {
|
||||
if let Some(override_id) = &ev_override
|
||||
.event
|
||||
.get_recurrence_id()
|
||||
.as_ref()
|
||||
@@ -131,7 +132,7 @@ impl EventObject {
|
||||
{
|
||||
// We have an override for this occurence
|
||||
//
|
||||
events.push(_override.event.clone());
|
||||
events.push(ev_override.event.clone());
|
||||
continue 'recurrence;
|
||||
}
|
||||
}
|
||||
@@ -185,7 +186,7 @@ mod tests {
|
||||
use crate::CalendarObject;
|
||||
use ical::generator::Emitter;
|
||||
|
||||
const ICS: &str = r#"BEGIN:VCALENDAR
|
||||
const ICS: &str = r"BEGIN:VCALENDAR
|
||||
CALSCALE:GREGORIAN
|
||||
VERSION:2.0
|
||||
BEGIN:VTIMEZONE
|
||||
@@ -203,7 +204,7 @@ SUMMARY:weekly stuff
|
||||
TRANSP:OPAQUE
|
||||
RRULE:FREQ=WEEKLY;COUNT=4;INTERVAL=2;BYDAY=TU,TH,SU
|
||||
END:VEVENT
|
||||
END:VCALENDAR"#;
|
||||
END:VCALENDAR";
|
||||
|
||||
const EXPANDED: [&str; 4] = [
|
||||
"BEGIN:VEVENT\r
|
||||
@@ -251,13 +252,7 @@ END:VEVENT\r\n",
|
||||
#[test]
|
||||
fn test_expand_recurrence() {
|
||||
let event = CalendarObject::from_ics(ICS.to_string()).unwrap();
|
||||
let (event, overrides) = if let crate::CalendarObjectComponent::Event(
|
||||
main_event,
|
||||
overrides,
|
||||
) = event.get_data()
|
||||
{
|
||||
(main_event, overrides)
|
||||
} else {
|
||||
let crate::CalendarObjectComponent::Event(event, overrides) = event.get_data() else {
|
||||
panic!()
|
||||
};
|
||||
|
||||
|
||||
@@ -26,7 +26,8 @@ pub enum CalendarObjectType {
|
||||
}
|
||||
|
||||
impl CalendarObjectType {
|
||||
#[must_use] pub const fn as_str(&self) -> &'static str {
|
||||
#[must_use]
|
||||
pub const fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Event => "VEVENT",
|
||||
Self::Todo => "VTODO",
|
||||
@@ -208,15 +209,18 @@ impl CalendarObject {
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use] pub const fn get_vtimezones(&self) -> &HashMap<String, IcalTimeZone> {
|
||||
#[must_use]
|
||||
pub const fn get_vtimezones(&self) -> &HashMap<String, IcalTimeZone> {
|
||||
&self.vtimezones
|
||||
}
|
||||
|
||||
#[must_use] pub const fn get_data(&self) -> &CalendarObjectComponent {
|
||||
#[must_use]
|
||||
pub const fn get_data(&self) -> &CalendarObjectComponent {
|
||||
&self.data
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_id(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_id(&self) -> &str {
|
||||
match &self.data {
|
||||
// We've made sure before that the first component exists and all components share the
|
||||
// same UID
|
||||
@@ -226,22 +230,26 @@ impl CalendarObject {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_etag(&self) -> String {
|
||||
#[must_use]
|
||||
pub fn get_etag(&self) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(self.get_id());
|
||||
hasher.update(self.get_ics());
|
||||
format!("\"{:x}\"", hasher.finalize())
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_ics(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_ics(&self) -> &str {
|
||||
&self.ics
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_component_name(&self) -> &str {
|
||||
#[must_use]
|
||||
pub fn get_component_name(&self) -> &str {
|
||||
self.get_object_type().as_str()
|
||||
}
|
||||
|
||||
#[must_use] pub fn get_object_type(&self) -> CalendarObjectType {
|
||||
#[must_use]
|
||||
pub fn get_object_type(&self) -> CalendarObjectType {
|
||||
(&self.data).into()
|
||||
}
|
||||
|
||||
@@ -249,7 +257,7 @@ impl CalendarObject {
|
||||
match &self.data {
|
||||
CalendarObjectComponent::Event(main_event, overrides) => Ok(overrides
|
||||
.iter()
|
||||
.chain([main_event].into_iter())
|
||||
.chain(std::iter::once(main_event))
|
||||
.map(super::event::EventObject::get_dtstart)
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
@@ -263,7 +271,7 @@ impl CalendarObject {
|
||||
match &self.data {
|
||||
CalendarObjectComponent::Event(main_event, overrides) => Ok(overrides
|
||||
.iter()
|
||||
.chain([main_event].into_iter())
|
||||
.chain(std::iter::once(main_event))
|
||||
.map(super::event::EventObject::get_last_occurence)
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
|
||||
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
|
||||
mod timestamp;
|
||||
mod timezone;
|
||||
pub use timestamp::*;
|
||||
|
||||
@@ -3,14 +3,11 @@ use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveDateTime, Naiv
|
||||
use chrono_tz::Tz;
|
||||
use derive_more::derive::Deref;
|
||||
use ical::property::Property;
|
||||
use lazy_static::lazy_static;
|
||||
use rustical_xml::{ValueDeserialize, ValueSerialize};
|
||||
use std::{borrow::Cow, collections::HashMap, ops::Add};
|
||||
use std::{borrow::Cow, collections::HashMap, ops::Add, sync::LazyLock};
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_VCARD_DATE_MM_DD: regex::Regex =
|
||||
regex::Regex::new(r"^--(?<m>\d{2})(?<d>\d{2})$").unwrap();
|
||||
}
|
||||
static RE_VCARD_DATE_MM_DD: LazyLock<regex::Regex> =
|
||||
LazyLock::new(|| regex::Regex::new(r"^--(?<m>\d{2})(?<d>\d{2})$").unwrap());
|
||||
|
||||
const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S";
|
||||
const UTC_DATE_TIME: &str = "%Y%m%dT%H%M%SZ";
|
||||
@@ -137,9 +134,7 @@ impl CalDateTime {
|
||||
let prop_value = prop
|
||||
.value
|
||||
.as_ref()
|
||||
.ok_or(CalDateTimeError::InvalidDatetimeFormat(
|
||||
"empty property".to_owned(),
|
||||
))?;
|
||||
.ok_or_else(|| CalDateTimeError::InvalidDatetimeFormat("empty property".into()))?;
|
||||
|
||||
let timezone = if let Some(tzid) = prop.get_param("TZID") {
|
||||
if let Some(timezone) = timezones.get(tzid) {
|
||||
@@ -158,7 +153,8 @@ impl CalDateTime {
|
||||
Self::parse(prop_value, timezone)
|
||||
}
|
||||
|
||||
#[must_use] pub fn format(&self) -> String {
|
||||
#[must_use]
|
||||
pub fn format(&self) -> String {
|
||||
match self {
|
||||
Self::DateTime(datetime) => match datetime.timezone() {
|
||||
ICalTimezone::Olson(chrono_tz::UTC) => datetime.format(UTC_DATE_TIME).to_string(),
|
||||
@@ -168,25 +164,29 @@ impl CalDateTime {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use] pub fn format_date(&self) -> String {
|
||||
#[must_use]
|
||||
pub fn format_date(&self) -> String {
|
||||
match self {
|
||||
Self::DateTime(datetime) => datetime.format(LOCAL_DATE).to_string(),
|
||||
Self::Date(date, _) => date.format(LOCAL_DATE).to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use] pub fn date(&self) -> NaiveDate {
|
||||
#[must_use]
|
||||
pub fn date(&self) -> NaiveDate {
|
||||
match self {
|
||||
Self::DateTime(datetime) => datetime.date_naive(),
|
||||
Self::Date(date, _) => date.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use] pub const fn is_date(&self) -> bool {
|
||||
#[must_use]
|
||||
pub const fn is_date(&self) -> bool {
|
||||
matches!(&self, Self::Date(_, _))
|
||||
}
|
||||
|
||||
#[must_use] pub fn as_datetime(&self) -> Cow<'_, DateTime<ICalTimezone>> {
|
||||
#[must_use]
|
||||
pub fn as_datetime(&self) -> Cow<'_, DateTime<ICalTimezone>> {
|
||||
match self {
|
||||
Self::DateTime(datetime) => Cow::Borrowed(datetime),
|
||||
Self::Date(date, tz) => Cow::Owned(
|
||||
@@ -219,8 +219,7 @@ impl CalDateTime {
|
||||
if let Ok(datetime) = NaiveDateTime::parse_from_str(value, UTC_DATE_TIME) {
|
||||
return Ok(datetime.and_utc().into());
|
||||
}
|
||||
let timezone = timezone
|
||||
.map_or(ICalTimezone::Local, ICalTimezone::Olson);
|
||||
let timezone = timezone.map_or(ICalTimezone::Local, ICalTimezone::Olson);
|
||||
if let Ok(date) = NaiveDate::parse_from_str(value, LOCAL_DATE) {
|
||||
return Ok(Self::Date(date, timezone));
|
||||
}
|
||||
@@ -251,7 +250,7 @@ impl CalDateTime {
|
||||
return Ok((
|
||||
Self::Date(
|
||||
NaiveDate::from_ymd_opt(year, month, day)
|
||||
.ok_or(CalDateTimeError::ParseError(value.to_string()))?,
|
||||
.ok_or_else(|| CalDateTimeError::ParseError(value.to_string()))?,
|
||||
ICalTimezone::Local,
|
||||
),
|
||||
false,
|
||||
@@ -260,11 +259,13 @@ impl CalDateTime {
|
||||
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
|
||||
}
|
||||
|
||||
#[must_use] pub fn utc(&self) -> DateTime<Utc> {
|
||||
#[must_use]
|
||||
pub fn utc(&self) -> DateTime<Utc> {
|
||||
self.as_datetime().to_utc()
|
||||
}
|
||||
|
||||
#[must_use] pub fn timezone(&self) -> ICalTimezone {
|
||||
#[must_use]
|
||||
pub fn timezone(&self) -> ICalTimezone {
|
||||
match &self {
|
||||
Self::DateTime(datetime) => datetime.timezone(),
|
||||
Self::Date(_, tz) => tz.to_owned(),
|
||||
@@ -349,9 +350,7 @@ impl Datelike for CalDateTime {
|
||||
fn with_month0(&self, month0: u32) -> Option<Self> {
|
||||
match &self {
|
||||
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_month0(month0)?)),
|
||||
Self::Date(date, tz) => {
|
||||
Some(Self::Date(date.with_month0(month0)?, tz.to_owned()))
|
||||
}
|
||||
Self::Date(date, tz) => Some(Self::Date(date.with_month0(month0)?, tz.to_owned())),
|
||||
}
|
||||
}
|
||||
fn with_day(&self, day: u32) -> Option<Self> {
|
||||
@@ -368,22 +367,14 @@ impl Datelike for CalDateTime {
|
||||
}
|
||||
fn with_ordinal(&self, ordinal: u32) -> Option<Self> {
|
||||
match &self {
|
||||
Self::DateTime(datetime) => {
|
||||
Some(Self::DateTime(datetime.with_ordinal(ordinal)?))
|
||||
}
|
||||
Self::Date(date, tz) => {
|
||||
Some(Self::Date(date.with_ordinal(ordinal)?, tz.to_owned()))
|
||||
}
|
||||
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal(ordinal)?)),
|
||||
Self::Date(date, tz) => Some(Self::Date(date.with_ordinal(ordinal)?, tz.to_owned())),
|
||||
}
|
||||
}
|
||||
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self> {
|
||||
match &self {
|
||||
Self::DateTime(datetime) => {
|
||||
Some(Self::DateTime(datetime.with_ordinal0(ordinal0)?))
|
||||
}
|
||||
Self::Date(date, tz) => {
|
||||
Some(Self::Date(date.with_ordinal0(ordinal0)?, tz.to_owned()))
|
||||
}
|
||||
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal0(ordinal0)?)),
|
||||
Self::Date(date, tz) => Some(Self::Date(date.with_ordinal0(ordinal0)?, tz.to_owned())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user