mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
fix: remove workspace trait, add channel error
This commit is contained in:
parent
37e6268f0c
commit
98cae0969d
2 changed files with 20 additions and 24 deletions
|
@ -1,5 +1,6 @@
|
||||||
use std::{error::Error, fmt::Display};
|
use std::{error::Error, fmt::Display};
|
||||||
|
|
||||||
|
use tokio::sync::mpsc;
|
||||||
use tonic::{Status, Code};
|
use tonic::{Status, Code};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
|
@ -24,7 +25,9 @@ pub enum CodempError {
|
||||||
status: Code,
|
status: Code,
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
Channel { },
|
Channel {
|
||||||
|
send: bool
|
||||||
|
},
|
||||||
|
|
||||||
// TODO filler error, remove later
|
// TODO filler error, remove later
|
||||||
Filler {
|
Filler {
|
||||||
|
@ -38,7 +41,7 @@ impl Display for CodempError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Transport { status, message } => write!(f, "Transport error: ({}) {}", status, message),
|
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"),
|
_ => 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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
31
src/state.rs
31
src/state.rs
|
@ -1,13 +1,14 @@
|
||||||
use std::{collections::BTreeMap, sync::Arc};
|
use std::{collections::BTreeMap, sync::Arc};
|
||||||
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::{RwLock, Mutex};
|
||||||
use tonic::async_trait;
|
use tonic::async_trait;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
buffer::{client::CodempClient, controller::OperationControllerSubscriber},
|
buffer::{controller::BufferController, handle::OperationControllerEditor},
|
||||||
cursor::controller::CursorSubscriber, errors::CodempError,
|
errors::CodempError, Controller, proto::Cursor, cursor::tracker::CursorTracker,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "static")]
|
#[cfg(feature = "static")]
|
||||||
pub mod instance {
|
pub mod instance {
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
|
@ -26,18 +27,7 @@ pub mod instance {
|
||||||
pub struct Workspace {
|
pub struct Workspace {
|
||||||
client: CodempClient,
|
client: CodempClient,
|
||||||
buffers: RwLock<BTreeMap<Box<str>, BufferController>>,
|
buffers: RwLock<BTreeMap<Box<str>, BufferController>>,
|
||||||
cursor: CursorController,
|
cursor: Arc<CursorTracker>,
|
||||||
}
|
|
||||||
|
|
||||||
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>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Workspace {
|
impl Workspace {
|
||||||
|
@ -52,25 +42,22 @@ impl Workspace {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl WorkspaceHandle for Workspace {
|
|
||||||
// Cursor
|
// Cursor
|
||||||
async fn cursor(&self) -> CursorController {
|
pub async fn cursor(&self) -> Arc<CursorTracker> {
|
||||||
self.cursor.clone()
|
self.cursor.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffer
|
// 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()
|
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?)
|
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?;
|
let controller = self.client.clone().attach(path).await?;
|
||||||
self.buffers.write().await.insert(path.into(), Arc::new(controller));
|
self.buffers.write().await.insert(path.into(), Arc::new(controller));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue