Update src/main.rs

This commit is contained in:
Григорий Сафронов 2025-05-25 20:40:33 +00:00
parent 4d3152004b
commit 88e7c6c7b6

View File

@ -55,7 +55,7 @@ fn main() {
// Print prompt // Print prompt
print_prompt(&host, port); print_prompt(&host, port);
// Read user input // Read input
input.clear(); input.clear();
io::stdin().read_line(&mut input).expect("Failed to read input"); io::stdin().read_line(&mut input).expect("Failed to read input");
let input = input.trim(); let input = input.trim();
@ -81,7 +81,7 @@ fn main() {
print_response(&response); print_response(&response);
} }
Err(e) => { Err(e) => {
eprintln!("Error: {}", e); eprintln!("Error: {}", e.to_string().replace("KeyDB", "Futriix"));
// Check if connection was lost // Check if connection was lost
if e.kind() == io::ErrorKind::ConnectionAborted || if e.kind() == io::ErrorKind::ConnectionAborted ||
e.kind() == io::ErrorKind::ConnectionReset { e.kind() == io::ErrorKind::ConnectionReset {
@ -94,17 +94,14 @@ fn main() {
} }
fn is_valid_command(cmd: &str) -> bool { fn is_valid_command(cmd: &str) -> bool {
// Basic validation - command should not be empty and should contain only printable characters
if cmd.is_empty() { if cmd.is_empty() {
return false; return false;
} }
// Check for control characters
if cmd.chars().any(|c| c.is_control()) { if cmd.chars().any(|c| c.is_control()) {
return false; return false;
} }
// Check for valid command structure (at least one non-whitespace character)
cmd.split_whitespace().next().is_some() cmd.split_whitespace().next().is_some()
} }
@ -115,29 +112,29 @@ fn print_prompt(host: &str, port: u16) {
} }
fn send_command(stream: &TcpStream, command: &str) -> io::Result<resp::Value> { fn send_command(stream: &TcpStream, command: &str) -> io::Result<resp::Value> {
// Parse command into RESP format
let parts: Vec<&str> = command.split_whitespace().collect(); let parts: Vec<&str> = command.split_whitespace().collect();
let mut resp_command = String::new(); let mut resp_command = String::new();
// RESP protocol: *<number of arguments>\r\n$<length of argument>\r\n<argument>\r\n...
resp_command.push_str(&format!("*{}\r\n", parts.len())); resp_command.push_str(&format!("*{}\r\n", parts.len()));
for part in parts { for part in parts {
resp_command.push_str(&format!("${}\r\n{}\r\n", part.len(), part)); resp_command.push_str(&format!("${}\r\n{}\r\n", part.len(), part));
} }
// Send command
let mut stream = stream.try_clone()?; let mut stream = stream.try_clone()?;
stream.write_all(resp_command.as_bytes())?; stream.write_all(resp_command.as_bytes())?;
// Read response
let mut decoder = resp::Decoder::new(&stream); let mut decoder = resp::Decoder::new(&stream);
decoder.decode() decoder.decode()
} }
fn print_response(value: &resp::Value) { fn print_response(value: &resp::Value) {
match value { match value {
resp::Value::SimpleString(s) | resp::Value::BulkString(s) => println!("{}", s), resp::Value::SimpleString(s) | resp::Value::BulkString(s) => {
resp::Value::Error(e) => println!("(error) {}", e), println!("{}", s.replace("KeyDB", "Futriix"))
},
resp::Value::Error(e) => {
println!("(error) {}", e.replace("KeyDB", "Futriix"))
},
resp::Value::Integer(i) => println!("(integer) {}", i), resp::Value::Integer(i) => println!("(integer) {}", i),
resp::Value::Array(arr) => { resp::Value::Array(arr) => {
for (i, item) in arr.iter().enumerate() { for (i, item) in arr.iter().enumerate() {