crystal 0.0.0

Crystal, a simple database.
Documentation
use crystal::KvStore;
use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Result as ReadlineResult};
use std::process;

fn main() -> ReadlineResult<()> {
    let mut store = KvStore::new("./data").unwrap_or_else(|err| {
        eprintln!("Failed to initialize store: {}", err);
        process::exit(1);
    });

    let mut rl = DefaultEditor::new()?;
    if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }

    loop {
        let readline = rl.readline("crystal> ");
        match readline {
            Ok(line) => {
                rl.add_history_entry(line.as_str())?;
                let parts: Vec<&str> = line.split_whitespace().collect();
                match parts.as_slice() {
                    ["set", key, value] => {
                        if let Err(e) = store.set(key.to_string(), value.to_string()) {
                            eprintln!("Error: {}", e);
                        }
                    }
                    ["get", key] => match store.get(key) {
                        Ok(Some(value)) => println!("{}", value),
                        Ok(None) => println!("Key not found"),
                        Err(e) => eprintln!("Error: {}", e),
                    },
                    ["rm", key] => {
                        if let Err(e) = store.remove(key) {
                            eprintln!("Error: {}", e);
                        }
                    }
                    ["exit"] => break,
                    [] => continue,
                    _ => println!("Unknown command. Available commands: set [key] [value], get [key], rm [key], exit"),
                }
            }
            Err(ReadlineError::Interrupted) => {
                continue;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break;
            }
            Err(err) => {
                eprintln!("Error: {:?}", err);
                break;
            }
        }
    }

    rl.save_history("history.txt").unwrap();

    Ok(())
}