use crate::error::Error;
use crate::model::{Span, ZonePeriod};
use crate::tags::{ParticipantContext, Role, Tag};
use crate::time::{Date, Duration, WorkingDays, ZoneName};
pub use crate::tags::ParticipantKind;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Participant {
pub id: String,
pub kind: ParticipantKind,
pub role: Role,
pub tags: Vec<Tag>,
pub zone: ZoneName,
pub zone_periods: Vec<ZonePeriod>,
pub calendar: Option<Vec<Block>>,
}
impl Participant {
#[must_use]
pub fn new(id: impl Into<String>, zone: ZoneName) -> Self {
Self {
id: id.into(),
kind: ParticipantKind::Person,
role: Role::Host,
tags: Vec::new(),
zone,
zone_periods: Vec::new(),
calendar: None,
}
}
#[must_use]
pub fn attendee(id: impl Into<String>, zone: ZoneName) -> Self {
let mut participant = Self::new(id, zone);
participant.role = Role::Attendee;
participant
}
#[must_use]
pub fn with_kind(mut self, kind: ParticipantKind) -> Self {
self.kind = kind;
self
}
#[must_use]
pub fn with_role(mut self, role: Role) -> Self {
self.role = role;
self
}
#[must_use]
pub fn with_tags(mut self, tags: Vec<Tag>) -> Self {
self.tags = tags;
self
}
#[must_use]
pub fn with_zone_periods(mut self, periods: Vec<ZonePeriod>) -> Self {
self.zone_periods = periods;
self
}
#[must_use]
pub fn with_calendar(mut self, blocks: Vec<Block>) -> Self {
self.calendar = Some(blocks);
self
}
#[must_use]
pub fn blocks(&self) -> &[Block] {
self.calendar.as_deref().unwrap_or(&[])
}
#[must_use]
pub fn context(&self) -> ParticipantContext<'_> {
ParticipantContext {
id: &self.id,
kind: self.kind,
role: self.role,
tags: &self.tags,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BlockStatus {
Free,
Tentative,
#[default]
Busy,
}
impl BlockStatus {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Free => "free",
Self::Tentative => "tentative",
Self::Busy => "busy",
}
}
}
impl core::fmt::Display for BlockStatus {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Block {
pub span: Span,
pub status: BlockStatus,
pub all_day: bool,
pub tags: Vec<Tag>,
pub event_key: Option<String>,
pub travel_before: Duration,
pub travel_after: Duration,
}
impl Block {
#[must_use]
pub fn new(span: Span) -> Self {
Self {
span,
status: BlockStatus::Busy,
all_day: false,
tags: Vec::new(),
event_key: None,
travel_before: Duration::ZERO,
travel_after: Duration::ZERO,
}
}
pub(crate) fn occupied_minutes(&self) -> (i64, i64) {
(
self.span
.start()
.minutes()
.saturating_sub(self.travel_before.minutes()),
self.span
.end()
.minutes()
.saturating_add(self.travel_after.minutes()),
)
}
#[must_use]
pub fn with_status(mut self, status: BlockStatus) -> Self {
self.status = status;
self
}
#[must_use]
pub fn with_all_day(mut self, all_day: bool) -> Self {
self.all_day = all_day;
self
}
#[must_use]
pub fn with_tags(mut self, tags: Vec<Tag>) -> Self {
self.tags = tags;
self
}
pub fn with_event_key(mut self, event_key: impl Into<String>) -> Result<Self, Error> {
let event_key = event_key.into();
if event_key.trim().is_empty() {
return Err(Error::EventKeyEmpty { event_key });
}
self.event_key = Some(event_key);
Ok(self)
}
#[must_use]
pub fn with_travel_before(mut self, before: Duration) -> Self {
self.travel_before = before;
self
}
#[must_use]
pub fn with_travel_after(mut self, after: Duration) -> Self {
self.travel_after = after;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CalendarSpec {
pub zone: ZoneName,
pub working_days: WorkingDays,
pub holidays: Vec<Date>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Occurrence {
pub event_key: String,
pub span: Span,
pub tags: Vec<Tag>,
pub participants: Vec<String>,
}
impl Occurrence {
pub fn new(
event_key: impl Into<String>,
span: Span,
participants: Vec<String>,
) -> Result<Self, Error> {
let event_key = event_key.into();
if event_key.trim().is_empty() {
return Err(Error::EventKeyEmpty { event_key });
}
if participants.is_empty() {
return Err(Error::OccurrenceWithoutParticipants { event_key });
}
Ok(Self {
event_key,
span,
tags: Vec::new(),
participants,
})
}
#[must_use]
pub fn with_tags(mut self, tags: Vec<Tag>) -> Self {
self.tags = tags;
self
}
}