qcs_api_client_common/py/
mod.rs

1/// Implements __repr__ on a [`pyo3::pyclass`] by using it's [`std::fmt::Debug`]
2/// implementation.
3///
4/// The pyclass must implement [`std::fmt::Debug`].
5#[macro_export]
6macro_rules! impl_repr {
7    ($name: ident) => {
8        #[pyo3::pymethods]
9        impl $name {
10            #[must_use]
11            fn __repr__(&self) -> String {
12                format!("{:?}", self)
13            }
14        }
15    };
16}
17
18/// Implement `__str__` for wrapper types whose inner type implements [`Display`](std::fmt::Display).
19#[macro_export]
20macro_rules! impl_str {
21    ($name: ident) => {
22        #[$crate::pyo3::pymethods]
23        impl $name {
24            fn __str__(&self) -> String {
25                format!("{}", $crate::PyWrapper::as_inner(self))
26            }
27        }
28    };
29}
30
31/// Provides support for equality checks of a [`pyo3::pyclass`] by implementing `__richcmp__`.
32///
33/// The pyclass must implement [`PartialEq`].
34#[macro_export]
35macro_rules! impl_eq {
36    ($name: ident) => {
37        #[pymethods]
38        impl $name {
39            fn __richcmp__(
40                &self,
41                py: pyo3::Python<'_>,
42                other: &Self,
43                op: pyo3::pyclass::CompareOp,
44            ) -> pyo3::PyObject {
45                use pyo3::IntoPy;
46                match op {
47                    pyo3::pyclass::CompareOp::Eq => (self == other).into_py(py),
48                    pyo3::pyclass::CompareOp::Ne => (self != other).into_py(py),
49                    _ => py.NotImplemented(),
50                }
51            }
52        }
53    };
54}