From 98cae0969d0287fbfc70d1a0c65121d099bc3ab9 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 11 Aug 2023 15:34:04 +0200 Subject: [PATCH] fix: remove workspace trait, add channel error --- src/errors.rs | 13 +++++++++++-- src/state.rs | 31 +++++++++---------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index b3f53cc..54caa6b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,6 @@ use std::{error::Error, fmt::Display}; +use tokio::sync::mpsc; use tonic::{Status, Code}; use tracing::warn; @@ -24,7 +25,9 @@ pub enum CodempError { status: Code, message: String, }, - Channel { }, + Channel { + send: bool + }, // TODO filler error, remove later Filler { @@ -38,7 +41,7 @@ impl Display for CodempError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Transport { status, message } => write!(f, "Transport error: ({}) {}", status, message), - Self::Channel { } => write!(f, "Channel error"), + Self::Channel { send } => write!(f, "Channel error (send:{})", send), _ => write!(f, "Unknown error"), } } @@ -57,3 +60,9 @@ impl From for CodempError { } } } + +impl From> for CodempError { + fn from(_value: mpsc::error::SendError) -> Self { + CodempError::Channel { send: true } + } +} diff --git a/src/state.rs b/src/state.rs index 2431ddb..39d1f54 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,13 +1,14 @@ use std::{collections::BTreeMap, sync::Arc}; -use tokio::sync::RwLock; +use tokio::sync::{RwLock, Mutex}; use tonic::async_trait; use crate::{ - buffer::{client::CodempClient, controller::OperationControllerSubscriber}, - cursor::controller::CursorSubscriber, errors::CodempError, + buffer::{controller::BufferController, handle::OperationControllerEditor}, + errors::CodempError, Controller, proto::Cursor, cursor::tracker::CursorTracker, }; + #[cfg(feature = "static")] pub mod instance { use tokio::runtime::Runtime; @@ -26,18 +27,7 @@ pub mod instance { pub struct Workspace { client: CodempClient, buffers: RwLock, BufferController>>, - cursor: CursorController, -} - -pub type CursorController = Arc; -pub type BufferController = Arc; - -#[async_trait] -pub trait WorkspaceHandle { - async fn cursor(&self) -> CursorController; - async fn buffer(&self, path: &str) -> Option; - async fn attach(&self, path: &str) -> Result<(), CodempError>; - async fn create(&self, path: &str, content: Option<&str>) -> Result; + cursor: Arc, } impl Workspace { @@ -52,25 +42,22 @@ impl Workspace { } ) } -} -#[async_trait] -impl WorkspaceHandle for Workspace { // Cursor - async fn cursor(&self) -> CursorController { + pub async fn cursor(&self) -> Arc { self.cursor.clone() } // Buffer - async fn buffer(&self, path: &str) -> Option { + pub async fn buffer(&self, path: &str) -> Option { self.buffers.read().await.get(path).cloned() } - async fn create(&self, path: &str, content: Option<&str>) -> Result { + pub async fn create(&self, path: &str, content: Option<&str>) -> Result { Ok(self.client.clone().create(path, content).await?) } - async fn attach(&self, path: &str) -> Result<(), CodempError> { + pub async fn attach(&self, path: &str) -> Result<(), CodempError> { let controller = self.client.clone().attach(path).await?; self.buffers.write().await.insert(path.into(), Arc::new(controller)); Ok(())