aft_parse/callgraph_store/
facts.rs1use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6pub 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 fn records_config_facts(&self) -> bool {
25 false
26 }
27 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 fn record_domain(&self, _key: &str) {}
40 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 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
72pub 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
123pub struct FactPaths<'a> {
125 pub root: &'a Path,
126 pub facts: &'a dyn ProjectFacts,
127}
128impl FactPaths<'_> {
129 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 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}