Skip to content

Commit

Permalink
add log feature
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-dupre committed Dec 29, 2024
1 parent c1ed040 commit 8c16db0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 54 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ jobs:
matrix:
crate:
[".", "compact-calendar", "opening-hours-py", "opening-hours-syntax"]
toolchain: ["stable", "beta", "nightly"]
features: [""]
include:
- crate: "."
features: "log"
- crate: "opening-hours-syntax"
features: "log"
defaults:
run:
working-directory: ${{ matrix.crate }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.toolchain }}
toolchain: stable
components: rustfmt, clippy
- name: Display rust version
run: |
rustc --version
cargo clippy -- --version
cargo fmt -- --version
- name: Lint
run: cargo clippy -- -D warnings
if: matrix.toolchain != 'nightly'
run: cargo clippy --no-default-features --features "${{ matrix.features }}" -- -D warnings
- name: Format
run: cargo fmt -- --check
if: matrix.toolchain != 'nightly'
- name: Tests
run: cargo test
run: cargo test --no-default-features --features "${{ matrix.features }}"

# ---
# --- Check that all versions are consistency accross packages
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ path = "opening-hours/src/lib.rs"
# measuring coverage.
disable-test-timeouts = []

log = ["opening-hours-py/log", "log"]

[dependencies]
compact-calendar = { path = "compact-calendar", version = "0.10.0" }
opening-hours-syntax = { path = "opening-hours-syntax", version = "0.10.0" }
chrono = "0.4"
chrono-tz = "0.10" # TODO: feature flag
country-boundaries = "1.2" # TODO: feature flag
flate2 = "1.0" # TODO: feature flag
log = { version = "0.4", features = [ "kv" ] }
log = { version = "0.4", features = [ "kv" ], optional = true }
sunrise-next = "1.2" # TODO: feature flag
tzf-rs = { version = "0.4", default-features = false } # TODO: feature flag

Expand Down
43 changes: 13 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
🦀 Rust implementation for OSM Opening Hours
============================================
# 🦀 Rust implementation for OSM Opening Hours

[![](https://img.shields.io/crates/v/opening-hours)][opening-hours]
[![](https://img.shields.io/pypi/v/opening-hours-py)][pypy]
Expand All @@ -8,7 +7,6 @@
[![](https://img.shields.io/codecov/c/github/remi-dupre/opening-hours-rs)][codecov]
[![](https://img.shields.io/crates/d/opening-hours)][opening-hours]


**🐍 Python bindings can be found [here](https://github.com/remi-dupre/opening-hours-rs/tree/master/python)**

A Rust library for parsing and working with OSM's opening hours field. You can
Expand All @@ -21,9 +19,7 @@ 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
-----
## Usage

Add this to your `Cargo.toml`:

Expand All @@ -50,41 +46,28 @@ fn main() {
}
```


Supported features
------------------
## 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.


### 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.

### Logging

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

[opening-hours]: https://crates.io/crates/opening-hours
"Package"

[opening-hours-syntax]: https://crates.io/crates/opening-hours-syntax
"Syntax Package"

[docs]: https://docs.rs/opening-hours
"Documentation"

[pypy]: https://pypi.org/project/opening-hours-py
"Python package"

[codecov]: https://app.codecov.io/gh/remi-dupre/opening-hours-rs
"Code coverage"

[workalendar]: https://pypi.org/project/workalendar/
"Worldwide holidays and working days helper and toolkit."

[nager]: https://date.nager.at/api/v3
"Worldwide holidays (REST API)"
[codecov]: https://app.codecov.io/gh/remi-dupre/opening-hours-rs "Code coverage"
[crate-log]: https://crates.io/crates/log
[docs]: https://docs.rs/opening-hours "Documentation"
[nager]: https://date.nager.at/api/v3 "Worldwide holidays (REST API)"
[opening-hours]: https://crates.io/crates/opening-hours "Package"
[opening-hours-syntax]: https://crates.io/crates/opening-hours-syntax "Syntax Package"
[pypy]: https://pypi.org/project/opening-hours-py "Python package"
[workalendar]: https://pypi.org/project/workalendar/ "Worldwide holidays and working days helper and toolkit."
5 changes: 4 additions & 1 deletion opening-hours-syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ edition = "2021"

[dependencies]
chrono = { version = "0.4", default-features = false }
log = { version = "0.4", features = [ "kv" ] }
log = { version = "0.4", features = [ "kv" ], optional = true }
pest = "2.0"
pest_derive = "2.0"

[features]
log = ["log"]
20 changes: 8 additions & 12 deletions opening-hours-syntax/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
Rust parser for OSM Opening Hours
=================================
# Rust parser for OSM Opening Hours

[![](https://img.shields.io/crates/v/opening-hours-syntax)][opening-hours-syntax]
[![](https://img.shields.io/docsrs/opening-hours-syntax)][docs]

Parsing component of [opening-hours] crate.


Usage
-----
## Usage

Add this to your `Cargo.toml`:

Expand All @@ -31,13 +28,12 @@ fn main() {
}
```

## Supported features

[opening-hours]: https://crates.io/crates/opening-hours
"Root Package"

[opening-hours-syntax]: https://crates.io/crates/opening-hours
"Package"
### Logging

[docs]: https://docs.rs/opening-hours-syntax
"Documentation"
The **log** feature can be enabled to emit warnings the [crate-log] crate.

[docs]: https://docs.rs/opening-hours-syntax "Documentation"
[opening-hours]: https://crates.io/crates/opening-hours "Root Package"
[opening-hours-syntax]: https://crates.io/crates/opening-hours "Package"
8 changes: 6 additions & 2 deletions opening-hours-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::convert::TryInto;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::RangeInclusive;
use std::sync::{Arc, Once};
use std::sync::Arc;

use chrono::Duration;

Expand All @@ -16,7 +16,8 @@ use crate::rules as rl;
use crate::rules::day as ds;
use crate::rules::time as ts;

static WARN_EASTER: Once = Once::new();
#[cfg(feature = "log")]
static WARN_EASTER: std::sync::Once = std::sync::Once::new();

#[derive(Parser)]
#[grammar = "grammar.pest"]
Expand Down Expand Up @@ -584,6 +585,7 @@ fn build_date_from(pair: Pair<Rule>) -> ds::Date {

match pairs.peek().expect("empty date (from)").as_rule() {
Rule::variable_date => {
#[cfg(feature = "log")]
WARN_EASTER.call_once(|| log::warn!("Easter is not supported yet"));
ds::Date::Easter { year }
}
Expand Down Expand Up @@ -764,11 +766,13 @@ fn build_daynum(pair: Pair<Rule>) -> u8 {
let daynum = pair.as_str().parse().expect("invalid month format");

if daynum == 0 {
#[cfg(feature = "log")]
log::warn!("Found day number 0 in opening hours: specify the 1st or 31st instead.");
return 1;
}

if daynum > 31 {
#[cfg(feature = "log")]
log::warn!("Found day number {daynum} in opening hours");
return 31;
}
Expand Down
3 changes: 1 addition & 2 deletions opening-hours/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl ContextHolidays {
/// Specifies how dates should be localized while evaluating opening hours. No
/// localisation is available by default but this can be used to specify a
/// timezone and coordinates (which affect sun events).
///
/// TODO: Only export in a location module?
pub trait Localize: Clone + Send + Sync {
/// The type for localized date & time.
type DateTime: Clone + Add<Duration, Output = Self::DateTime>;
Expand Down Expand Up @@ -141,6 +139,7 @@ impl TzLocation<chrono_tz::Tz> {
let tz_name = TZ_NAME_FINDER.get_tz_name(lon, lat);

let tz = TZ_BY_NAME.get(tz_name).copied().unwrap_or_else(|| {
#[cfg(feature = "log")]
log::warn!("Could not find time zone `{tz_name}` at {lat},{lon}");
chrono_tz::UTC
});
Expand Down
1 change: 1 addition & 0 deletions opening-hours/src/country/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Country {
.expect("unable to parse holiday data");

let Ok(country) = region.parse() else {
#[cfg(feature = "log")]
log::warn!("Unknown initialized country code {region}");
return None;
};
Expand Down

0 comments on commit 8c16db0

Please sign in to comment.