use std::collections::BTreeMap;
use crate::error::Error;
use crate::model::{Participant, Span};
use crate::tags::{Role, Selector, Tag};
use crate::time::{Duration, Grid, Instant, ZoneName};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Goal {
pub parts: Vec<Part>,
pub grid: Option<Grid>,
pub candidates: Option<Vec<Instant>>,
pub search_window: Span,
pub zone: ZoneName,
pub display_zone: ZoneName,
}
impl Goal {
#[must_use]
pub fn new(zone: ZoneName, search_window: Span, parts: Vec<Part>) -> Self {
Self {
parts,
grid: None,
candidates: None,
search_window,
display_zone: zone.clone(),
zone,
}
}
#[must_use]
pub fn with_grid(mut self, grid: Grid) -> Self {
self.grid = Some(grid);
self
}
#[must_use]
pub fn with_candidates(mut self, candidates: Vec<Instant>) -> Self {
self.candidates = Some(candidates);
self
}
#[must_use]
pub fn with_display_zone(mut self, zone: ZoneName) -> Self {
self.display_zone = zone;
self
}
#[must_use]
pub fn part(&self) -> Option<&Part> {
match self.parts.as_slice() {
[only] => Some(only),
_ => None,
}
}
#[must_use]
pub fn single(
zone: ZoneName,
search_window: Span,
duration: Duration,
requirement: Requirement,
) -> Self {
Self::new(zone, search_window, vec![Part::new(duration, requirement)])
}
#[must_use]
pub fn single_part(zone: ZoneName, search_window: Span, part: Part) -> Self {
Self::new(zone, search_window, vec![part])
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Part {
pub id: String,
pub tags: Vec<Tag>,
pub duration: Duration,
pub requirement: Requirement,
pub travel: Travel,
}
impl Part {
#[must_use]
pub fn new(duration: Duration, requirement: Requirement) -> Self {
Self {
id: "main".to_owned(),
tags: Vec::new(),
duration,
requirement,
travel: Travel::default(),
}
}
#[must_use]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = id.into();
self
}
#[must_use]
pub fn with_tags(mut self, tags: Vec<Tag>) -> Self {
self.tags = tags;
self
}
#[must_use]
pub fn with_travel(mut self, travel: Travel) -> Self {
self.travel = travel;
self
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Requirement {
pub terms: Vec<Term>,
}
impl Requirement {
#[must_use]
pub fn new(terms: Vec<Term>) -> Self {
Self { terms }
}
#[must_use]
pub fn slots(&self) -> usize {
self.terms
.iter()
.map(Term::slots)
.fold(0_usize, usize::saturating_add)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Term {
Person {
id: String,
},
AtLeast {
count: u32,
of: Selector,
},
}
impl Term {
#[must_use]
pub fn person(id: impl Into<String>) -> Self {
Self::Person { id: id.into() }
}
#[must_use]
pub fn at_least(count: u32, of: Selector) -> Self {
Self::AtLeast { count, of }
}
#[must_use]
pub fn slots(&self) -> usize {
match self {
Self::Person { .. } => 1,
Self::AtLeast { count, .. } => usize::try_from(*count).unwrap_or(usize::MAX),
}
}
#[must_use]
pub fn filled_by(&self, participant: &Participant) -> bool {
if participant.role != Role::Host {
return false;
}
match self {
Self::Person { id } => participant.id == *id,
Self::AtLeast { of, .. } => of.matches_participant(participant.context()),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Travel {
pub before: Duration,
pub after: Duration,
pub by_participant: BTreeMap<String, ParticipantTravel>,
}
impl Travel {
#[must_use]
pub fn new(before: Duration, after: Duration) -> Self {
Self {
before,
after,
by_participant: BTreeMap::new(),
}
}
#[must_use]
pub fn with_participant(
mut self,
id: impl Into<String>,
before: Duration,
after: Duration,
) -> Self {
self.by_participant
.insert(id.into(), ParticipantTravel { before, after });
self
}
#[must_use]
pub fn for_participant(&self, participant: &str) -> (Duration, Duration) {
match self.by_participant.get(participant) {
Some(travel) => (travel.before, travel.after),
None => (self.before, self.after),
}
}
#[must_use]
pub fn largest_before(&self) -> Duration {
self.by_participant
.values()
.map(|travel| travel.before)
.fold(self.before, Duration::max)
}
#[must_use]
pub fn largest_after(&self) -> Duration {
self.by_participant
.values()
.map(|travel| travel.after)
.fold(self.after, Duration::max)
}
pub(crate) fn largest_before_for_participants(&self, participants: &[Participant]) -> Duration {
match participants.split_first() {
None => self.largest_before(),
Some((first, rest)) => rest.iter().fold(
self.for_participant(first.id.as_str()).0,
|largest, participant| largest.max(self.for_participant(participant.id.as_str()).0),
),
}
}
pub(crate) fn largest_after_for_participants(&self, participants: &[Participant]) -> Duration {
match participants.split_first() {
None => self.largest_after(),
Some((first, rest)) => rest.iter().fold(
self.for_participant(first.id.as_str()).1,
|largest, participant| largest.max(self.for_participant(participant.id.as_str()).1),
),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParticipantTravel {
pub before: Duration,
pub after: Duration,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ZonePeriod {
zone: ZoneName,
from: Instant,
until: Instant,
}
impl ZonePeriod {
pub fn new(zone: ZoneName, from: Instant, until: Instant) -> Result<Self, Error> {
if from >= until {
return Err(Error::ZonePeriodEmpty { from, until });
}
Ok(Self { zone, from, until })
}
#[must_use]
pub fn zone(&self) -> &ZoneName {
&self.zone
}
#[must_use]
pub const fn from(&self) -> Instant {
self.from
}
#[must_use]
pub const fn until(&self) -> Instant {
self.until
}
#[must_use]
pub fn contains(&self, instant: Instant) -> bool {
self.from <= instant && instant < self.until
}
}
#[cfg(test)]
mod tests {
use super::{Term, Travel};
use crate::model::Participant;
use crate::tags::{Role, Selector};
use crate::time::{Duration, ZoneName};
#[test]
fn an_attendee_fills_no_term() {
let mut participant = Participant::new("tom", ZoneName::new("UTC").unwrap());
assert!(Term::person("tom").filled_by(&participant));
assert!(Term::at_least(1, Selector::True).filled_by(&participant));
participant.role = Role::Attendee;
assert!(!Term::person("tom").filled_by(&participant));
assert!(!Term::at_least(1, Selector::True).filled_by(&participant));
}
#[test]
fn travel_builders_add_one_participant_beside_the_shared_travel() {
let travel = Travel::new(Duration::from_minutes(15), Duration::from_minutes(20))
.with_participant(
"tom",
Duration::from_minutes(35),
Duration::from_minutes(40),
);
assert_eq!(
travel.for_participant("tom"),
(Duration::from_minutes(35), Duration::from_minutes(40))
);
assert_eq!(
travel.for_participant("ana"),
(Duration::from_minutes(15), Duration::from_minutes(20))
);
assert_eq!(travel.largest_before(), Duration::from_minutes(35));
assert_eq!(travel.largest_after(), Duration::from_minutes(40));
}
#[test]
fn part_wide_travel_includes_a_default_larger_than_every_override() {
let travel = Travel::new(Duration::from_minutes(35), Duration::from_minutes(40))
.with_participant("tom", Duration::ZERO, Duration::ZERO);
assert_eq!(travel.largest_before(), Duration::from_minutes(35));
assert_eq!(travel.largest_after(), Duration::from_minutes(40));
assert_eq!(
travel.largest_before_for_participants(&[Participant::new(
"tom",
ZoneName::new("UTC").unwrap(),
)]),
Duration::ZERO
);
assert_eq!(
travel.largest_after_for_participants(&[Participant::new(
"tom",
ZoneName::new("UTC").unwrap(),
)]),
Duration::ZERO
);
}
#[test]
fn the_request_wide_envelope_falls_back_to_the_part_wide_maximum_without_participants() {
let travel = Travel::new(Duration::from_minutes(1), Duration::from_minutes(2))
.with_participant(
"tom",
Duration::from_minutes(35),
Duration::from_minutes(40),
);
assert_eq!(
travel.largest_before_for_participants(&[]),
travel.largest_before()
);
assert_eq!(
travel.largest_after_for_participants(&[]),
travel.largest_after()
);
assert_eq!(travel.largest_before(), Duration::from_minutes(35));
assert_eq!(travel.largest_after(), Duration::from_minutes(40));
let solo = [Participant::new("ana", ZoneName::new("UTC").unwrap())];
assert_eq!(
travel.largest_before_for_participants(&solo),
Duration::from_minutes(1)
);
assert_eq!(
travel.largest_after_for_participants(&solo),
Duration::from_minutes(2)
);
let both = [
solo[0].clone(),
Participant::new("tom", ZoneName::new("UTC").unwrap()),
];
assert_eq!(
travel.largest_before_for_participants(&both),
Duration::from_minutes(35)
);
assert_eq!(
travel.largest_after_for_participants(&both),
Duration::from_minutes(40)
);
}
}