mod commands;
mod config;
mod paths;
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(name = "furigana", version, about, long_about = None)]
struct Cli {
#[arg(long, env = "FURIGANA_DATA_DIR", global = true)]
data_dir: Option<PathBuf>,
#[arg(long, env = "FURIGANA_CONFIG", global = true)]
config: Option<PathBuf>,
#[arg(short, long, global = true)]
verbose: bool,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
enum Commands {
Lookup(commands::lookup::Args),
Repl(commands::repl::Args),
Serve(commands::serve::Args),
Dict(commands::dict::Args),
}
fn main() -> Result<()> {
let cli = Cli::parse();
init_logging(cli.verbose);
let paths = paths::Paths::resolve(cli.data_dir.as_deref(), cli.config.as_deref())?;
let cfg = config::Config::load(&paths)?;
tracing::debug!("data_dir: {}", paths.data_dir.display());
tracing::debug!("config_file: {}", paths.config_file.display());
match cli.command {
Some(Commands::Lookup(args)) => commands::lookup::run(args, &paths, &cfg),
Some(Commands::Repl(args)) => commands::repl::run(args, &paths, &cfg),
Some(Commands::Serve(args)) => commands::serve::run(args, &paths, &cfg),
Some(Commands::Dict(args)) => commands::dict::run(args, &paths, &cfg),
None => commands::repl::run(commands::repl::Args::default(), &paths, &cfg),
}
}
fn init_logging(verbose: bool) {
use tracing_subscriber::EnvFilter;
let env_filter = if verbose {
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))
} else {
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"))
};
tracing_subscriber::fmt()
.with_env_filter(env_filter)
.with_target(false)
.with_writer(std::io::stderr)
.init();
}