memplace 0.1.0

command and snippet manager
mod commands;
mod config;
mod storage;
mod tui;
use clap::{Parser, Subcommand};
use commands::{save, search};
use std::process;

#[derive(Parser, Debug)]
#[command(version,about,long_about=None)]
struct Args {
    #[command(subcommand)]
    command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
    /// Search for a saved command using levenhstein  
    Search {
        /// The keyword or phrase to search for (e.g., "git commit")
        pattern: String,
    },

    /// Save a new command to your database
    Save {
        /// The actual command you want to save
        val: String,
    },
}
fn main() {
    let db_path_buf = config::get_database_path();
    let db_path_str = db_path_buf.to_str().expect("Path contains invalid unicode");
    let mut posts = storage::read_it(db_path_str).unwrap_or_else(|err| {
        eprintln!("unable to locate/create jsonfile {:?}", err);
        process::exit(1)
    });

    let args = Args::parse();
    match args.command {
        Command::Search { pattern } => {
            search::execute(&pattern, &posts).unwrap();
            // tui::check().unwrap();
        }

        Command::Save { val } => {
            save::execute(&val, &mut posts, db_path_str).unwrap_or_else(|err| {
                eprintln!(
                    "unable to add the command to the database , error: {:?}",
                    err
                )
            })
        }
    }
}