Skip to content

Commit

Permalink
use a Python enum for state
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-dupre committed Nov 29, 2024
1 parent 53a0571 commit ef4f340
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.10.0

### Python

- Add the `opening_hours.State` type.

## 0.9.0

### General
Expand All @@ -22,29 +28,25 @@
- The iterator returned by `OpeningHours.intervals` can be moved between
threads.


## 0.8.3

### Fixes

- Fix [#52](https://github.com/remi-dupre/opening-hours-rs/pull/52): intervals
were stopping at midnight before the last day.


## 0.8.2

### Fixes

- Python's Linux binary build were not uploading


## 0.8.1

### Fixes

- Rust crate couldn't publish


## 0.8.0

### General
Expand Down
1 change: 1 addition & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl PyOpeningHours {
fn opening_hours(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
pyo3_log::init();
m.add_function(wrap_pyfunction!(validate, m)?)?;
m.add_class::<State>()?;
m.add_class::<PyOpeningHours>()?;
Ok(())
}
55 changes: 34 additions & 21 deletions python/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::convert::Infallible;
use std::sync::OnceLock;

use chrono::{Local, NaiveDateTime};
use opening_hours::opening_hours::DATE_LIMIT;
use opening_hours::DateTimeRange;
use opening_hours_syntax::rules::RuleKind;
use pyo3::prelude::*;
use pyo3::types::PyString;

pub(crate) fn get_time(datetime: Option<NaiveDateTime>) -> NaiveDateTime {
datetime.unwrap_or_else(|| Local::now().naive_local())
Expand All @@ -24,9 +20,15 @@ pub(crate) fn res_time(datetime: NaiveDateTime) -> Option<NaiveDateTime> {
// --- State
// ---

/// Specify the state of an opening hours interval.
#[pyclass(ord, eq, frozen, hash, str, rename_all = "UPPERCASE")]
#[derive(Hash, PartialOrd, Ord, PartialEq, Eq)]
pub enum State {
/// Currently open
Open,
/// Currently closed
Closed,
/// May be open depending on context
Unknown,
}

Expand All @@ -40,26 +42,36 @@ impl From<RuleKind> for State {
}
}

impl<'py> IntoPyObject<'py> for State {
type Target = PyString;
type Output = Borrowed<'static, 'py, Self::Target>;
type Error = Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
static PY_CACHE_OPEN: OnceLock<Py<PyString>> = OnceLock::new();
static PY_CACHE_CLOSED: OnceLock<Py<PyString>> = OnceLock::new();
static PY_CACHE_UNKNOWN: OnceLock<Py<PyString>> = OnceLock::new();

let res = match self {
Self::Open => PY_CACHE_OPEN.get_or_init(|| PyString::new(py, "open").unbind()),
Self::Closed => PY_CACHE_CLOSED.get_or_init(|| PyString::new(py, "closed").unbind()),
Self::Unknown => PY_CACHE_UNKNOWN.get_or_init(|| PyString::new(py, "unknown").unbind()),
};

Ok(res.bind_borrowed(py))
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::Open => write!(f, "open"),
State::Closed => write!(f, "closed"),
State::Unknown => write!(f, "unknown"),
}
}
}

// impl<'py> IntoPyObject<'py> for State {
// type Target = PyString;
// type Output = Borrowed<'static, 'py, Self::Target>;
// type Error = Infallible;
//
// fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
// static PY_CACHE_OPEN: OnceLock<Py<PyString>> = OnceLock::new();
// static PY_CACHE_CLOSED: OnceLock<Py<PyString>> = OnceLock::new();
// static PY_CACHE_UNKNOWN: OnceLock<Py<PyString>> = OnceLock::new();
//
// let res = match self {
// Self::Open => PY_CACHE_OPEN.get_or_init(|| PyString::new(py, "open").unbind()),
// Self::Closed => PY_CACHE_CLOSED.get_or_init(|| PyString::new(py, "closed").unbind()),
// Self::Unknown => PY_CACHE_UNKNOWN.get_or_init(|| PyString::new(py, "unknown").unbind()),
// };
//
// Ok(res.bind_borrowed(py))
// }
// }

// ---
// --- RangeIterator
// ---
Expand Down Expand Up @@ -89,6 +101,7 @@ impl RangeIterator {
}
}

/// Iterator over a range of opening hours.
#[pymethods]
impl RangeIterator {
fn __iter__(slf: PyRef<Self>) -> Py<Self> {
Expand Down

0 comments on commit ef4f340

Please sign in to comment.