|
| 1 | +use async_tiff::tiff::Value; |
| 2 | +use pyo3::exceptions::PyRuntimeError; |
| 3 | +use pyo3::prelude::*; |
| 4 | +use pyo3::IntoPyObjectExt; |
| 5 | + |
| 6 | +pub struct PyValue(Value); |
| 7 | + |
| 8 | +impl<'py> IntoPyObject<'py> for PyValue { |
| 9 | + type Target = PyAny; |
| 10 | + type Output = Bound<'py, Self::Target>; |
| 11 | + type Error = PyErr; |
| 12 | + |
| 13 | + fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> { |
| 14 | + match self.0 { |
| 15 | + Value::Byte(val) => val.into_bound_py_any(py), |
| 16 | + Value::Short(val) => val.into_bound_py_any(py), |
| 17 | + Value::SignedByte(val) => val.into_bound_py_any(py), |
| 18 | + Value::SignedShort(val) => val.into_bound_py_any(py), |
| 19 | + Value::Signed(val) => val.into_bound_py_any(py), |
| 20 | + Value::SignedBig(val) => val.into_bound_py_any(py), |
| 21 | + Value::Unsigned(val) => val.into_bound_py_any(py), |
| 22 | + Value::UnsignedBig(val) => val.into_bound_py_any(py), |
| 23 | + Value::Float(val) => val.into_bound_py_any(py), |
| 24 | + Value::Double(val) => val.into_bound_py_any(py), |
| 25 | + Value::List(val) => val |
| 26 | + .into_iter() |
| 27 | + .map(|v| PyValue(v).into_bound_py_any(py)) |
| 28 | + .collect::<PyResult<Vec<_>>>()? |
| 29 | + .into_bound_py_any(py), |
| 30 | + Value::Rational(num, denom) => (num, denom).into_bound_py_any(py), |
| 31 | + Value::RationalBig(num, denom) => (num, denom).into_bound_py_any(py), |
| 32 | + Value::SRational(num, denom) => (num, denom).into_bound_py_any(py), |
| 33 | + Value::SRationalBig(num, denom) => (num, denom).into_bound_py_any(py), |
| 34 | + Value::Ascii(val) => val.into_bound_py_any(py), |
| 35 | + Value::Ifd(_val) => Err(PyRuntimeError::new_err("Unsupported value type 'Ifd'")), |
| 36 | + Value::IfdBig(_val) => Err(PyRuntimeError::new_err("Unsupported value type 'IfdBig'")), |
| 37 | + v => Err(PyRuntimeError::new_err(format!( |
| 38 | + "Unknown value type: {:?}", |
| 39 | + v |
| 40 | + ))), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl From<Value> for PyValue { |
| 46 | + fn from(value: Value) -> Self { |
| 47 | + Self(value) |
| 48 | + } |
| 49 | +} |
0 commit comments