Skip to main content

graphrecords_python/graphrecord/
attribute.rs

1use super::{traits::DeepFrom, value::convert_pyobject_to_graphrecordvalue};
2use crate::graphrecord::errors::PyGraphRecordError;
3use graphrecords_core::graphrecord::GraphRecordAttribute;
4use pyo3::{
5    Borrowed, Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python,
6};
7use std::{hash::Hash, ops::Deref};
8
9#[repr(transparent)]
10#[derive(PartialEq, Eq, Hash, Clone, Debug)]
11pub struct PyGraphRecordAttribute(GraphRecordAttribute);
12
13impl From<GraphRecordAttribute> for PyGraphRecordAttribute {
14    fn from(value: GraphRecordAttribute) -> Self {
15        Self(value)
16    }
17}
18
19impl From<PyGraphRecordAttribute> for GraphRecordAttribute {
20    fn from(value: PyGraphRecordAttribute) -> Self {
21        value.0
22    }
23}
24
25impl DeepFrom<PyGraphRecordAttribute> for GraphRecordAttribute {
26    fn deep_from(value: PyGraphRecordAttribute) -> Self {
27        value.into()
28    }
29}
30
31impl DeepFrom<GraphRecordAttribute> for PyGraphRecordAttribute {
32    fn deep_from(value: GraphRecordAttribute) -> Self {
33        value.into()
34    }
35}
36
37impl Deref for PyGraphRecordAttribute {
38    type Target = GraphRecordAttribute;
39
40    fn deref(&self) -> &Self::Target {
41        &self.0
42    }
43}
44
45pub(crate) fn convert_pyobject_to_graphrecordattribute(
46    ob: &Bound<'_, PyAny>,
47) -> PyResult<GraphRecordAttribute> {
48    Ok(convert_pyobject_to_graphrecordvalue(ob)?
49        .try_into()
50        .map_err(PyGraphRecordError::from)?)
51}
52
53impl FromPyObject<'_, '_> for PyGraphRecordAttribute {
54    type Error = PyErr;
55
56    fn extract(ob: Borrowed<'_, '_, PyAny>) -> PyResult<Self> {
57        convert_pyobject_to_graphrecordattribute(&ob).map(Self::from)
58    }
59}
60
61impl<'py> IntoPyObject<'py> for PyGraphRecordAttribute {
62    type Target = PyAny;
63    type Output = Bound<'py, Self::Target>;
64    type Error = PyErr;
65
66    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
67        match self.0 {
68            GraphRecordAttribute::String(value) => value.into_bound_py_any(py),
69            GraphRecordAttribute::Int(value) => value.into_bound_py_any(py),
70        }
71    }
72}