mod goal;
mod output;
mod participant;
mod rule;
pub use goal::{Goal, Part, ParticipantTravel, Requirement, Term, Travel, ZonePeriod};
pub use output::{
DateCount, Meta, Offer, OfferEvent, OfferFlag, ReserveKind, ReserveSpan, Response,
SCHEMA_VERSION, SlotCheck,
};
pub use participant::{Block, BlockStatus, CalendarSpec, Occurrence, Participant, ParticipantKind};
pub use rule::{
Cap, CapMeasure, CapPer, CapPeriod, Clearance, LeadTime, LeadTimeMax, Rest, Rule, RuleKind,
StatusEffect, StatusPolicy, StatusPolicyEntry,
};
pub use crate::tags::{Role, Selector, Tag};
use std::collections::BTreeMap;
use crate::error::Error;
use crate::time::Instant;
pub const DEFAULT_LIMIT: usize = 1_000;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Span {
start: Instant,
end: Instant,
}
impl Span {
pub const EVERYTHING: Self = Self {
start: Instant::MIN,
end: Instant::MAX,
};
pub fn new(start: Instant, end: Instant) -> Result<Self, Error> {
if start == end {
return Err(Error::SpanEmpty { start, end });
}
if start > end {
return Err(Error::SpanReversed { start, end });
}
Ok(Self { start, end })
}
#[must_use]
pub const fn start(&self) -> Instant {
self.start
}
#[must_use]
pub const fn end(&self) -> Instant {
self.end
}
#[must_use]
pub fn contains(&self, instant: Instant) -> bool {
self.start <= instant && instant < self.end
}
#[must_use]
pub fn minutes(&self) -> i64 {
self.end.minutes() - self.start.minutes()
}
#[must_use]
pub(crate) const fn to_minute_span(self) -> crate::intervals::Span {
crate::intervals::Span::from_valid(self.start.minutes(), self.end.minutes())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Request {
pub now: Instant,
pub goal: Goal,
pub participants: Vec<Participant>,
pub rules: Vec<Rule>,
pub occurrences: Vec<Occurrence>,
pub exclude_event_keys: Vec<String>,
pub calendars: BTreeMap<String, CalendarSpec>,
pub weights: BTreeMap<String, i32>,
pub limit: Option<usize>,
pub explain: Option<Explain>,
}
impl Request {
#[must_use]
pub fn new(now: Instant, goal: Goal) -> Self {
Self {
now,
goal,
participants: Vec::new(),
rules: Vec::new(),
occurrences: Vec::new(),
exclude_event_keys: Vec::new(),
calendars: BTreeMap::new(),
weights: BTreeMap::new(),
limit: None,
explain: None,
}
}
#[must_use]
pub fn with_participants(mut self, participants: Vec<Participant>) -> Self {
self.participants = participants;
self
}
#[must_use]
pub fn with_rules(mut self, rules: Vec<Rule>) -> Self {
self.rules = rules;
self
}
#[must_use]
pub fn with_occurrences(mut self, occurrences: Vec<Occurrence>) -> Self {
self.occurrences = occurrences;
self
}
#[must_use]
pub fn with_exclude_event_keys(mut self, keys: Vec<String>) -> Self {
self.exclude_event_keys = keys;
self
}
#[must_use]
pub fn exclude_event_key(mut self, key: impl Into<String>) -> Self {
self.exclude_event_keys.push(key.into());
self
}
#[must_use]
pub fn with_calendars(mut self, calendars: BTreeMap<String, CalendarSpec>) -> Self {
self.calendars = calendars;
self
}
#[must_use]
pub fn with_weights(mut self, weights: BTreeMap<String, i32>) -> Self {
self.weights = weights;
self
}
#[must_use]
pub fn with_limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
#[must_use]
pub fn with_explain(mut self, limit: usize) -> Self {
self.explain = Some(Explain::new(limit));
self
}
#[must_use]
pub fn limit(&self) -> usize {
self.limit.unwrap_or(DEFAULT_LIMIT)
}
#[must_use]
pub fn weight(&self, participant: &str) -> i32 {
self.weights.get(participant).copied().unwrap_or(0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Explain {
pub limit: usize,
}
impl Explain {
#[must_use]
pub const fn new(limit: usize) -> Self {
Self { limit }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Proposal {
pub events: Vec<ProposalEvent>,
}
impl Proposal {
#[must_use]
pub fn single(part: impl Into<String>, start: Instant, assignment: Vec<Vec<String>>) -> Self {
Self {
events: vec![ProposalEvent {
part: part.into(),
start,
assignment,
}],
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProposalEvent {
pub part: String,
pub start: Instant,
pub assignment: Vec<Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangesQuery {
pub now: Instant,
pub goal: Goal,
pub rules: Vec<Rule>,
pub participants: Vec<Participant>,
}
impl RangesQuery {
#[must_use]
pub fn new(now: Instant, goal: Goal) -> Self {
Self {
now,
goal,
rules: Vec::new(),
participants: Vec::new(),
}
}
#[must_use]
pub fn with_participants(mut self, participants: Vec<Participant>) -> Self {
self.participants = participants;
self
}
#[must_use]
pub fn with_rules(mut self, rules: Vec<Rule>) -> Self {
self.rules = rules;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RequiredRanges {
pub calendar: Span,
pub occurrences: Option<Span>,
}
#[cfg(test)]
mod tests {
use super::{DEFAULT_LIMIT, Span};
use crate::time::Instant;
#[test]
fn a_span_is_half_open_and_non_empty() {
let start = Instant::MIN;
let end = Instant::from_minutes(start.minutes() + 30).unwrap();
let span = Span::new(start, end).unwrap();
assert!(span.contains(start));
assert!(!span.contains(end));
assert_eq!(span.minutes(), 30);
assert_eq!(Span::new(start, start).unwrap_err().code(), "span_empty");
assert_eq!(Span::new(end, start).unwrap_err().code(), "span_reversed");
}
#[test]
fn the_default_limit_is_the_published_one() {
assert_eq!(DEFAULT_LIMIT, 1_000);
}
}