Skip to content

Commit 4d0eabd

Browse files
committed
incorporated feedback
1 parent 9b17ba4 commit 4d0eabd

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

python/python/async_tiff/enums.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from enum import IntEnum
22

3+
class Endianness(StrEnum):
4+
"""
5+
endianness of the underlying tiff file
6+
"""
7+
8+
LittleEndian = "LittleEndian"
9+
BigEndian = "BigEndian"
10+
311

412
class CompressionMethod(IntEnum):
513
"""

python/src/enums.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@ use pyo3::prelude::*;
99
use pyo3::types::{PyString, PyTuple};
1010
use pyo3::{intern, IntoPyObjectExt};
1111

12-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13-
#[pyclass(eq, eq_int, name = "Endianness")]
14-
#[repr(u16)]
15-
pub(crate) enum PyEndianness {
16-
LittleEndian = 0x4949, // b"II"
17-
BigEndian = 0x4D4D, // b"MM"
18-
}
12+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13+
pub(crate) struct PyEndianness(Endianness);
1914

2015
impl From<Endianness> for PyEndianness {
2116
fn from(value: Endianness) -> Self {
22-
match value {
23-
Endianness::LittleEndian => Self::LittleEndian,
24-
Endianness::BigEndian => Self::BigEndian,
25-
}
17+
Self(value)
2618
}
2719
}
2820

2921
impl From<PyEndianness> for Endianness {
3022
fn from(value: PyEndianness) -> Self {
31-
match value {
32-
PyEndianness::LittleEndian => Self::LittleEndian,
33-
PyEndianness::BigEndian => Self::BigEndian,
23+
value.0
24+
}
25+
}
26+
27+
impl<'py> IntoPyObject<'py> for PyEndianness {
28+
type Target = PyAny;
29+
type Output = Bound<'py, PyAny>;
30+
type Error = PyErr;
31+
32+
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self:: Error> {
33+
// import the python module
34+
let enums_mod = py.import(intern!(py, "async_tiff.enums"))?;
35+
// get our python enum
36+
let enum_cls = enums_mod.getattr(intern!(py, "Endianness"))?;
37+
match self.0 {
38+
Endianness::LittleEndian => enum_cls.getattr(intern!(py, "LittleEndian")),
39+
Endianness::BigEndian => enum_cls.getattr(intern!(py, "BigEndian")),
3440
}
3541
}
3642
}

python/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod value;
1313
use pyo3::prelude::*;
1414

1515
use crate::decoder::PyDecoderRegistry;
16-
use crate::enums::PyEndianness;
1716
use crate::geo::PyGeoKeyDirectory;
1817
use crate::ifd::PyImageFileDirectory;
1918
use crate::thread_pool::PyThreadPool;
@@ -51,7 +50,6 @@ fn _async_tiff(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
5150
check_debug_build(py)?;
5251

5352
m.add_wrapped(wrap_pyfunction!(___version))?;
54-
m.add_class::<PyEndianness>()?;
5553
m.add_class::<PyDecoderRegistry>()?;
5654
m.add_class::<PyGeoKeyDirectory>()?;
5755
m.add_class::<PyImageFileDirectory>()?;

0 commit comments

Comments
 (0)