From 2472164350af6b83253fffa93d5a86fea45fba4c Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 11 Apr 2023 14:00:49 +0200 Subject: [PATCH] chore: removed cli client --- Cargo.toml | 5 -- src/client/cli/main.rs | 46 ----------------- src/client/nvim/client.rs | 104 -------------------------------------- 3 files changed, 155 deletions(-) delete mode 100644 src/client/cli/main.rs delete mode 100644 src/client/nvim/client.rs diff --git a/Cargo.toml b/Cargo.toml index 98e3c18..f19676a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,6 @@ path = "src/lib/lib.rs" name = "server" path = "src/server/main.rs" -[[bin]] -name = "client-cli" -path = "src/client/cli/main.rs" -required-features = ["cli"] - [[bin]] name = "client-nvim" path = "src/client/nvim/main.rs" diff --git a/src/client/cli/main.rs b/src/client/cli/main.rs deleted file mode 100644 index ad27332..0000000 --- a/src/client/cli/main.rs +++ /dev/null @@ -1,46 +0,0 @@ -use clap::Parser; -use codemp::proto::{buffer_client::BufferClient, BufferPayload}; -use tokio_stream::StreamExt; - -#[derive(Parser, Debug)] -struct CliArgs { - /// path of buffer to create - path: String, - - /// initial content for buffer - #[arg(short, long)] - content: Option, - - /// attach instead of creating a new buffer - #[arg(long, default_value_t = false)] - attach: bool, - - /// host to connect to - #[arg(long, default_value = "http://[::1]:50051")] - host: String, -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let args = CliArgs::parse(); - - let mut client = BufferClient::connect(args.host).await?; - - let request = BufferPayload { - path: args.path, - content: args.content, - }; - - if !args.attach { - client.create(request.clone()).await.unwrap(); - } - - let mut stream = client.attach(request).await.unwrap().into_inner(); - - while let Some(item) = stream.next().await { - println!("> {:?}", item); - } - - Ok(()) -} - diff --git a/src/client/nvim/client.rs b/src/client/nvim/client.rs deleted file mode 100644 index 2fab297..0000000 --- a/src/client/nvim/client.rs +++ /dev/null @@ -1,104 +0,0 @@ -use std::sync::{Arc, Mutex}; - -use codemp::{proto::{buffer_client::BufferClient, BufferPayload, RawOp, OperationRequest}, tonic::{transport::Channel, Status, Streaming}, opfactory::OperationFactory}; -use tracing::{error, warn}; -use uuid::Uuid; - -type FactoryHandle = Arc>; - -impl From::> for CodempClient { - fn from(x: BufferClient) -> CodempClient { - CodempClient { - id: Uuid::new_v4(), - client:x, - factory: Arc::new(Mutex::new(OperationFactory::new(None))) - } - } -} - -#[derive(Clone)] -pub struct CodempClient { - id: Uuid, - client: BufferClient, - factory: FactoryHandle, // TODO less jank solution than Arc -} - -impl CodempClient { - pub async fn create(&mut self, path: String, content: Option) -> Result { - Ok( - self.client.create( - BufferPayload { - path, - content, - user: self.id.to_string(), - } - ) - .await? - .into_inner() - .accepted - ) - } - - pub async fn insert(&mut self, path: String, txt: String, pos: u64) -> Result { - let res = { self.factory.lock().unwrap().insert(&txt, pos) }; - match res { - Ok(op) => { - Ok( - self.client.edit( - OperationRequest { - path, - hash: "".into(), - opseq: serde_json::to_string(&op).unwrap(), - user: self.id.to_string(), - } - ) - .await? - .into_inner() - .accepted - ) - }, - Err(e) => Err(Status::internal(format!("invalid operation: {}", e))), - } - } - - pub async fn attach () + Send + 'static>(&mut self, path: String, callback: F) -> Result<(), Status> { - let stream = self.client.attach( - BufferPayload { - path, - content: None, - user: self.id.to_string(), - } - ) - .await? - .into_inner(); - - let factory = self.factory.clone(); - tokio::spawn(async move { Self::worker(stream, factory, callback).await } ); - - Ok(()) - } - - async fn worker ()>(mut stream: Streaming, factory: FactoryHandle, callback: F) { - loop { - match stream.message().await { - Ok(v) => match v { - Some(operation) => { - let op = serde_json::from_str(&operation.opseq).unwrap(); - let res = { factory.lock().unwrap().process(op) }; - match res { - Ok(x) => callback(x), - Err(e) => break error!("desynched: {}", e), - } - } - None => break warn!("stream closed"), - }, - Err(e) => break error!("error receiving change: {}", e), - } - } - } - - pub fn content(&self) -> String { - let factory = self.factory.lock().unwrap(); - factory.content() - } -}