diff --git a/futriix-cli/src/main.rs b/futriix-cli/src/main.rs index f251ce6..804517e 100644 --- a/futriix-cli/src/main.rs +++ b/futriix-cli/src/main.rs @@ -13,6 +13,11 @@ enum Command { Get { key: String }, Update { key: String, value: serde_json::Value }, Delete { key: String }, + BeginTransaction, + CommitTransaction, + RollbackTransaction, + CreateIndex { field: String }, + DropIndex { field: String }, } #[derive(Debug, Serialize, Deserialize)] @@ -32,30 +37,24 @@ async fn send_command(cmd: Command) -> Result> { let mut stream = TcpStream::connect(&addr).await?; let cmd_bytes = rmp_serde::to_vec(&cmd)?; - - // Send command length (4 bytes) let len = cmd_bytes.len() as u32; stream.write_all(&len.to_be_bytes()).await?; - - // Send command stream.write_all(&cmd_bytes).await?; - // Read response length let mut len_buf = [0u8; 4]; stream.read_exact(&mut len_buf).await?; let len = u32::from_be_bytes(len_buf) as usize; - // Read response let mut buf = vec![0u8; len]; stream.read_exact(&mut buf).await?; - let response = rmp_serde::from_slice(&buf)?; - Ok(response) + Ok(rmp_serde::from_slice(&buf)?) } #[tokio::main] async fn main() -> Result<(), Box> { - println!("{}", "Futriix CLI Client".bright_blue()); + println!(); + println!("{}", "Futriix CLI Client".bright_cyan()); println!("Type 'help' for available commands"); let mut rl = Editor::<()>::new()?; @@ -83,14 +82,15 @@ async fn main() -> Result<(), Box> { let key = parts[1].to_string(); let value_str = parts[2..].join(" "); match serde_json::from_str(&value_str) { - Ok(value) => { - match send_command(Command::Insert { key, value }).await { - Ok(Response::Success(_)) => println!("Insert successful"), - Ok(Response::Error(e)) => println!("{}: {}", "Error".red(), e), - Err(e) => println!("{}: {}", "Connection error".red(), e), - } + Ok(value) => match send_command(Command::Insert { key, value }).await { + Ok(Response::Success(_)) => { + println!("{}", "Insert successful".bright_green()); + println!(); + }, + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), }, - Err(e) => println!("{}: Invalid JSON: {}", "Error".red(), e), + Err(e) => println!("{}: {}", "Invalid JSON".red(), e), } }, "get" | "g" => { @@ -100,8 +100,8 @@ async fn main() -> Result<(), Box> { } match send_command(Command::Get { key: parts[1].to_string() }).await { Ok(Response::Success(Some(value))) => println!("{}", serde_json::to_string_pretty(&value)?), - Ok(Response::Success(None)) => println!("Key not found"), - Ok(Response::Error(e)) => println!("{}: {}", "Error".red(), e), + Ok(Response::Success(None)) => println!("{}", "Error: Key not found".bold().red()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), Err(e) => println!("{}: {}", "Connection error".red(), e), } }, @@ -113,14 +113,12 @@ async fn main() -> Result<(), Box> { let key = parts[1].to_string(); let value_str = parts[2..].join(" "); match serde_json::from_str(&value_str) { - Ok(value) => { - match send_command(Command::Update { key, value }).await { - Ok(Response::Success(_)) => println!("Update successful"), - Ok(Response::Error(e)) => println!("{}: {}", "Error".red(), e), - Err(e) => println!("{}: {}", "Connection error".red(), e), - } + Ok(value) => match send_command(Command::Update { key, value }).await { + Ok(Response::Success(_)) => println!("{}", "Update successful".bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), }, - Err(e) => println!("{}: Invalid JSON: {}", "Error".red(), e), + Err(e) => println!("{}: {}", "Invalid JSON".red(), e), } }, "delete" | "d" => { @@ -129,8 +127,45 @@ async fn main() -> Result<(), Box> { continue; } match send_command(Command::Delete { key: parts[1].to_string() }).await { - Ok(Response::Success(_)) => println!("Delete successful"), - Ok(Response::Error(e)) => println!("{}: {}", "Error".red(), e), + Ok(Response::Success(_)) => println!("{}", "Delete successful".bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), + } + }, + "begin" => match send_command(Command::BeginTransaction).await { + Ok(Response::Success(_)) => println!("{}", "Transaction started".bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), + }, + "commit" => match send_command(Command::CommitTransaction).await { + Ok(Response::Success(_)) => println!("{}", "Transaction committed".bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), + }, + "rollback" => match send_command(Command::RollbackTransaction).await { + Ok(Response::Success(_)) => println!("{}", "Transaction rolled back".bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), + }, + "create_index" => { + if parts.len() != 2 { + println!("{}", "Usage: create_index ".red()); + continue; + } + match send_command(Command::CreateIndex { field: parts[1].to_string() }).await { + Ok(Response::Success(_)) => println!("{}", format!("Index created on field '{}'", parts[1]).bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), + Err(e) => println!("{}: {}", "Connection error".red(), e), + } + }, + "drop_index" => { + if parts.len() != 2 { + println!("{}", "Usage: drop_index ".red()); + continue; + } + match send_command(Command::DropIndex { field: parts[1].to_string() }).await { + Ok(Response::Success(_)) => println!("{}", format!("Index dropped on field '{}'", parts[1]).bright_green()), + Ok(Response::Error(e)) => println!("{}", e.bold().red()), Err(e) => println!("{}: {}", "Connection error".red(), e), } }, @@ -140,6 +175,11 @@ async fn main() -> Result<(), Box> { println!(" get|g - Get document"); println!(" update|u - Update document"); println!(" delete|d - Delete document"); + println!(" begin - Start transaction"); + println!(" commit - Commit transaction"); + println!(" rollback - Rollback transaction"); + println!(" create_index - Create index"); + println!(" drop_index - Drop index"); println!(" help|h - Show this help"); println!(" exit|quit|q - Exit the client"); }, @@ -158,4 +198,4 @@ async fn main() -> Result<(), Box> { rl.save_history("futriix-cli-history.txt")?; Ok(()) -} +} \ No newline at end of file