Skip to main content

fallow_engine/
source.rs

1//! Source parsing contracts owned by the engine boundary.
2
3use std::sync::atomic::AtomicBool;
4
5use fallow_types::discover::DiscoveredFile;
6#[cfg(test)]
7pub use fallow_types::extract::{ExportName, MemberKind, VisibilityTag};
8pub use fallow_types::extract::{ModuleInfo, ParseResult, SourceReadFailure};
9
10type CacheStore = fallow_extract::cache::CacheStore;
11
12/// On-demand, transient function extraction for local similar-code inference.
13pub mod similar_code {
14    use std::path::Path;
15
16    pub use fallow_types::similar_code::{
17        ExtractedSimilarCodeFunction, SIMILAR_CODE_EXTRACTION_SEMANTICS_VERSION,
18        SimilarCodeExtraction, SimilarCodeExtractionLimits, SimilarCodeExtractionSkip,
19        SimilarCodeExtractionSkipReason, SimilarCodeFunctionKind, SimilarCodeFunctionLocation,
20        SimilarCodeSideEffectHint, SimilarCodeSourceDigest,
21    };
22
23    /// Extract bounded named functions without touching the normal parse cache.
24    #[must_use]
25    pub fn extract(
26        path: &Path,
27        source: &str,
28        limits: SimilarCodeExtractionLimits,
29    ) -> SimilarCodeExtraction {
30        fallow_extract::extract_similar_code_functions(path, source, limits)
31    }
32}
33
34/// Source inventory walking for coverage and upload surfaces.
35pub mod inventory {
36    use std::path::Path;
37
38    use rustc_hash::FxHashMap;
39
40    /// A single static-inventory entry for one function.
41    ///
42    /// This is the engine-owned inventory contract exposed to CLI upload
43    /// surfaces. The extractor owns AST traversal; the engine owns the public
44    /// shape that downstream crates construct and upload.
45    #[derive(Debug, Clone, PartialEq, Eq)]
46    pub struct InventoryEntry {
47        /// Beacon-compatible function name.
48        pub name: String,
49        /// 1-based source line of the function declaration.
50        pub line: u32,
51        /// 1-indexed UTF-16 column of the function node start.
52        pub start_column: u32,
53        /// 1-based source line where the function node ends.
54        pub end_line: u32,
55        /// 1-indexed UTF-16 column of the function node end.
56        pub end_column: u32,
57        /// Content digest of the function's full-span source slice.
58        pub source_hash: String,
59    }
60
61    impl From<fallow_extract::inventory::InventoryEntry> for InventoryEntry {
62        fn from(entry: fallow_extract::inventory::InventoryEntry) -> Self {
63            Self {
64                name: entry.name,
65                line: entry.line,
66                start_column: entry.start_column,
67                end_line: entry.end_line,
68                end_column: entry.end_column,
69                source_hash: entry.source_hash,
70            }
71        }
72    }
73
74    /// Per-function static complexity collected alongside the inventory walk.
75    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
76    pub struct InventoryComplexity {
77        /// `McCabe` cyclomatic complexity (1 + decision points).
78        pub cyclomatic: u16,
79        /// `SonarSource` cognitive complexity (structural + nesting penalty).
80        pub cognitive: u16,
81    }
82
83    impl From<fallow_extract::inventory::InventoryComplexity> for InventoryComplexity {
84        fn from(complexity: fallow_extract::inventory::InventoryComplexity) -> Self {
85            Self {
86                cyclomatic: complexity.cyclomatic,
87                cognitive: complexity.cognitive,
88            }
89        }
90    }
91
92    /// Walk source and emit engine-owned function inventory entries.
93    #[must_use]
94    pub fn walk_source(path: &Path, source: &str) -> Vec<InventoryEntry> {
95        fallow_extract::inventory::walk_source(path, source)
96            .into_iter()
97            .map(InventoryEntry::from)
98            .collect()
99    }
100
101    /// Walk source once and emit inventory entries plus static complexity by source hash.
102    #[must_use]
103    pub fn walk_source_with_complexity(
104        path: &Path,
105        source: &str,
106    ) -> (Vec<InventoryEntry>, FxHashMap<String, InventoryComplexity>) {
107        let (entries, complexity) =
108            fallow_extract::inventory::walk_source_with_complexity(path, source);
109        let entries = entries.into_iter().map(InventoryEntry::from).collect();
110        let complexity = complexity
111            .into_iter()
112            .map(|(hash, metrics)| (hash, InventoryComplexity::from(metrics)))
113            .collect();
114        (entries, complexity)
115    }
116}
117
118/// Parse discovered source files into typed module facts.
119///
120/// Keeping parsing behind the engine boundary lets sessions and future
121/// incremental runners choose cache policy without exposing the extract crate
122/// as the public orchestration layer.
123///
124/// `cancellation`, when set mid-parse, turns every remaining file into a no-op
125/// and truncates the returned modules; see
126/// [`fallow_extract::parse_all_files_cancellable`].
127#[must_use]
128pub(crate) fn parse_all_files(
129    files: &[DiscoveredFile],
130    cache: Option<&CacheStore>,
131    need_complexity: bool,
132    cancellation: Option<&AtomicBool>,
133) -> ParseResult {
134    fallow_extract::parse_all_files_cancellable(files, cache, need_complexity, cancellation)
135}