From 31f296a55c0530c7b8077c343999cbd207388922 Mon Sep 17 00:00:00 2001 From: Camillo Schenone Date: Wed, 23 Aug 2023 11:18:04 +0200 Subject: [PATCH] Added errors wrappers and version bump Former-commit-id: a9e1c1594f5d199499f7f08d273e2096f71f9bf1 --- Cargo.toml | 4 ++-- src/lib.rs | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 923d3b8..9e8f171 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "CodempClient-Sublime" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -9,7 +9,7 @@ name = "codemp_client" crate-type = ["cdylib"] [dependencies] -codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.3" } +codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.4.4" } pyo3 = { version = "0.19", features = ["extension-module"] } pyo3-asyncio = { version = "0.19", features = ["tokio-runtime"] } tokio = "1.29.1" diff --git a/src/lib.rs b/src/lib.rs index f42586a..e957cd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,39 @@ -use std::{sync::Arc, error::Error}; +use std::{sync::Arc, error::Error, format}; -use codemp::{ - client::CodempClient, - controller::{cursor::{CursorSubscriber, CursorControllerHandle}, - buffer::{OperationControllerHandle, OperationControllerSubscriber}}, - proto::Position, factory::OperationFactory, tokio::sync::Mutex -}; +use codemp::prelude::*; +use codemp::errors::Error as CodempError; + +use tokio::sync::Mutex; use pyo3::{ prelude::*, - exceptions::PyConnectionError, + exceptions::{PyConnectionError, PyRuntimeError}, types::{PyBool, PyString} }; +struct PyCodempError(CodempError); +impl From:: for PyCodempError { + fn from(err: CodempError) -> Self { + PyCodempError(err) + } +} + +impl std::convert::From for PyErr { + fn from(err: PyCodempError) -> PyErr { + match err.0 { + CodempError::Transport { status, message } => { + PyConnectionError::new_err(format!("Transport error: ({}) {}", status, message)) + } + CodempError::Channel { send } => { + PyConnectionError::new_err(format!("Channel error (send:{})", send)) + }, + CodempError::InvalidState { msg } => { + PyRuntimeError::new_err(format!("Invalid state: {}", msg)) + } + } + } +} + #[pyfunction] fn connect<'a>(py: Python<'a>, dest: String) -> PyResult<&'a PyAny> { // construct a python coroutine