Skip to main content

tldr_cli/commands/
extract.rs

1//! Extract command - Extract complete module info from a file
2//!
3//! Returns functions, classes, imports, and call graph for a single file.
4//! Auto-routes through daemon when available for ~35x speedup.
5
6use std::path::PathBuf;
7
8use anyhow::Result;
9use clap::Args;
10
11use tldr_core::types::ModuleInfo;
12use tldr_core::{extract_file, Language};
13
14use crate::commands::daemon_router::{params_with_file, try_daemon_route};
15use crate::output::{format_module_info_text, OutputFormat, OutputWriter};
16
17/// Extract complete module info from a file
18#[derive(Debug, Args)]
19pub struct ExtractArgs {
20    /// File to extract
21    pub file: PathBuf,
22
23    /// Programming language (auto-detected from file extension if not specified)
24    #[arg(long, short = 'l')]
25    pub lang: Option<Language>,
26}
27
28impl ExtractArgs {
29    /// Run the extract command
30    pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
31        let writer = OutputWriter::new(format, quiet);
32
33        // Try daemon first for cached result (use file's parent as project root)
34        let project = self.file.parent().unwrap_or(&self.file);
35        if let Some(result) =
36            try_daemon_route::<ModuleInfo>(project, "extract", params_with_file(&self.file))
37        {
38            if writer.is_text() {
39                writer.write_text(&format_module_info_text(&result))?;
40            } else {
41                writer.write(&result)?;
42            }
43            return Ok(());
44        }
45
46        // Fallback to direct compute
47        writer.progress(&format!(
48            "Extracting module info from {}...",
49            self.file.display()
50        ));
51
52        // Extract module info - the core function handles language detection
53        let result = extract_file(&self.file, None)?;
54
55        // Output based on format
56        if writer.is_text() {
57            writer.write_text(&format_module_info_text(&result))?;
58        } else {
59            writer.write(&result)?;
60        }
61
62        Ok(())
63    }
64}