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