fix: remove workspace trait, add channel error

This commit is contained in:
əlemi 2023-08-11 15:34:04 +02:00
parent 37e6268f0c
commit 98cae0969d
2 changed files with 20 additions and 24 deletions

View file

@ -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<tonic::transport::Error> for CodempError {
}
}
}
impl<T> From<mpsc::error::SendError<T>> for CodempError {
fn from(_value: mpsc::error::SendError<T>) -> Self {
CodempError::Channel { send: true }
}
}

View file

@ -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<BTreeMap<Box<str>, BufferController>>,
cursor: CursorController,
}
pub type CursorController = Arc<dyn CursorSubscriber + Send + Sync>;
pub type BufferController = Arc<dyn OperationControllerSubscriber + Send + Sync>;
#[async_trait]
pub trait WorkspaceHandle {
async fn cursor(&self) -> CursorController;
async fn buffer(&self, path: &str) -> Option<BufferController>;
async fn attach(&self, path: &str) -> Result<(), CodempError>;
async fn create(&self, path: &str, content: Option<&str>) -> Result<bool, CodempError>;
cursor: Arc<CursorTracker>,
}
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<CursorTracker> {
self.cursor.clone()
}
// Buffer
async fn buffer(&self, path: &str) -> Option<BufferController> {
pub async fn buffer(&self, path: &str) -> Option<BufferController> {
self.buffers.read().await.get(path).cloned()
}
async fn create(&self, path: &str, content: Option<&str>) -> Result<bool, CodempError> {
pub async fn create(&self, path: &str, content: Option<&str>) -> Result<bool, CodempError> {
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(())