Crate opening_hours

Source
Expand description

ยง๐Ÿฆ€ Rust implementation for OSM Opening Hours

๐Ÿ Python bindings can be found here

A Rust library for parsing and working with OSMโ€™s opening hours field. You can find its specification here and the reference JS library here.

Note that the specification is quite messy and that the JS library takes liberty to extend it quite a lot. This means that most of the real world data donโ€™t actually comply to the very restrictive grammar detailed in the official specification. This library tries to fit with the real world data while remaining as close as possible to the core specification.

ยงUsage

Add this to your Cargo.toml:

[dependencies]
opening-hours = "0"

Hereโ€™s a simple example that parse an opening hours description and displays its current status and date for next change:

use chrono::Local;
use opening_hours::OpeningHours;

// Opens until 18pm during the week and until 12am the week-end.
const OH: &str = "Mo-Fr 10:00-18:00; Sa-Su 10:00-12:00";

fn main() {
    let oh: OpeningHours = OH.parse().unwrap();
    let date = Local::now().naive_local();
    println!("Current status is {:?}", oh.state(date));
    println!("This will change at {:?}", oh.next_change(date).unwrap());
}

ยงSupported features

ยงHolidays

A public holiday database is loaded using nager. You can refer to their website for more detail on supported country or if you want to contribute.

If you enable the auto-country feature, you can automatically detect the country of a point of interest from its coordinate.

ยงSyntax

If you are only interested in parsing expressions but not on the evaluation or if you want to build your own evaluation engine, you should probably rely on the opening-hours-syntax crate.

ยงTimezone

You can attach the timezone of the POI corresponding to your opening hours in the evaluation context. If you enable the auto-timezone feature, you can also automatically infer the timezone from coordinates.

ยงLogging

The log feature can be enabled to emit warnings the crate-log crate.

ยงContributing

ยงRunning tests

Tests can be run by running cargo test.

A fuzzing can be run using cargo-fuzz by running cargo +nightly fuzz run -j 4 parse_oh.

ยงRe-generating Python stub file

opening_hours.pyi should not be edited manually, if you make changes to Python bindings, you need to update it automatically:

# Install required dev dependencies
poetry install --with dev

# Generate stub file
cd opening-hours-py
cargo run --bin stub_gen

Modulesยง

localization
schedule

Macrosยง

schedule
Macro that allows to quickly create a complex schedule.

Structsยง

Context
All the context attached to a parsed OpeningHours expression and that can alter its evaluation semantics.
ContextHolidays
Pairs a set of public holidays with a set of school holidays.
DateTimeRange
OpeningHours
A parsed opening hours expression and its evaluation context.

Enumsยง

RuleKind

Constantsยง

DATE_LIMIT
The upper bound of dates handled by specification