use crate::buffer::Controller as CodempBufferController; use crate::cursor::Controller as CodempCursorController; use crate::workspace::Workspace as CodempWorkspace; use pyo3::prelude::*; use pyo3::types::PyString; #[pymethods] impl CodempWorkspace { // join a workspace #[pyo3(name = "create")] fn pycreate<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&'p PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { ws.create(path.as_str()).await?; Ok(()) }) } #[pyo3(name = "attach")] fn pyattach<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { let buffctl: CodempBufferController = ws.attach(path.as_str()).await?; Python::with_gil(|py| Py::new(py, buffctl)) }) } #[pyo3(name = "detach")] fn pydetach(&self, path: String) -> bool { match self.detach(path.as_str()) { crate::workspace::DetachResult::NotAttached => false, crate::workspace::DetachResult::Detaching => true, crate::workspace::DetachResult::AlreadyDetached => true, } } #[pyo3(name = "fetch_buffers")] fn pyfetch_buffers<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { ws.fetch_buffers().await?; Ok(()) }) } #[pyo3(name = "fetch_users")] fn pyfetch_users<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { ws.fetch_users().await?; Ok(()) }) } #[pyo3(name = "list_buffer_users")] fn pylist_buffer_users<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { let usrlist: Vec = ws .list_buffer_users(path.as_str()) .await? .into_iter() .map(|e| e.id) .collect(); Ok(usrlist) }) } #[pyo3(name = "delete")] fn pydelete<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> { let ws = self.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { ws.delete(path.as_str()).await?; Ok(()) }) } #[pyo3(name = "id")] fn pyid(&self, py: Python<'_>) -> Py { PyString::new(py, self.id().as_str()).into() } #[pyo3(name = "cursor")] fn pycursor(&self, py: Python<'_>) -> PyResult> { Py::new(py, self.cursor()) } #[pyo3(name = "buffer_by_name")] fn pybuffer_by_name( &self, py: Python<'_>, path: String, ) -> PyResult>> { let Some(bufctl) = self.buffer_by_name(path.as_str()) else { return Ok(None); }; Ok(Some(Py::new(py, bufctl)?)) } #[pyo3(name = "buffer_list")] fn pybuffer_list(&self) -> Vec { self.buffer_list() } #[pyo3(name = "filetree")] fn pyfiletree(&self) -> Vec { self.filetree() } }