Skip to main content

brokk_bifrost_python/
source_facts.rs

1//! Mounted read-side input for Python declaration consumers.
2
3use brokk_bifrost_core::analyzer::ProjectFile;
4use brokk_bifrost_core::analyzer::python_facts::PythonSourceFacts;
5use brokk_bifrost_core::analyzer::query_token::QueryToken;
6use brokk_bifrost_core::analyzer::source_facts::SourceFactRows;
7use std::sync::Arc;
8
9#[derive(Debug, Clone)]
10pub struct PythonFileSourceFacts {
11    pub occurrences: SourceFactRows,
12    pub facts: PythonSourceFacts,
13}
14
15impl PythonFileSourceFacts {
16    /// Select the canonical callable for one exact current-syntax focus.
17    pub fn callable_return_for_range(
18        &self,
19        start: usize,
20        end: usize,
21        keep_going: &dyn Fn() -> bool,
22    ) -> Option<&brokk_bifrost_core::analyzer::python_facts::PythonCallableReturnFact> {
23        for fact in &self.facts.callable_returns {
24            if !keep_going() {
25                return None;
26            }
27            let declaration = self.occurrences.declaration(fact.declaration);
28            let range = self.occurrences.occurrence(declaration.occurrence).range;
29            if range.start_byte == start && range.end_byte == end {
30                return Some(fact);
31            }
32        }
33        None
34    }
35
36    pub fn estimated_retained_bytes(&self) -> usize {
37        self.occurrences
38            .estimated_bytes()
39            .saturating_add(self.facts.estimated_retained_bytes())
40    }
41}
42
43pub trait PythonSourceFactProvider: Send + Sync {
44    /// Missing publication is unavailable; a published empty file returns Some.
45    /// `keep_going` is the caller's admission and traversal continuation check.
46    fn python_source_facts(
47        &self,
48        token: QueryToken<'_>,
49        file: &ProjectFile,
50        keep_going: &dyn Fn() -> bool,
51    ) -> Option<Arc<PythonFileSourceFacts>>;
52}