Skip to content

Commit

Permalink
fix: updated js and py glues with new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alemidev authored and zaaarf committed Sep 5, 2024
1 parent d25e744 commit 921a8ee
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dist/py/codemp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BufferController:
Handle to the controller for a specific buffer, which manages the back and forth
of operations to and from other peers.
"""
def name(self) -> str: ...
def path(self) -> str: ...
def content(self) -> Promise[str]: ...
def send(self,
start: int,
Expand Down
6 changes: 3 additions & 3 deletions src/ffi/js/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ impl BufferController {
}


#[napi(js_name = "get_name")]
pub fn js_name(&self) -> napi::Result<&str> {
Ok(&self.name())
#[napi(js_name = "get_path")]
pub fn js_path(&self) -> napi::Result<&str> {
Ok(&self.path())
}

#[napi(js_name = "poll")]
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/js/ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use napi_derive::napi;
use crate::hash;
use crate::ext::hash;


#[napi(js_name = "hash")]
Expand Down
22 changes: 15 additions & 7 deletions src/ffi/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ pub mod op_cache;
pub mod ext;


impl From<crate::Error> for napi::Error {
fn from(value: crate::Error) -> Self {
let msg = format!("{value}");
match value {
crate::Error::Deadlocked => napi::Error::new(napi::Status::WouldDeadlock, msg),
_ => napi::Error::new(napi::Status::GenericFailure, msg),
}
impl From<crate::errors::ConnectionError> for napi::Error {
fn from(value: crate::errors::ConnectionError) -> Self {
napi::Error::new(napi::Status::GenericFailure, format!("{value}"))
}
}

impl From<crate::errors::RemoteError> for napi::Error {
fn from(value: crate::errors::RemoteError) -> Self {
napi::Error::new(napi::Status::GenericFailure, format!("{value}"))
}
}

impl From<crate::errors::ControllerError> for napi::Error {
fn from(value: crate::errors::ControllerError) -> Self {
napi::Error::new(napi::Status::GenericFailure, format!("{value}"))
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/ffi/python/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ use pyo3::prelude::*;

#[pymethods]
impl Client {
// #[new]
// fn __new__(host: String, username: String, password: String) -> crate::Result<Self> {
// tokio().block_on(Client::connect(host, username, password))
#[new]
fn __new__(host: String, username: String, password: String) -> crate::errors::ConnectionResult<Self> {
tokio().block_on(Client::connect(host, username, password))
}

// #[pyo3(name = "join_workspace")]
// async fn pyjoin_workspace(&self, workspace: String) -> JoinHandle<crate::Result<Workspace>> {
// tracing::info!("attempting to join the workspace {}", workspace);

// let this = self.clone();
// async {
// tokio()
// .spawn(async move { this.join_workspace(workspace).await })
// .await
// }
// }

#[pyo3(name = "join_workspace")]
Expand Down
6 changes: 3 additions & 3 deletions src/ffi/python/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl CursorController {
// need to do manually since Controller is a trait implementation
#[pymethods]
impl BufferController {
#[pyo3(name = "name")]
fn pyname(&self) -> String {
self.name().to_string()
#[pyo3(name = "path")]
fn pypath(&self) -> String {
self.path().to_string()
}

#[pyo3(name = "content")]
Expand Down
29 changes: 15 additions & 14 deletions src/ffi/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,21 @@ fn set_logger(logging_cb: Py<PyFunction>, debug: bool) -> bool {
log_subscribed
}

impl From<crate::Error> for PyErr {
fn from(value: crate::Error) -> Self {
match value {
crate::Error::Transport { status, message } => {
PyConnectionError::new_err(format!("Transport error: ({}) {}", status, message))
}
crate::Error::Channel { send } => {
PyConnectionError::new_err(format!("Channel error (send:{})", send))
}
crate::Error::InvalidState { msg } => {
PyRuntimeError::new_err(format!("Invalid state: {}", msg))
}
crate::Error::Deadlocked => PyRuntimeError::new_err("Deadlock, retry."),
}
impl From<crate::errors::ConnectionError> for PyErr {
fn from(value: crate::errors::ConnectionError) -> Self {
PyConnectionError::new_err(format!("Connection error: {value}"))
}
}

impl From<crate::errors::RemoteError> for PyErr {
fn from(value: crate::errors::RemoteError) -> Self {
PyRuntimeError::new_err(format!("Remote error: {value}"))
}
}

impl From<crate::errors::ControllerError> for PyErr {
fn from(value: crate::errors::ControllerError) -> Self {
PyRuntimeError::new_err(format!("Controller error: {value}"))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ffi/python/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ impl Workspace {
#[pyo3(name = "detach")]
fn pydetach(&self, path: String) -> bool {
match self.detach(path.as_str()) {
crate::workspace::worker::DetachResult::NotAttached => false,
crate::workspace::worker::DetachResult::Detaching => true,
crate::workspace::worker::DetachResult::AlreadyDetached => true,
crate::workspace::DetachResult::NotAttached => false,
crate::workspace::DetachResult::Detaching => true,
crate::workspace::DetachResult::AlreadyDetached => true,
}
}

Expand Down

0 comments on commit 921a8ee

Please sign in to comment.