1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! The scheduling domain model: what a request is and what a response is.
//!
//! These are the types every surface parses into and the engine evaluates. They
//! are independent of any time zone database and of the engine's limits: a
//! request names zones and calendars, and [`crate::Engine`] resolves the names
//! when it evaluates. That is what lets a request be built, stored and parsed
//! before any database is chosen, and what lets `validate` report an unknown
//! zone instead of a constructor failing.
//!
//! Shapes that will grow already allow it, so growth is additive within one major
//! version: a goal has a list of parts, an offer a list of events, and an
//! assignment one list per term. What is not list-shaped is closed against
//! construction but open to new members: [`Response`], [`SlotCheck`] and the
//! diagnostics are `#[non_exhaustive]`, [`Error`] and its time and
//! interval companions are non-exhaustive enums, and [`crate::Limits`] is a
//! non-exhaustive struct built through its `with_*` methods. A new response
//! field, reason code, error variant or limit is therefore a minor release, not a
//! breaking one.
//!
//! ```
//! use opportune::model::{Goal, Part, Requirement, Request, Span, Term};
//! use opportune::time::{Duration, Instant, ZoneName};
//!
//! let search = Span::new(Instant::MIN, Instant::MAX).unwrap();
//! let part = Part::new(
//! Duration::from_minutes(30),
//! Requirement::new(vec![Term::person("rich")]),
//! );
//! let request = Request::new(Instant::MIN, Goal::new(ZoneName::new("UTC").unwrap(), search, vec![part]));
//! assert_eq!(request.limit(), 1_000);
//! assert_eq!(request.goal.parts[0].id, "main");
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
// The tag vocabulary's three types a model value names: `Participant.role`, and
// `Rule.who`, `Term::AtLeast.of` and `StatusPolicyEntry.tags`. They travel with
// the domain types so a caller can name every field type of every value here
// from `opportune::model` alone, as it already can for `ParticipantKind`.
pub use crate;
use BTreeMap;
use crateError;
use crateInstant;
/// The default maximum number of offers returned (`1,000`).
pub const DEFAULT_LIMIT: usize = 1_000;
/// A half-open span of instants, `[start, end)`, at minute resolution.
///
/// Spans are non-empty by construction, so every span covers at least one
/// minute and a zero-length or inverted pair is rejected where it is written
/// rather than silently covering nothing.
///
/// ```
/// use opportune::model::Span;
/// use opportune::time::Instant;
///
/// let span = Span::new(Instant::MIN, Instant::MAX).unwrap();
/// assert!(span.contains(Instant::MIN));
/// assert!(!span.contains(Instant::MAX));
/// assert!(Span::new(Instant::MAX, Instant::MIN).is_err());
/// ```
/// One scheduling request: the current instant, what is being booked, who is
/// involved and the rules that decide availability.
///
/// `calendars` holds the named working calendars that a `lead_time` maximum's
/// working-day form refers to. The time zone database and the engine's limits
/// belong to the engine, not to the request, so a request is a value that can be
/// built and stored without either.
///
/// ```
/// use opportune::model::{Explain, Goal, Part, Requirement, Request, Span, Term};
/// use opportune::time::{Duration, Instant, ZoneName};
///
/// let part = Part::new(
/// Duration::from_minutes(30),
/// Requirement::new(vec![Term::person("rich")]),
/// );
/// let goal = Goal::new(
/// ZoneName::new("Europe/London").unwrap(),
/// Span::new(Instant::MIN, Instant::MAX).unwrap(),
/// vec![part],
/// );
/// let mut request = Request::new(Instant::MAX, goal);
/// request.limit = Some(20);
/// request.explain = Some(Explain::new(200));
/// request.weights.insert("rich".to_owned(), 3);
/// assert_eq!(request.limit(), 20);
/// assert_eq!(request.explain.map(|explain| explain.limit), Some(200));
/// assert_eq!(request.weight("rich"), 3);
/// assert_eq!(request.weight("ana"), 0);
/// ```
/// How much exclusion detail a search returns.
///
/// Entries are atomic: a start is reported with every reason it has, or not at
/// all, so an explanation is never cut in the middle of one start's reasons.
/// A booking a caller proposes to confirm, checked with
/// [`Engine::check_slot`](crate::Engine::check_slot).
///
/// ```
/// use opportune::model::Proposal;
/// use opportune::time::Instant;
///
/// let proposal = Proposal::single("main", Instant::MIN, vec![vec!["ana".to_owned()]]);
/// assert_eq!(proposal.events.len(), 1);
/// ```
/// One event of a [`Proposal`]: which part it books, when, and who takes it.
/// What calendar and occurrence data a request needs, before fetching any.
///
/// The query carries the request's `now`, goal, rules and participants (without
/// calendars), because the range depends on the booking's travel and the
/// clearance between events — not on the blocks themselves.
///
/// ```
/// use opportune::model::{Goal, RangesQuery, Span};
/// use opportune::time::{Instant, ZoneName};
///
/// let goal = Goal::new(
/// ZoneName::new("UTC").unwrap(),
/// Span::new(Instant::MIN, Instant::MAX).unwrap(),
/// Vec::new(),
/// );
/// let query = RangesQuery::new(Instant::MIN, goal);
/// assert_eq!(query.now, Instant::MIN);
/// ```
/// One UTC envelope of data a request needs.
///
/// Call [`Engine::required_ranges`](crate::Engine::required_ranges) before a
/// search or a confirmation when the request can be capped or when calendars come
/// from a provider: it says which records a caller must load so
/// [`Engine::find_availability`](crate::Engine::find_availability) or
/// [`Engine::check_slot`](crate::Engine::check_slot) can answer from complete
/// data. `calendar` is the
/// hull every participant's local dates in the padded search window fall inside,
/// covering blocking events, clearance, the effective travel of the participants
/// the query names and neighbouring `rest` days. `occurrences` is `None` when no
/// cap applies; when a cap is present it is the hull of every bucket the search
/// window touches in the cap's own zone.
///
/// # The normal path: an initial fetch, then event-key closure
///
/// The hulls start a two-phase fetch, not a one-pass query. A cap merges every
/// record sharing an event key across all supplied time ranges, so finishing the
/// fetch means closing over the keys the hulls observe:
///
/// 1. **Initial fetch.** Fetch every availability block whose occupied span — its
/// own span widened by its travel — overlaps `calendar`, and every occurrence
/// and calendar block whose own span overlaps `occurrences`, on every request
/// participant.
/// 2. **Observe keys.** Collect the deduplicated event keys of every record from
/// step 1 whose own span overlaps `occurrences` — every occurrence and every
/// calendar block, on every participant.
/// 3. **Event-key closure.** Fetch every occurrence and every block carrying one
/// of those keys, on every request participant, across the supported time
/// domain. Include all-day and otherwise nonmatching records, and deduplicate
/// by provider record identity rather than by event key. One round settles
/// because every record carries at most one event key.
/// 4. **Evaluate the closed request.** Judge the search or confirmation against
/// that request and a consistent provider snapshot.
///
/// `Request::occurrences` is one request-global list, not a per-participant one:
/// never filter it by the participants an occurrence names. One naming only
/// people outside the request still overrides a same-key block's span and
/// participants, and dropping it charges that block separately. Do not prefilter
/// step 1 by cap tags, status or `all_day`: a cap's `count` matches the union of an
/// occurrence's tags with every same-key block's, so a record another participant
/// holds can supply the counted tag. A one-pass fetch can therefore produce a
/// false offer — an out-of-hull same-key block missing from a minutes cap — or a
/// false exclusion — an out-of-hull same-key block that would have moved a count
/// event to another bucket.
///
/// # Edge case: confirming a start outside the search window
///
/// The hulls are planned from `Goal::search_window` and cover only instants inside
/// it, but `check_slot` ignores that window. A reschedule, drag-and-drop or "book
/// this instead" flow may confirm a start outside it: plan a second `RangesQuery`
/// whose `goal.search_window` contains that start, then repeat the initial fetch
/// and event-key closure above. The search's hulls do not travel with the
/// confirmation; reusing them strips records the window did not touch, so a spent
/// cap or a blocked minute can read as free.
///
/// # Edge case: exact closure is unavailable
///
/// A one-pass fetch is key-complete only under a caller-enforced invariant: every
/// occurrence and block sharing an event key has the identical own span.
/// Per-instance keys alone are not enough when split copies can carry different
/// spans. When closure cannot be completed exactly, fetch all occurrences and
/// blocks across the full supported time domain (`Span::EVERYTHING`,
/// `1975-01-01T00:00Z..2100-01-01T00:00Z`) with [`crate::Limits`] raised to cover
/// it. The published defaults (`max_blocks_per_participant` 10,000 and
/// `max_occurrences` 50,000) refuse that fetch with `blocks_limit` or
/// `occurrences_limit` on a calendar large enough to need it; raise them with
/// [`crate::Limits::with_max_blocks_per_participant`] and
/// [`crate::Limits::with_max_occurrences`]. Or fail closed: the caller's own entry
/// point returns an error rather than evaluating a narrowed request as partial cap
/// data. The engine cannot detect a partial fetch, so no engine call fails closed,
/// and the full domain is a fallback data requirement rather than a replacement for
/// the narrow `occurrences` hull.