ezcal 0.3.4

Ergonomic iCalendar + vCard library for Rust
Documentation
use crate::common::property::Property;
use crate::error::Result;
use crate::ical::event::Event;
use crate::ical::todo::Todo;

const PRODID: &str = "-//ezcal//ezcal//EN";

/// An iCalendar VCALENDAR object.
#[derive(Debug, Clone)]
pub struct Calendar {
    pub(crate) prodid: String,
    pub(crate) version: String,
    pub(crate) calscale: Option<String>,
    pub(crate) method: Option<String>,
    pub(crate) name: Option<String>,
    pub(crate) events: Vec<Event>,
    pub(crate) todos: Vec<Todo>,
    pub(crate) extra_properties: Vec<Property>,
}

impl Calendar {
    /// Create a new calendar builder.
    pub fn new() -> Self {
        Self {
            prodid: PRODID.to_string(),
            version: "2.0".to_string(),
            calscale: None,
            method: None,
            name: None,
            events: Vec::new(),
            todos: Vec::new(),
            extra_properties: Vec::new(),
        }
    }

    /// Parse an iCalendar string into a `Calendar`.
    pub fn parse(input: &str) -> Result<Self> {
        crate::ical::parser::parse_calendar(input)
    }

    pub fn prodid(mut self, prodid: impl Into<String>) -> Self {
        self.prodid = prodid.into();
        self
    }

    pub fn calscale(mut self, calscale: impl Into<String>) -> Self {
        self.calscale = Some(calscale.into());
        self
    }

    pub fn method(mut self, method: impl Into<String>) -> Self {
        self.method = Some(method.into());
        self
    }

    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Add an event to the calendar.
    pub fn event(mut self, event: Event) -> Self {
        self.events.push(event);
        self
    }

    /// Add a todo to the calendar.
    pub fn todo(mut self, todo: Todo) -> Self {
        self.todos.push(todo);
        self
    }

    /// Add an extra property.
    pub fn property(mut self, prop: Property) -> Self {
        self.extra_properties.push(prop);
        self
    }

    /// Build the calendar (finalizes and returns self).
    pub fn build(self) -> Self {
        self
    }

    // --- Accessors ---

    pub fn events(&self) -> &[Event] {
        &self.events
    }

    pub fn todos(&self) -> &[Todo] {
        &self.todos
    }

    pub fn get_prodid(&self) -> &str {
        &self.prodid
    }

    pub fn get_version(&self) -> &str {
        &self.version
    }

    pub fn get_calscale(&self) -> Option<&str> {
        self.calscale.as_deref()
    }

    pub fn get_method(&self) -> Option<&str> {
        self.method.as_deref()
    }

    pub fn get_name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn get_extra_properties(&self) -> &[Property] {
        &self.extra_properties
    }
}

impl Default for Calendar {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for Calendar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", crate::ical::writer::write_calendar(self))
    }
}