1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Strange Days Tech S.A.S. de C.V. <https://strangedays.tech>
pub use ArboristError;
pub use LanguageProfile;
pub use ;
use Path;
/// Analyze a source file, auto-detecting language from its extension.
///
/// The language is determined from the file extension (e.g., `.rs` → Rust,
/// `.py` → Python). Uses default configuration (no threshold, methods included).
///
/// # Errors
///
/// - [`ArboristError::FileNotFound`] if the path does not exist.
/// - [`ArboristError::UnrecognizedExtension`] if the extension is unknown.
/// - [`ArboristError::LanguageNotEnabled`] if the language feature flag is off.
///
/// # Examples
///
/// ```no_run
/// use arborist::analyze_file;
///
/// let report = analyze_file("src/main.rs")?;
/// println!("{}: cognitive={}", report.path, report.file_cognitive);
/// for func in &report.functions {
/// println!(" {} cognitive={}", func.name, func.cognitive);
/// }
/// # Ok::<(), arborist::ArboristError>(())
/// ```
/// Analyze a source file with custom configuration.
///
/// Like [`analyze_file`], but accepts an [`AnalysisConfig`] to control
/// threshold flagging and method inclusion.
///
/// # Errors
///
/// Same as [`analyze_file`].
///
/// # Examples
///
/// ```no_run
/// use arborist::{analyze_file_with_config, AnalysisConfig};
///
/// let config = AnalysisConfig {
/// cognitive_threshold: Some(8),
/// ..Default::default()
/// };
/// let report = analyze_file_with_config("src/lib.rs", &config)?;
/// for func in &report.functions {
/// if func.exceeds_threshold == Some(true) {
/// eprintln!("WARNING: {} has cognitive complexity {}", func.name, func.cognitive);
/// }
/// }
/// # Ok::<(), arborist::ArboristError>(())
/// ```
/// Analyze source code provided as a string, with explicit language.
///
/// Use this when the source code is already in memory (e.g., from an editor
/// buffer or a network response). The returned [`FileReport`] will have an
/// empty `path`.
///
/// # Errors
///
/// - [`ArboristError::LanguageNotEnabled`] if the language feature flag is off.
///
/// # Examples
///
/// ```
/// use arborist::{analyze_source, Language};
///
/// let source = r#"
/// fn add(a: i32, b: i32) -> i32 {
/// a + b
/// }
/// "#;
///
/// let report = analyze_source(source, Language::Rust)?;
/// assert_eq!(report.functions.len(), 1);
/// assert_eq!(report.functions[0].name, "add");
/// assert_eq!(report.functions[0].cognitive, 0);
/// # Ok::<(), arborist::ArboristError>(())
/// ```
/// Analyze source code with explicit language and custom configuration.
///
/// Like [`analyze_source`], but accepts an [`AnalysisConfig`] to control
/// threshold flagging and method inclusion.
///
/// # Errors
///
/// Same as [`analyze_source`].
///
/// # Examples
///
/// ```
/// use arborist::{analyze_source_with_config, AnalysisConfig, Language};
///
/// let source = r#"
/// fn complex(x: i32) -> i32 {
/// if x > 0 {
/// if x > 10 {
/// x * 2
/// } else {
/// x + 1
/// }
/// } else {
/// 0
/// }
/// }
/// "#;
///
/// let config = AnalysisConfig {
/// cognitive_threshold: Some(1),
/// ..Default::default()
/// };
/// let report = analyze_source_with_config(source, Language::Rust, &config)?;
/// assert_eq!(report.functions[0].exceeds_threshold, Some(true));
/// # Ok::<(), arborist::ArboristError>(())
/// ```