From 041acdbb9475dda10f5ad232fb4928f04842eed3 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 13 Jul 2023 01:35:18 +0200 Subject: [PATCH] feat: initial barebones structure for workspace --- src/lib.rs | 1 + src/workspace.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/workspace.rs diff --git a/src/lib.rs b/src/lib.rs index 3931ea4..2debfb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod proto; pub mod client; +pub mod workspace; pub mod controller; pub mod cursor; pub mod errors; diff --git a/src/workspace.rs b/src/workspace.rs new file mode 100644 index 0000000..56609da --- /dev/null +++ b/src/workspace.rs @@ -0,0 +1,57 @@ +use std::{collections::BTreeMap, ops::Range}; + +use tokio::sync::RwLock; +use tonic::Status; + +use crate::{ + client::CodempClient, factory::OperationFactory, + controller::{buffer::{OperationControllerHandle, OperationControllerSubscriber}, + cursor::{CursorControllerHandle, CursorSubscriber}}, proto::Cursor, +}; + +pub struct Workspace { + client: CodempClient, + buffers: RwLock>, + cursor: RwLock, +} + +impl Workspace { + pub async fn new(mut client: CodempClient) -> Result { + Ok( + Workspace { + cursor: RwLock::new(client.listen().await?), + client, + buffers: RwLock::new(BTreeMap::new()), + } + ) + } + + pub async fn create(&self, path: &str, content: Option) -> Result { + self.client.clone().create(path.into(), content).await + } + + pub async fn attach(&self, path: String) -> Result<(), Status> { + self.buffers.write().await.insert( + path.clone(), + self.client.clone().attach(path).await? + ); + Ok(()) + } + + pub async fn diff(&self, path: &str, span: Range, text: &str) { + if let Some(controller) = self.buffers.read().await.get(path) { + if let Some(op) = controller.delta(span.start, text, span.end) { + controller.apply(op).await + } + } + } + + pub async fn send(&self, path: &str, start: (i32, i32), end: (i32, i32)) { + self.cursor.read().await.send(path, start.into(), end.into()).await + } + + pub async fn recv(&self) -> Option { + self.cursor.write().await.poll().await + + } +}