use crate::error::Diagnostic;
use crate::model::{Proposal, ProposalEvent, Span};
use crate::reasons::{Excluded, Reason};
use crate::time::{Date, Instant};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Offer {
pub events: Vec<OfferEvent>,
pub flags: Vec<OfferFlag>,
}
impl Offer {
#[must_use]
pub fn start(&self) -> Option<Instant> {
self.events.first().map(|event| event.start)
}
#[must_use]
pub fn end(&self) -> Option<Instant> {
self.events.last().map(|event| event.end)
}
#[must_use]
pub fn assigned_hosts(&self) -> Vec<&str> {
self.events
.iter()
.flat_map(|event| event.assignment.iter().flatten().map(String::as_str))
.collect()
}
#[must_use]
pub fn reserve(&self) -> Vec<&ReserveSpan> {
self.events
.iter()
.flat_map(|event| event.reserve.iter())
.collect()
}
#[must_use]
pub fn calendar_unknown(&self) -> bool {
self.flags.contains(&OfferFlag::CalendarUnknown)
}
#[must_use]
pub fn to_proposal(&self) -> Proposal {
Proposal {
events: self
.events
.iter()
.map(|event| ProposalEvent {
part: event.part.clone(),
start: event.start,
assignment: event.assignment.clone(),
})
.collect(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OfferEvent {
pub part: String,
pub start: Instant,
pub end: Instant,
pub eligible: Vec<Vec<String>>,
pub assignment: Vec<Vec<String>>,
pub reserve: Vec<ReserveSpan>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ReserveSpan {
pub participant: String,
pub kind: ReserveKind,
pub span: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ReserveKind {
Travel,
}
impl ReserveKind {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Travel => "travel",
}
}
}
impl core::fmt::Display for ReserveKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OfferFlag {
CalendarUnknown,
}
impl OfferFlag {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::CalendarUnknown => "calendar_unknown",
}
}
}
impl core::fmt::Display for OfferFlag {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DateCount {
pub date: Date,
pub offers: usize,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Response {
pub offers: Vec<Offer>,
pub dates: Vec<DateCount>,
pub truncated: bool,
pub excluded: Vec<Excluded>,
pub excluded_truncated: bool,
pub warnings: Vec<Diagnostic>,
pub meta: Meta,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Meta {
pub engine: String,
pub schema: u32,
pub tzdb: String,
}
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SlotCheck {
pub valid: bool,
pub reasons: Vec<Reason>,
pub flags: Vec<OfferFlag>,
pub reserve: Vec<ReserveSpan>,
pub warnings: Vec<Diagnostic>,
pub meta: Meta,
}
#[cfg(test)]
mod tests {
use super::{Offer, OfferEvent, ReserveKind, ReserveSpan};
use crate::model::Span;
use crate::time::Instant;
fn instant(minute: i64) -> Instant {
Instant::from_minutes(Instant::MIN.minutes() + minute).unwrap()
}
fn hold(participant: &str, from: i64, until: i64) -> ReserveSpan {
ReserveSpan {
participant: participant.to_owned(),
kind: ReserveKind::Travel,
span: Span::new(instant(from), instant(until)).unwrap(),
}
}
#[test]
fn an_offers_reserve_flattens_every_event_in_order() {
let offer = Offer {
events: vec![
OfferEvent {
part: "first".to_owned(),
start: instant(60),
end: instant(90),
eligible: Vec::new(),
assignment: Vec::new(),
reserve: vec![hold("ana", 30, 60)],
},
OfferEvent {
part: "second".to_owned(),
start: instant(120),
end: instant(150),
eligible: Vec::new(),
assignment: Vec::new(),
reserve: vec![hold("tom", 90, 120), hold("tom", 150, 180)],
},
],
flags: Vec::new(),
};
let reserved = offer.reserve();
assert_eq!(
reserved
.iter()
.map(|span| span.participant.as_str())
.collect::<Vec<_>>(),
["ana", "tom", "tom"]
);
assert_eq!(reserved[2].span.end(), instant(180));
}
}