online Minecraft written book viewer

feat: split cli

kokirigla.de 0c4853e8 982ec075

verified
+29 -12
+14 -2
src/cli.rs
··· 1 1 use std::path::PathBuf; 2 2 3 - use clap::{ArgAction, Parser}; 3 + use clap::{ArgAction, Args, Parser, Subcommand}; 4 4 5 5 use crate::web::TextureKind; 6 6 7 7 #[derive(Parser, Debug)] 8 8 #[command(version, about, long_about = None)] 9 - pub struct Args { 9 + pub struct Cli { 10 + #[command(subcommand)] 11 + pub command: Command, 12 + } 13 + 14 + #[derive(Subcommand, Debug)] 15 + pub enum Command { 16 + /// Build the library and optionally run the webserver. 17 + Serve(ServeArgs), 18 + } 19 + 20 + #[derive(Args, Debug)] 21 + pub struct ServeArgs { 10 22 /// The path to the `realm.nbt` file created by Infinity Item Editor. 11 23 #[arg(short = 'r', long = "realm", default_value = "realm.nbt")] 12 24 pub realm_path: PathBuf,
+13 -8
src/main.rs
··· 4 4 use clap::Parser as _; 5 5 6 6 use crate::{ 7 - cli::Args, 7 + cli::{Cli, Command, ServeArgs}, 8 8 library::{Library, Realm}, 9 9 web::start_webserver, 10 10 }; ··· 18 18 #[tokio::main] 19 19 async fn main() -> anyhow::Result<()> { 20 20 tracing_subscriber::fmt::init(); 21 - let cli_args = Args::parse(); 22 - let library = build_library(&cli_args).context("Building library")?; 21 + let cli = Cli::parse(); 22 + 23 + match cli.command { 24 + Command::Serve(args) => { 25 + let library = build_library(&args).context("Building library")?; 23 26 24 - if cli_args.start_webserver { 25 - start_webserver(&cli_args, library) 26 - .await 27 - .context("Running webserver")?; 27 + if args.start_webserver { 28 + start_webserver(&args, library) 29 + .await 30 + .context("Running webserver")?; 31 + } 32 + } 28 33 } 29 34 30 35 Ok(()) 31 36 } 32 37 33 - fn build_library(args: &Args) -> anyhow::Result<Library> { 38 + fn build_library(args: &ServeArgs) -> anyhow::Result<Library> { 34 39 let realm_path = &args.realm_path; 35 40 36 41 if !fs::exists(realm_path)
+2 -2
src/web.rs
··· 8 8 use tokio::net::TcpListener; 9 9 use tower_http::compression::CompressionLayer; 10 10 11 - use crate::{cli::Args, library::Library}; 11 + use crate::{cli::ServeArgs, library::Library}; 12 12 13 13 pub mod api; 14 14 pub mod assets; ··· 32 32 } 33 33 34 34 pub async fn start_webserver( 35 - args: &Args, 35 + args: &ServeArgs, 36 36 library: Library, 37 37 ) -> anyhow::Result<()> { 38 38 let library = Arc::new(Mutex::new(library));