use std::process::ExitCode;
use clap::{Args, ValueEnum};
use sdml_core::{
load::ModuleLoader,
store::{InMemoryModuleCache, ModuleStore},
};
use sdml_errors::Error;
use sdml_generate::{
actions::deps::{DependencyViewGenerator, DependencyViewOptions, DependencyViewRepresentation},
Generator,
};
#[derive(Args, Debug)]
pub(crate) struct Command {
#[arg(short = 'f', long)]
#[arg(value_enum)]
#[arg(default_value_t = OutputFormat::Tree)]
output_format: OutputFormat,
#[arg(short = 'd', long)]
#[arg(default_value = "0")]
depth: usize,
#[command(flatten)]
files: super::FileArgs,
}
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum OutputFormat {
Graph,
Rdf,
Tree,
}
impl super::Command for Command {
#[allow(clippy::redundant_closure_call)]
fn execute(&self) -> Result<ExitCode, Error> {
call_with_module!(self, |module, cache: &InMemoryModuleCache, _| {
let options: DependencyViewOptions = DependencyViewOptions::default()
.with_depth(self.depth)
.with_representation(self.output_format.into());
let mut generator = DependencyViewGenerator::default();
let mut output = self.files.output.clone();
let mut writer = output.lock();
generator.generate_with_options(module, cache, options, None, &mut writer)?;
Ok(ExitCode::SUCCESS)
});
}
}
impl From<OutputFormat> for DependencyViewRepresentation {
fn from(v: OutputFormat) -> Self {
match v {
OutputFormat::Tree => DependencyViewRepresentation::TextTree,
OutputFormat::Graph => DependencyViewRepresentation::DotGraph(Default::default()),
OutputFormat::Rdf => DependencyViewRepresentation::RdfImports,
}
}
}