use anyhow::Result;
use clap::{Args, Subcommand};
use std::path::PathBuf;
use crate::cli::OutputConfig;
use crate::tags::TagsConfig;
mod infer;
mod query;
#[cfg(test)]
mod tests;
use infer::run_infer;
use query::{run_expand, run_list, run_path, run_show, run_tree};
#[derive(Args)]
pub struct OntologyArgs {
#[command(subcommand)]
command: OntologyCommands,
#[arg(default_value = ".", global = true)]
path: PathBuf,
}
#[derive(Subcommand)]
enum OntologyCommands {
Show(ShowArgs),
Expand(ExpandArgs),
Path(PathArgs),
List(ListArgs),
Tree(TreeArgs),
Infer(InferArgs),
}
#[derive(Args)]
struct InferArgs {
#[arg(long, default_value = "0.3")]
threshold: f32,
#[arg(long, default_value = "3")]
min_size: usize,
#[arg(long, default_value = "20")]
limit: usize,
}
#[derive(Args)]
struct ShowArgs {
tag: String,
}
#[derive(Args)]
struct ExpandArgs {
tag: String,
}
#[derive(Args)]
struct PathArgs {
from: String,
to: String,
}
#[derive(Args)]
struct ListArgs {
#[arg(long)]
roots: bool,
}
#[derive(Args)]
struct TreeArgs {
root: Option<String>,
}
pub async fn run(args: OntologyArgs, output: OutputConfig) -> Result<()> {
let repo_root = args.path.canonicalize().unwrap_or(args.path);
let config = load_tags_config(&repo_root);
match args.command {
OntologyCommands::Show(a) => run_show(&config, &a, &output),
OntologyCommands::Expand(a) => run_expand(&config, &a, &output),
OntologyCommands::Path(a) => run_path(&config, &a, &output),
OntologyCommands::List(a) => run_list(&config, &a, &output),
OntologyCommands::Tree(a) => run_tree(&config, &a, &output),
OntologyCommands::Infer(a) => run_infer(&repo_root, &config, &a, &output),
}
}
fn load_tags_config(repo_root: &std::path::Path) -> TagsConfig {
let tags_path = TagsConfig::tags_path(repo_root);
if tags_path.exists() {
TagsConfig::load_or_default(&tags_path)
} else {
if let Some(global_dir) = crate::config::Config::global_config_dir() {
let global_path = global_dir.join("tags.toml");
if global_path.exists() {
return TagsConfig::load_or_default(&global_path);
}
}
TagsConfig::default()
}
}