Skip to content

Commit

Permalink
Merge pull request #15 from hadronzoo/feature/calendar-name
Browse files Browse the repository at this point in the history
Support calendar properties
  • Loading branch information
hoodie committed Feb 25, 2020
2 parents 05e7c5d + 9ec3782 commit ce18c4c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/calendar.rs
@@ -1,5 +1,7 @@
use crate::components::*;

use crate::{Parameter, Property};
use chrono::Duration;
use std::convert::Into;
use std::fmt;
use std::iter::FromIterator;
Expand Down Expand Up @@ -45,6 +47,7 @@ impl CalendarElement {
/// You can `.add()` `Component`s to this.
#[derive(Default, Debug)]
pub struct Calendar {
properties: Vec<Property>,
components: Vec<CalendarElement>,
}

Expand All @@ -65,6 +68,12 @@ impl Calendar {
self.components.append(&mut other.components);
}

/// Append a given `Property` to the `Calendar`
pub fn append_property(&mut self, property: Property) -> &mut Self {
self.properties.push(property);
self
}

/// Extends this `Calendar` with the contends of another.
pub fn extend<T, U>(&mut self, other: T)
where
Expand All @@ -80,13 +89,50 @@ impl Calendar {
self
}

/// Set the NAME and X-WR-CALNAME `Property`s
pub fn name(&mut self, name: &str) -> &mut Self {
self.append_property(Property::new("NAME", name));
self.append_property(Property::new("X-WR-CALNAME", name));
self
}

/// Set the DESCRIPTION and X-WR-CALDESC `Property`s
pub fn description(&mut self, description: &str) -> &mut Self {
self.append_property(Property::new("DESCRIPTION", description));
self.append_property(Property::new("X-WR-CALDESC", description));
self
}

/// Set the TIMEZONE-ID and X-WR-TIMEZONE `Property`s
pub fn timezone(&mut self, timezone: &str) -> &mut Self {
self.append_property(Property::new("TIMEZONE-ID", timezone));
self.append_property(Property::new("X-WR-TIMEZONE", timezone));
self
}

/// Set the REFRESH-INTERVAL and X-PUBLISHED-TTL `Property`s
pub fn ttl(&mut self, duration: &Duration) -> &mut Self {
let duration_string = duration.to_string();
self.append_property(
Property::new("REFRESH-INTERVAL", duration_string.as_str())
.append_parameter(Parameter::new("VALUE", "DURATION"))
.done(),
);
self.append_property(Property::new("X-PUBLISHED-TTL", duration_string.as_str()));
self
}

/// Writes `Calendar` into a `Writer` using `std::fmt`.
fn fmt_write<W: fmt::Write>(&self, out: &mut W) -> Result<(), fmt::Error> {
write_crlf!(out, "BEGIN:VCALENDAR")?;
write_crlf!(out, "VERSION:2.0")?;
write_crlf!(out, "PRODID:ICALENDAR-RS")?;
write_crlf!(out, "CALSCALE:GREGORIAN")?;

for property in &self.properties {
property.fmt_write(out)?;
}

for component in &self.components {
component.fmt_write(out)?;
}
Expand Down Expand Up @@ -119,6 +165,7 @@ impl<C: Into<CalendarElement>> FromIterator<C> for Calendar {
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
Calendar {
components: iter.into_iter().map(Into::into).collect(),
..Default::default()
}
}
}
Expand Down

0 comments on commit ce18c4c

Please sign in to comment.