feat(python): fixed macro and expanded to workplace methods

This commit is contained in:
cschen 2024-08-20 22:18:29 +02:00
parent feff54bcdf
commit 4a575c587f
3 changed files with 42 additions and 52 deletions

View file

@ -13,16 +13,17 @@ impl Client {
#[pyo3(name = "join_workspace")] #[pyo3(name = "join_workspace")]
fn pyjoin_workspace(&self, workspace: String) -> PyResult<super::RustPromise> { fn pyjoin_workspace(&self, workspace: String) -> PyResult<super::RustPromise> {
tracing::info!("attempting to join the workspace {workspace}"); tracing::info!("attempting to join the workspace {}", workspace);
// crate::a_sync! { self => self.join_workspace(workspace).await } let this = self.clone();
let rc = self.clone(); crate::a_sync!(this.join_workspace(workspace).await)
Ok(super::RustPromise(Some(tokio().spawn(async move { // let rc = self.clone();
Ok(rc // Ok(super::RustPromise(Some(tokio().spawn(async move {
.join_workspace(workspace) // Ok(rc
.await // .join_workspace(workspace)
.map(|f| Python::with_gil(|py| f.into_py(py)))?) // .await
})))) // .map(|f| Python::with_gil(|py| f.into_py(py)))?)
// }))))
} }
#[pyo3(name = "leave_workspace")] #[pyo3(name = "leave_workspace")]

View file

@ -61,23 +61,14 @@ pub fn tokio() -> &'static tokio::runtime::Runtime {
#[macro_export] #[macro_export]
macro_rules! a_sync { macro_rules! a_sync {
($($clone:ident)* => $x:expr) => { ($x:expr) => {{
{ Ok($crate::ffi::python::RustPromise(Some(
$(let $clone = $clone.clone();)* $crate::ffi::python::tokio()
Ok($crate::ffi::python::RustPromise(Some($crate::ffi::python::tokio().spawn(async move { .spawn(async move { Ok($x.map(|f| Python::with_gil(|py| f.into_py(py)))?) }),
Ok($x.map(|f| Python::with_gil(|py| f.into_py(py)))?) )))
})))) }};
}
};
} }
// #[macro_export]
// macro_rules! spawn_future {
// ($fut:expr) => {
// $crate::ffi::python::tokio().spawn(async move { $fut.await })
// };
// }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct LoggerProducer(mpsc::UnboundedSender<String>); struct LoggerProducer(mpsc::UnboundedSender<String>);

View file

@ -3,23 +3,23 @@ use crate::cursor::Controller as CursorController;
use crate::workspace::Workspace; use crate::workspace::Workspace;
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::spawn_future; use super::RustPromise;
use crate::a_sync;
// use super::Promise; // use super::Promise;
#[pymethods] #[pymethods]
impl Workspace { impl Workspace {
// join a workspace // join a workspace
#[pyo3(name = "create")] #[pyo3(name = "create")]
async fn pycreate(&self, path: String) -> crate::Result<()> { fn pycreate(&self, path: String) -> PyResult<RustPromise> {
let rc = self.clone(); let this = self.clone();
spawn_future!(rc.create(path.as_str())).await.unwrap() a_sync!(this.create(path.as_str()).await)
} }
#[pyo3(name = "attach")] #[pyo3(name = "attach")]
async fn pyattach(&self, path: String) -> crate::Result<BufferController> { fn pyattach(&self, path: String) -> PyResult<RustPromise> {
let rc = self.clone(); let this = self.clone();
spawn_future!(rc.attach(path.as_str())).await.unwrap() a_sync!(this.attach(path.as_str()).await)
} }
#[pyo3(name = "detach")] #[pyo3(name = "detach")]
@ -31,37 +31,35 @@ impl Workspace {
} }
} }
// #[pyo3(name = "event")] #[pyo3(name = "event")]
// fn pyevent(&self) -> Promise { fn pyevent(&self) -> PyResult<RustPromise> {
// crate::a_sync! { self => let this = self.clone();
// self.event().await a_sync!(this.event().await)
// } }
// }
#[pyo3(name = "fetch_buffers")] #[pyo3(name = "fetch_buffers")]
async fn pyfetch_buffers(&self) -> crate::Result<()> { fn pyfetch_buffers(&self) -> PyResult<RustPromise> {
let rc = self.clone(); let this = self.clone();
spawn_future!(rc.fetch_buffers()).await.unwrap() a_sync!(this.fetch_buffers().await)
} }
#[pyo3(name = "fetch_users")] #[pyo3(name = "fetch_users")]
async fn pyfetch_users(&self) -> crate::Result<()> { fn pyfetch_users(&self) -> PyResult<RustPromise> {
let rc = self.clone(); let this = self.clone();
spawn_future!(rc.fetch_users()).await.unwrap() a_sync!(this.fetch_users().await)
} }
#[pyo3(name = "list_buffer_users")] #[pyo3(name = "list_buffer_users")]
async fn pylist_buffer_users(&self, path: String) -> crate::Result<Vec<crate::api::User>> { fn pylist_buffer_users(&self, path: String) -> PyResult<RustPromise> {
let rc = self.clone(); // crate::Result<Vec<crate::api::User>> {
spawn_future!(rc.list_buffer_users(path.as_str())) let this = self.clone();
.await a_sync!(this.list_buffer_users(path.as_str()).await)
.unwrap()
} }
#[pyo3(name = "delete")] #[pyo3(name = "delete")]
async fn pydelete(&self, path: String) -> crate::Result<()> { fn pydelete(&self, path: String) -> PyResult<RustPromise> {
let rc = self.clone(); let this = self.clone();
spawn_future!(rc.delete(path.as_str())).await.unwrap() a_sync!(this.delete(path.as_str()).await)
} }
#[pyo3(name = "id")] #[pyo3(name = "id")]