use clap::{Parser, Subcommand}; mod markdown; use markdown::{get_all_posts, get_post_by_slug, get_posts_by_tag, watch_posts}; use serde_json; use std::fs; #[derive(Parser)] #[command(name = "Markdown Backend")] #[command(about = "A CLI for managing markdown blog posts", long_about = None)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// List all posts List, /// Show a post by slug Show { slug: String, }, /// List posts by tag Tags { tag: String, }, /// Watch for changes in the posts directory Watch, /// Show Rust parser statistics Rsparseinfo, } fn main() { markdown::load_post_cache_from_disk(); let cli = Cli::parse(); match &cli.command { Commands::List => { let posts = get_all_posts().unwrap_or_else(|e| { eprintln!("{}", e); std::process::exit(1); }); println!("{}", serde_json::to_string(&posts).unwrap()); } Commands::Show { slug } => { match get_post_by_slug(slug) { Ok(post) => { println!("{}", serde_json::to_string(&post).unwrap()); markdown::save_post_cache_to_disk(); } Err(e) => { eprintln!("{}", e); std::process::exit(1); } } } Commands::Tags { tag } => { let posts = get_posts_by_tag(tag).unwrap_or_else(|e| { eprintln!("{}", e); std::process::exit(1); }); println!("{}", serde_json::to_string(&posts).unwrap()); } Commands::Watch => { println!("Watching for changes in posts directory. Press Ctrl+C to exit."); let _ = watch_posts(|| { println!("Posts directory changed!"); }); // Keep the main thread alive loop { std::thread::sleep(std::time::Duration::from_secs(60)); } } Commands::Rsparseinfo => { println!("{}", markdown::rsparseinfo()); } } }