feat: implemented snapshot method and some getters

This commit is contained in:
zaaarf 2024-01-27 11:49:05 +01:00
parent 6fe1b213bd
commit f7fcf8bd22
4 changed files with 25 additions and 5 deletions

2
.gitignore vendored
View file

@ -8,3 +8,5 @@ Cargo.lock
/client/vscode/codemp.node /client/vscode/codemp.node
.cargo .cargo
.vscode/

View file

@ -20,6 +20,7 @@ message Operation {
message SnapshotRequest { message SnapshotRequest {
required string path = 1; required string path = 1;
} }
message SnapshotResponse { message SnapshotResponse {
required string content = 1; required string content = 1;
} }

View file

@ -16,7 +16,7 @@ message WorkspaceUserList {
} }
message WorkspaceMessage { message WorkspaceMessage {
required int32 id = 1; //unused??? required string id = 1; //unused???
} }
message JoinRequest { message JoinRequest {
@ -25,7 +25,7 @@ message JoinRequest {
} }
message AttachRequest { message AttachRequest {
required string id = 1; required string path = 1;
} }
message Token { message Token {

View file

@ -2,7 +2,7 @@ use std::{collections::{BTreeMap, BTreeSet}, sync::Arc};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
proto::{user::UserIdentity, workspace::{AttachRequest, BufferListRequest, BufferPayload, Token, UserListRequest}}, proto::{buffer_service::SnapshotRequest, user::UserIdentity, workspace::{AttachRequest, BufferListRequest, BufferPayload, Token, UserListRequest}},
api::controller::ControllerWorker, api::controller::ControllerWorker,
buffer::{self, worker::BufferWorker}, buffer::{self, worker::BufferWorker},
client::Services, client::Services,
@ -42,6 +42,7 @@ pub struct Workspace {
} }
impl Workspace { impl Workspace {
/// create a new buffer and perform initial fetch operations
pub(crate) async fn new( pub(crate) async fn new(
id: String, id: String,
user_id: Uuid, user_id: Uuid,
@ -86,7 +87,7 @@ impl Workspace {
pub async fn attach(&mut self, path: &str) -> crate::Result<Arc<buffer::Controller>> { pub async fn attach(&mut self, path: &str) -> crate::Result<Arc<buffer::Controller>> {
let mut worskspace_client = self.services.workspace.clone(); let mut worskspace_client = self.services.workspace.clone();
self.token.send(worskspace_client.attach( self.token.send(worskspace_client.attach(
tonic::Request::new(AttachRequest { id: path.to_string() }) tonic::Request::new(AttachRequest { path: path.to_string() })
).await?.into_inner())?; ).await?.into_inner())?;
let (tx, rx) = mpsc::channel(10); let (tx, rx) = mpsc::channel(10);
@ -108,6 +109,16 @@ impl Workspace {
Ok(controller) Ok(controller)
} }
/// get a snapshot of a buffer (meaning its contents as a flat string)
pub async fn snapshot(&mut self, path: &str) -> crate::Result<String> {
let mut buffer_client = self.services.buffer.clone();
let contents = buffer_client.snapshot(
tonic::Request::new(SnapshotRequest { path: path.to_string() })
).await?.into_inner().content;
Ok(contents)
}
/// fetch a list of all buffers in a workspace /// fetch a list of all buffers in a workspace
pub async fn fetch_buffers(&mut self) -> crate::Result<()> { pub async fn fetch_buffers(&mut self) -> crate::Result<()> {
let mut workspace_client = self.services.workspace.clone(); let mut workspace_client = self.services.workspace.clone();
@ -173,6 +184,7 @@ impl Workspace {
Ok(()) Ok(())
} }
/// get the id of the workspace
pub fn id(&self) -> String { self.id.clone() } pub fn id(&self) -> String { self.id.clone() }
/// return a reference to current cursor controller, if currently in a workspace /// return a reference to current cursor controller, if currently in a workspace
@ -182,4 +194,9 @@ impl Workspace {
pub fn buffer_by_name(&self, path: &str) -> Option<Arc<buffer::Controller>> { pub fn buffer_by_name(&self, path: &str) -> Option<Arc<buffer::Controller>> {
self.buffers.get(path).cloned() self.buffers.get(path).cloned()
} }
/// get the currently cached "filetree"
pub fn filetree(&self) -> Vec<String> {
self.filetree.iter().map(|f| f.clone()).collect()
}
} }