|
| 1 | +// Copyright (c) "Neo4j" |
| 2 | +// Neo4j Sweden AB [https://neo4j.com] |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +mod v1; |
| 17 | + |
| 18 | +use pyo3::basic::CompareOp; |
| 19 | +use pyo3::exceptions::PyValueError; |
| 20 | +use pyo3::prelude::*; |
| 21 | +use pyo3::types::{PyBytes, PyTuple}; |
| 22 | +use pyo3::IntoPyObjectExt; |
| 23 | + |
| 24 | +use crate::register_package; |
| 25 | + |
| 26 | +pub(super) fn init_module(m: &Bound<PyModule>, name: &str) -> PyResult<()> { |
| 27 | + let py = m.py(); |
| 28 | + |
| 29 | + m.gil_used(false)?; |
| 30 | + register_package(m, name)?; |
| 31 | + |
| 32 | + let mod_v1 = PyModule::new(py, "v1")?; |
| 33 | + m.add_submodule(&mod_v1)?; |
| 34 | + v1::init_module(&mod_v1, format!("{name}.v1").as_str())?; |
| 35 | + |
| 36 | + m.add_class::<Structure>()?; |
| 37 | + |
| 38 | + Ok(()) |
| 39 | +} |
| 40 | + |
| 41 | +#[pyclass] |
| 42 | +#[derive(Debug)] |
| 43 | +pub struct Structure { |
| 44 | + tag: u8, |
| 45 | + #[pyo3(get)] |
| 46 | + fields: Vec<PyObject>, |
| 47 | +} |
| 48 | + |
| 49 | +#[pymethods] |
| 50 | +impl Structure { |
| 51 | + #[new] |
| 52 | + #[pyo3(signature = (tag, *fields))] |
| 53 | + #[pyo3(text_signature = "(tag, *fields)")] |
| 54 | + fn new(tag: &[u8], fields: Vec<PyObject>) -> PyResult<Self> { |
| 55 | + if tag.len() != 1 { |
| 56 | + return Err(PyErr::new::<PyValueError, _>("tag must be a single byte")); |
| 57 | + } |
| 58 | + let tag = tag[0]; |
| 59 | + Ok(Self { tag, fields }) |
| 60 | + } |
| 61 | + |
| 62 | + #[getter(tag)] |
| 63 | + fn read_tag<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> { |
| 64 | + PyBytes::new(py, &[self.tag]) |
| 65 | + } |
| 66 | + |
| 67 | + #[getter(fields)] |
| 68 | + fn read_fields<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> { |
| 69 | + PyTuple::new(py, &self.fields) |
| 70 | + } |
| 71 | + |
| 72 | + fn eq(&self, other: &Self, py: Python<'_>) -> PyResult<bool> { |
| 73 | + if self.tag != other.tag || self.fields.len() != other.fields.len() { |
| 74 | + return Ok(false); |
| 75 | + } |
| 76 | + for (a, b) in self |
| 77 | + .fields |
| 78 | + .iter() |
| 79 | + .map(|e| e.bind(py)) |
| 80 | + .zip(other.fields.iter().map(|e| e.bind(py))) |
| 81 | + { |
| 82 | + if !a.eq(b)? { |
| 83 | + return Ok(false); |
| 84 | + } |
| 85 | + } |
| 86 | + Ok(true) |
| 87 | + } |
| 88 | + |
| 89 | + fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult<PyObject> { |
| 90 | + Ok(match op { |
| 91 | + CompareOp::Eq => self.eq(other, py)?.into_py_any(py)?, |
| 92 | + CompareOp::Ne => (!self.eq(other, py)?).into_py_any(py)?, |
| 93 | + _ => py.NotImplemented(), |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + fn __hash__(&self, py: Python<'_>) -> PyResult<isize> { |
| 98 | + let mut fields_hash = 0; |
| 99 | + for field in &self.fields { |
| 100 | + fields_hash += field.bind(py).hash()?; |
| 101 | + } |
| 102 | + Ok(fields_hash.wrapping_add(self.tag.into())) |
| 103 | + } |
| 104 | +} |
0 commit comments