Skip to main content

aft_parse/callgraph_store/
facts.rs

1//! Membership, configuration and identity inputs for the existing resolver.
2
3use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6/// Immutable parse payloads are identified by their full content keys.
7pub type BlobKey = String;
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum EntryKind {
11    Regular,
12    Directory,
13    Symlink,
14}
15
16#[derive(Clone, Debug, Eq, PartialEq)]
17pub struct DirEntry {
18    pub name: Vec<u8>,
19    pub kind: EntryKind,
20}
21
22pub trait ProjectFacts {
23    /// Whether the resolver should record which configuration fields it reads.
24    fn records_config_facts(&self) -> bool {
25        false
26    }
27    /// Whether every checkout read is recorded with its answer (the data
28    /// layer's recorder), so a reader can record the key of a repository-wide
29    /// structure in place of the reads that build it.
30    fn records_answers(&self) -> bool {
31        false
32    }
33    fn config_fact(&self, _rel: &[u8], _name: &str) {}
34    fn memo_start(&self, _path: &Path, _kind: &str, _name: &str) {}
35    fn memo_finish(&self, _path: &Path, _kind: &str, _name: &str) {}
36    fn memo_replay(&self, _path: &Path, _kind: &str, _name: &str) {}
37    /// Records that the caller depends on a repository-wide structure,
38    /// named by its domain key, instead of on that structure's inputs.
39    fn record_domain(&self, _key: &str) {}
40    /// Stops recording consultations until the matching
41    /// [`ProjectFacts::resume_recording`]; for building a repository-wide
42    /// structure whose readers record its domain key instead.
43    fn pause_recording(&self) {}
44    fn resume_recording(&self) {}
45    fn workspace_package(&self, _root: &Path, _name: &str) -> Option<Option<PathBuf>> {
46        None
47    }
48    fn remember_workspace_package(&self, _root: &Path, _name: &str, _value: Option<PathBuf>) {}
49    fn workspace_members(&self, _root: &Path) -> Option<Arc<Vec<PathBuf>>> {
50        None
51    }
52    fn remember_workspace_members(&self, _root: &Path, _value: Arc<Vec<PathBuf>>) {}
53    fn is_file(&self, rel: &[u8]) -> bool;
54    fn is_dir(&self, rel: &[u8]) -> bool;
55    fn config_bytes(&self, rel: &[u8]) -> Option<Arc<[u8]>>;
56    fn attributed_config_bytes(&self, rel: &[u8]) -> Option<Arc<[u8]>> {
57        self.config_bytes(rel)
58    }
59    fn symlink_target(&self, rel: &[u8]) -> Option<&[u8]>;
60    fn canonical(&self, rel: &[u8]) -> Option<Vec<u8>>;
61    /// Probe identities may be shared within a resolver snapshot. Explicit
62    /// canonical-path requests remain live so identity revalidation is not memoized.
63    fn canonical_probe(&self, rel: &[u8]) -> Option<Vec<u8>> {
64        self.canonical(rel)
65    }
66    fn canonical_path(&self, root: &Path, rel: &[u8]) -> Option<PathBuf> {
67        Some(root.join(byte_path(&self.canonical(rel)?)))
68    }
69    fn list_dir(&self, rel: &[u8]) -> Vec<DirEntry>;
70}
71
72/// Stands for "no project facts": per-file extraction binds one file's parse
73/// on its own, before it belongs to any checkout. It is not an empty checkout.
74/// Every question answers that nothing exists, and no directory visit, read
75/// or consultation is recorded.
76pub struct NoProjectFacts;
77
78impl ProjectFacts for NoProjectFacts {
79    fn is_file(&self, _rel: &[u8]) -> bool {
80        false
81    }
82
83    fn is_dir(&self, _rel: &[u8]) -> bool {
84        false
85    }
86
87    fn config_bytes(&self, _rel: &[u8]) -> Option<Arc<[u8]>> {
88        None
89    }
90
91    fn symlink_target(&self, _rel: &[u8]) -> Option<&[u8]> {
92        None
93    }
94
95    fn canonical(&self, _rel: &[u8]) -> Option<Vec<u8>> {
96        None
97    }
98
99    fn list_dir(&self, _rel: &[u8]) -> Vec<DirEntry> {
100        Vec::new()
101    }
102}
103
104#[cfg(unix)]
105pub fn path_bytes(path: &Path) -> Vec<u8> {
106    use std::os::unix::ffi::OsStrExt;
107    path.as_os_str().as_bytes().to_vec()
108}
109#[cfg(not(unix))]
110pub fn path_bytes(path: &Path) -> Vec<u8> {
111    path.to_string_lossy().replace('\\', "/").into_bytes()
112}
113#[cfg(unix)]
114pub fn byte_path(bytes: &[u8]) -> PathBuf {
115    use std::os::unix::ffi::OsStrExt;
116    PathBuf::from(std::ffi::OsStr::from_bytes(bytes))
117}
118#[cfg(not(unix))]
119pub fn byte_path(bytes: &[u8]) -> PathBuf {
120    PathBuf::from(String::from_utf8_lossy(bytes).as_ref())
121}
122
123/// Adapts the resolver's existing absolute paths to project-relative byte keys.
124pub struct FactPaths<'a> {
125    pub root: &'a Path,
126    pub facts: &'a dyn ProjectFacts,
127}
128impl FactPaths<'_> {
129    /// Test the recording capability before allocating relative path bytes, so
130    /// the disk resolver's default hooks do no work and allocate nothing.
131    pub fn config_fact(&self, path: &Path, name: &str) {
132        if self.facts.records_config_facts() {
133            if let Some(rel) = self.rel(path) {
134                self.facts.config_fact(&rel, name);
135            }
136        }
137    }
138    pub fn config_field(&self, dir: &Path, input: &str, name: &str) {
139        if self.facts.records_config_facts() {
140            self.config_fact(&dir.join(input), name);
141        }
142    }
143    fn rel(&self, path: &Path) -> Option<Vec<u8>> {
144        Some(path_bytes(path.strip_prefix(self.root).unwrap_or(path)))
145    }
146    pub fn is_file(&self, path: &Path) -> bool {
147        self.rel(path)
148            .and_then(|rel| self.facts.canonical_probe(&rel))
149            .is_some_and(|rel| self.facts.is_file(&rel))
150    }
151    pub fn is_dir(&self, path: &Path) -> bool {
152        self.rel(path)
153            .and_then(|rel| self.facts.canonical_probe(&rel))
154            .is_some_and(|rel| self.facts.is_dir(&rel))
155    }
156    /// Opaque reads remain distinguishable from the field-instrumented readers.
157    pub fn attributed_bytes(&self, path: &Path) -> Option<Arc<[u8]>> {
158        self.facts.attributed_config_bytes(&self.rel(path)?)
159    }
160    pub fn canonical(&self, path: &Path) -> Option<PathBuf> {
161        self.facts.canonical_path(self.root, &self.rel(path)?)
162    }
163    pub fn list_dir(&self, path: &Path) -> Vec<DirEntry> {
164        self.rel(path)
165            .map(|rel| self.facts.list_dir(&rel))
166            .unwrap_or_default()
167    }
168}