Skip to main content

tree_sitter_language_pack/
process_config.rs

1use std::borrow::Cow;
2
3/// Configuration for the `process()` function.
4///
5/// Controls which analysis features are enabled and whether chunking is performed.
6///
7/// # Examples
8///
9/// ```
10/// use tree_sitter_language_pack::ProcessConfig;
11///
12/// // Defaults: structure + imports + exports enabled
13/// let config = ProcessConfig::new("python");
14///
15/// // With chunking
16/// let config = ProcessConfig::new("python").with_chunking(1000);
17///
18/// // Everything enabled
19/// let config = ProcessConfig::new("python").all();
20/// ```
21#[derive(Debug, Clone)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct ProcessConfig {
24    /// Language name (required).
25    pub language: Cow<'static, str>,
26    /// Extract structural items (functions, classes, etc.). Default: true.
27    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
28    pub structure: bool,
29    /// Extract import statements. Default: true.
30    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
31    pub imports: bool,
32    /// Extract export statements. Default: true.
33    #[cfg_attr(feature = "serde", serde(default = "default_true"))]
34    pub exports: bool,
35    /// Extract comments. Default: false.
36    #[cfg_attr(feature = "serde", serde(default))]
37    pub comments: bool,
38    /// Extract docstrings. Default: false.
39    #[cfg_attr(feature = "serde", serde(default))]
40    pub docstrings: bool,
41    /// Extract symbol definitions. Default: false.
42    #[cfg_attr(feature = "serde", serde(default))]
43    pub symbols: bool,
44    /// Include parse diagnostics. Default: false.
45    #[cfg_attr(feature = "serde", serde(default))]
46    pub diagnostics: bool,
47    /// Maximum chunk size in bytes. `None` disables chunking.
48    #[cfg_attr(feature = "serde", serde(default))]
49    pub chunk_max_size: Option<usize>,
50}
51
52#[cfg(feature = "serde")]
53fn default_true() -> bool {
54    true
55}
56
57impl Default for ProcessConfig {
58    fn default() -> Self {
59        Self {
60            language: Cow::Borrowed(""),
61            structure: true,
62            imports: true,
63            exports: true,
64            comments: false,
65            docstrings: false,
66            symbols: false,
67            diagnostics: false,
68            chunk_max_size: None,
69        }
70    }
71}
72
73impl ProcessConfig {
74    /// Create a new config for the given language with default settings.
75    pub fn new(language: impl Into<String>) -> Self {
76        Self {
77            language: Cow::Owned(language.into()),
78            ..Default::default()
79        }
80    }
81
82    /// Enable chunking with the given maximum chunk size in bytes.
83    pub fn with_chunking(mut self, max_size: usize) -> Self {
84        self.chunk_max_size = Some(max_size);
85        self
86    }
87
88    /// Enable all analysis features.
89    pub fn all(mut self) -> Self {
90        self.structure = true;
91        self.imports = true;
92        self.exports = true;
93        self.comments = true;
94        self.docstrings = true;
95        self.symbols = true;
96        self.diagnostics = true;
97        self
98    }
99
100    /// Disable all analysis features (only metrics computed).
101    pub fn minimal(mut self) -> Self {
102        self.structure = false;
103        self.imports = false;
104        self.exports = false;
105        self.comments = false;
106        self.docstrings = false;
107        self.symbols = false;
108        self.diagnostics = false;
109        self
110    }
111}