use crate::atoms::NodeId;
use crate::coords::Coords;
use crate::handle::Handle;
use crate::py::node::PyNode;
use pyo3::prelude::*;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "Coords")]
pub struct PyCoords {
pub(crate) handle: Handle<Coords>,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyCoords {
#[new]
fn py_new(dim: u8) -> PyResult<Self> {
let coords = Coords::new(dim)?;
Ok(Self {
handle: Handle::new(coords),
})
}
#[classmethod]
fn axisymmetric(_cls: &pyo3::Bound<'_, pyo3::types::PyType>) -> PyResult<Self> {
Ok(Self {
handle: Handle::new(Coords::axisymmetric()?),
})
}
#[getter]
fn dim(&self) -> PyResult<u8> {
Ok(self.handle.read().dim())
}
#[getter]
fn is_axisymmetric(&self) -> PyResult<bool> {
Ok(self.handle.read().is_axisymmetric())
}
fn node_count(&self) -> PyResult<usize> {
Ok(self.handle.read().node_count())
}
fn capacity(&self) -> PyResult<usize> {
Ok(self.handle.read().capacity())
}
fn is_alive(&self, id: u32) -> PyResult<bool> {
Ok(self.handle.read().is_alive(NodeId(id)))
}
fn add_node(&self, coords: Vec<f64>) -> PyResult<PyNode> {
let id = self.handle.write().add_node(&coords)?;
Ok(PyNode::from_raw(self.handle.clone(), id))
}
fn acquire(&self, id: u32) -> PyResult<PyNode> {
self.handle.write().incref(NodeId(id))?;
Ok(PyNode::from_raw(self.handle.clone(), NodeId(id)))
}
fn refcount(&self, id: u32) -> PyResult<u32> {
Ok(self.handle.read().refcount(NodeId(id)))
}
fn gc(&self) -> PyResult<usize> {
Ok(self.handle.write().gc())
}
fn add_config(&self, name: String) -> PyResult<usize> {
Ok(self.handle.write().add_config(name))
}
fn select(&self, config: usize) -> PyResult<()> {
self.handle.write().select(config)?;
Ok(())
}
#[getter]
fn active(&self) -> PyResult<usize> {
Ok(self.handle.read().active())
}
fn names(&self) -> PyResult<Vec<String>> {
Ok(self.handle.read().names().to_vec())
}
fn permutation(&self) -> PyResult<Option<Vec<u32>>> {
Ok(self.handle.read().permutation().map(|s| s.to_vec()))
}
fn set_permutation(&self, perm: Vec<u32>) -> PyResult<()> {
self.handle.write().set_permutation(perm)?;
Ok(())
}
fn clear_permutation(&self) -> PyResult<()> {
self.handle.write().clear_permutation();
Ok(())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", &*self.handle.read()))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", &*self.handle.read()))
}
}
crate::impl_dump_pymethod!(handle PyCoords, handle);