Skip to main content

Crate opportune

Crate opportune 

Source
Expand description

Calendar availability and scheduling calculations across constraints.

A Request goes in — the current instant, what is to be booked, the participants and their calendars, and the caller’s final rule list — and a Response comes out: the times that can be booked, who could take each one, and the travel to reserve. The engine is a pure function of its input: now is an argument, there is no clock and no I/O, and the same request against the same versions gives the same answer everywhere.

Engine holds the time zone database and the request limits. It is built once and reused, and it is the only thing that resolves a request’s zone and calendar names, which is what keeps a request a plain value a caller can build and store without either.

§Quickstart

One person, one 30-minute booking, over a week of weekdays with ten-minute buffers and four hours’ notice:

use opportune::Engine;
use opportune::model::{Goal, Participant, Request, Requirement, Span, Term};
use opportune::presets;
use opportune::time::{
    Date, Duration, Instant, RuleZone, TimeOfDay, Weekday, Weekdays, ZoneName,
};

let zone = ZoneName::new("Europe/London").unwrap();
let now = Instant::from_utc(Date::new(2027, 6, 14).unwrap(), TimeOfDay::new(7, 0).unwrap()).unwrap();
let until = Instant::from_utc(Date::new(2027, 6, 19).unwrap(), TimeOfDay::new(0, 0).unwrap()).unwrap();
let mut request = Request::new(
    now,
    Goal::single(
        zone.clone(),
        Span::new(now, until).unwrap(),
        Duration::from_minutes(30),
        Requirement::new(vec![Term::person("rich")]),
    ),
);
request
    .participants
    .push(Participant::new("rich", zone).with_calendar(Vec::new()));
request.rules.push(presets::business_hours(
    RuleZone::Participant,
    Weekdays::range(Weekday::Monday, Weekday::Friday),
    TimeOfDay::new(9, 0).unwrap(),
    TimeOfDay::new(17, 0).unwrap(),
).unwrap());
request.rules.push(presets::buffers(Duration::from_minutes(10), Duration::from_minutes(10)));
request.rules.push(presets::min_notice(Duration::from_hours_minutes(4, 0)));

let response = Engine::builder().build().unwrap().find_availability(&request).unwrap();
assert!(!response.offers.is_empty());

§Entry points

Engine::find_availability books forwards: every candidate start that the participants’ calendars, rules, caps and rest allow, each with who could take it. Engine::check_slot judges one proposed booking at confirmation time, against the same rules and calendars, reporting every failing reason. Engine::validate reports the request’s structural errors and its warnings before any availability is computed, and Engine::required_ranges says which calendar and occurrence data the caller has to fetch. With the default bundled-tzdb feature the same four are also free functions using a default engine.

§Time zone databases

The database is always an explicit choice, never the host’s: results that depend on the machine are not reproducible. EngineBuilder::tzdb_bundled uses the database compiled into the binary (the default with the bundled-tzdb feature), and EngineBuilder::tzdb_dir reads a directory of TZif files, labelling it from its +VERSION file or from the label the caller passes. Without bundled-tzdb there is no default engine and building one fails with tzdb_not_configured until a directory is given.

§Rule kinds

Rules are typed data with fixed meaning, and the caller supplies the final list: window, blackout, clearance, lead_time, status_policy, rest and cap. presets builds the common ones into Rule values, and the builders in model and time cover the rest.

§Reasons and errors

Every excluded start carries structured reasons when the request asks for an explanation, each with a stable ReasonCode; ReasonCode::ALL is the registry, in the precedence order a participant’s first failing reason follows. Every failure is a typed Error whose code() is stable across releases, and a request that Engine::validate rejects comes back with Diagnostic findings that name the request field at fault.

§Feature flags

bundled-tzdb (default) compiles the time zone database into the crate and provides the free functions. Turning it off leaves the core with a caller-supplied database only, which is what a strict license build or a wasm32 build with its own data uses.

This crate is pure Rust: the Python bindings live in opportune-py, which depends on this crate by path and re-exports VERSION as opportune.__version__.

Re-exports§

pub use error::Diagnostic;
pub use error::Diagnostics;
pub use error::Error;
pub use model::Block;
pub use model::BlockStatus;
pub use model::CalendarSpec;
pub use model::Cap;
pub use model::CapMeasure;
pub use model::CapPer;
pub use model::CapPeriod;
pub use model::Clearance;
pub use model::DEFAULT_LIMIT;
pub use model::DateCount;
pub use model::Explain;
pub use model::Goal;
pub use model::LeadTime;
pub use model::LeadTimeMax;
pub use model::Meta;
pub use model::Occurrence;
pub use model::Offer;
pub use model::OfferEvent;
pub use model::OfferFlag;
pub use model::Part;
pub use model::Participant;
pub use model::ParticipantKind;
pub use model::ParticipantTravel;
pub use model::Proposal;
pub use model::ProposalEvent;
pub use model::RangesQuery;
pub use model::Request;
pub use model::RequiredRanges;
pub use model::Requirement;
pub use model::ReserveKind;
pub use model::ReserveSpan;
pub use model::Response;
pub use model::Rest;
pub use model::Rule;
pub use model::RuleKind;
pub use model::SCHEMA_VERSION;
pub use model::SlotCheck;
pub use model::Span;
pub use model::StatusEffect;
pub use model::StatusPolicy;
pub use model::StatusPolicyEntry;
pub use model::Term;
pub use model::Travel;
pub use model::ZonePeriod;
pub use reasons::Excluded;
pub use reasons::Reason;
pub use reasons::ReasonCode;
pub use reasons::ReasonParams;

Modules§

error
Typed failures and the diagnostics validate reports.
intervals
Sorted, coalesced sets of half-open minute spans.
model
The scheduling domain model: what a request is and what a response is.
presets
Builders and presets over the domain types: sugar that constructs the general shapes and adds no semantics of their own.
reasons
Structured reasons why a candidate start was not offered, and why a proposed booking fails check_slot.
tags
Hierarchical, case-sensitive tags and boolean selectors over them.
time
Civil time: UTC instants, zones, the time zone database, zone periods, candidate grids, working calendars, lead_time horizons and the wall-clock window and blackout specifications.

Structs§

Engine
An engine: the time zone database and the limits every request is judged against.
EngineBuilder
Builds an Engine.
Limits
Configurable caps on request size.

Constants§

VERSION
The crate version, re-exported by the opportune-py bindings as opportune.__version__.

Functions§

check_slot
Checks one proposed booking, using a default engine with the bundled database and default limits.
find_availability
Offers, using a default engine with the bundled database and default limits.
required_ranges
The data a request needs, using a default engine with the bundled database and default limits.
validate
Static checks, using a default engine with the bundled database and default limits.