use std::collections::BTreeMap;
use std::sync::Arc;
use crate::error::Error;
use crate::intervals::{IntervalSet, Span as MinuteSpan};
use crate::rest::RestActivity;
use crate::rules::Applicable;
use crate::time::{Instant, RuleZone, TimeZoneDatabase, WindowSpec, Zone, ZoneSchedule};
use super::blocked::BlockedSpan;
pub(crate) type RuleMinutes<'a> = Vec<(&'a str, Arc<IntervalSet>)>;
#[derive(Default)]
pub(crate) struct FixedMinutes<'a> {
windows: BTreeMap<usize, Arc<IntervalSet>>,
blackouts: BTreeMap<usize, Arc<IntervalSet>>,
home_windows: BTreeMap<(usize, &'a str), Arc<IntervalSet>>,
home_blackouts: BTreeMap<(usize, &'a str), Arc<IntervalSet>>,
}
pub(crate) struct WindowContext<'a, 's> {
pub(crate) db: &'s TimeZoneDatabase,
pub(crate) schedule: &'s ZoneSchedule,
pub(crate) home_zone_name: &'a str,
pub(crate) event: &'s Zone,
pub(crate) judged: MinuteSpan,
}
impl<'a> FixedMinutes<'a> {
pub(crate) fn window(
&mut self,
position: usize,
spec: &WindowSpec,
ctx: &WindowContext<'a, '_>,
) -> Result<Arc<IntervalSet>, Error> {
memoized_rule(
&mut self.windows,
&mut self.home_windows,
position,
spec.zone(),
ctx.schedule.periods().is_empty(),
ctx.home_zone_name,
|| {
let zones = spec.zone().resolve(ctx.db, ctx.schedule, ctx.event)?;
Ok(crate::time::build_minutes(spec, &zones, ctx.judged)?)
},
)
}
}
fn is_fixed(zone: &RuleZone) -> bool {
match zone {
RuleZone::Event | RuleZone::Named(_) => true,
RuleZone::Participant => false,
}
}
fn memoized_rule<'a>(
fixed_cache: &mut BTreeMap<usize, Arc<IntervalSet>>,
home_cache: &mut BTreeMap<(usize, &'a str), Arc<IntervalSet>>,
position: usize,
zone: &RuleZone,
fixed_home: bool,
home_zone_name: &'a str,
build: impl FnOnce() -> Result<IntervalSet, Error>,
) -> Result<Arc<IntervalSet>, Error> {
if is_fixed(zone) {
if let Some(minutes) = fixed_cache.get(&position) {
return Ok(Arc::clone(minutes));
}
let minutes = Arc::new(build()?);
fixed_cache.insert(position, Arc::clone(&minutes));
return Ok(minutes);
}
if fixed_home {
let key = (position, home_zone_name);
if let Some(minutes) = home_cache.get(&key) {
return Ok(Arc::clone(minutes));
}
let minutes = Arc::new(build()?);
home_cache.insert(key, Arc::clone(&minutes));
return Ok(minutes);
}
Ok(Arc::new(build()?))
}
#[allow(clippy::too_many_arguments)]
pub(super) fn allowed_time<'a>(
db: &TimeZoneDatabase,
rules: &Applicable<'a>,
schedule: &ZoneSchedule,
home_zone_name: &'a str,
event: &Zone,
judged: MinuteSpan,
keep_per_rule: bool,
fixed: &mut FixedMinutes<'a>,
) -> Result<(Option<Arc<IntervalSet>>, RuleMinutes<'a>), Error> {
let mut allowed: Option<Arc<IntervalSet>> = None;
let mut windows = if keep_per_rule {
Vec::with_capacity(rules.windows.len())
} else {
Vec::new()
};
let ctx = WindowContext {
db,
schedule,
home_zone_name,
event,
judged,
};
for (id, position, spec) in &rules.windows {
if !keep_per_rule && allowed.as_ref().is_some_and(|current| current.is_empty()) {
break;
}
let set = fixed.window(*position, spec, &ctx)?;
allowed = Some(match &allowed {
Some(current) => Arc::new(current.intersection(&set)),
None => Arc::clone(&set),
});
if keep_per_rule {
windows.push((*id, set));
}
}
Ok((allowed, windows))
}
#[allow(clippy::too_many_arguments)]
pub(super) fn blackout_time<'a>(
db: &TimeZoneDatabase,
rules: &Applicable<'a>,
schedule: &ZoneSchedule,
home_zone_name: &'a str,
event: &Zone,
judged: MinuteSpan,
keep_per_rule: bool,
fixed: &mut FixedMinutes<'a>,
) -> Result<(RuleMinutes<'a>, Vec<MinuteSpan>), Error> {
let mut blackouts = if keep_per_rule {
Vec::with_capacity(rules.blackouts.len())
} else {
Vec::new()
};
let mut spans = Vec::new();
let fixed_home = schedule.periods().is_empty();
for (id, position, spec) in &rules.blackouts {
let set = memoized_rule(
&mut fixed.blackouts,
&mut fixed.home_blackouts,
*position,
spec.zone(),
fixed_home,
home_zone_name,
|| {
let zones = spec.zone().resolve(db, schedule, event)?;
Ok(crate::time::build_minutes(*spec, &zones, judged)?)
},
)?;
spans.extend_from_slice(set.spans());
if keep_per_rule {
blackouts.push((*id, set));
}
}
Ok((blackouts, spans))
}
pub(super) fn build_usable(
allowed: Option<&Arc<IntervalSet>>,
mut forbidden: Vec<MinuteSpan>,
blocked: &[BlockedSpan<'_>],
rests: &[RestActivity<'_>],
schedule: &ZoneSchedule,
jumps: &[i64],
) -> Arc<IntervalSet> {
for block in blocked {
forbidden.push(block.widened);
}
for rest in rests {
if !rest.late_dates.is_empty() {
crate::rest::collect_matching_date_spans(
&rest.early,
schedule,
jumps,
|day| {
day.plus_days(-1)
.is_ok_and(|prev| rest.late_dates.contains(&prev))
},
&mut forbidden,
);
}
if !rest.early_dates.is_empty() {
crate::rest::collect_matching_date_spans(
&rest.late,
schedule,
jumps,
|day| {
day.plus_days(1)
.is_ok_and(|next| rest.early_dates.contains(&next))
},
&mut forbidden,
);
}
}
if forbidden.is_empty() {
return match allowed {
Some(allowed) => Arc::clone(allowed),
None => Arc::new(representable_minutes()),
};
}
let blocked_set = IntervalSet::from_spans(forbidden);
match allowed {
Some(allowed) => Arc::new(allowed.difference(&blocked_set)),
None => Arc::new(representable_minutes().difference(&blocked_set)),
}
}
fn representable_minutes() -> IntervalSet {
IntervalSet::from_spans(vec![
MinuteSpan::new(Instant::MIN.minutes(), Instant::MAX.minutes() + 1)
.expect("the supported minute range is non-empty"),
])
}
pub(super) fn representable(span: MinuteSpan) -> Option<MinuteSpan> {
MinuteSpan::new(
span.start().max(Instant::MIN.minutes()),
span.end().min(Instant::MAX.minutes() + 1),
)
.ok()
}