aft_parse/callgraph_store/
join_record.rs1use std::collections::{BTreeMap, BTreeSet};
10
11use aft_store::content::codec::{Decoder, Encoder};
12use aft_store::content::DecodeError;
13
14use super::super::fact_answers::{FactAnswer, FactAnswers, FactQuestion};
15use super::{
16 finish_binding, ConfigConsultations, DispatchTypeTargetTrace, DispatchTypeTargets,
17 ViewBindingDependencies, ViewSurfaceQuery,
18};
19
20fn strings(out: &mut Encoder, values: &BTreeSet<String>) {
21 out.usize(values.len());
22 for value in values {
23 out.str(value);
24 }
25}
26
27fn string_set(input: &mut Decoder<'_>) -> Result<BTreeSet<String>, DecodeError> {
28 let count = input.count()?;
29 (0..count).map(|_| input.string()).collect()
30}
31
32fn config(out: &mut Encoder, value: &ConfigConsultations) {
33 out.usize(value.facts.len());
34 for (path, name) in &value.facts {
35 out.str(path);
36 out.str(name);
37 }
38 strings(out, &value.reads);
39 out.bool(value.unattributed);
40}
41
42fn read_config(input: &mut Decoder<'_>) -> Result<ConfigConsultations, DecodeError> {
43 let count = input.count()?;
44 let facts = (0..count)
45 .map(|_| Ok((input.string()?, input.string()?)))
46 .collect::<Result<_, DecodeError>>()?;
47 Ok(ConfigConsultations {
48 facts,
49 reads: string_set(input)?,
50 unattributed: input.bool()?,
51 })
52}
53
54fn answer(out: &mut Encoder, value: &str) {
56 match decode_hex(value) {
57 Some(bytes) => {
58 out.byte(0);
59 out.blob(&bytes);
60 }
61 None => {
62 out.byte(1);
63 out.str(value);
64 }
65 }
66}
67
68fn read_answer(input: &mut Decoder<'_>) -> Result<String, DecodeError> {
69 match input.byte()? {
70 0 => {
71 let bytes = input.blob()?;
72 Ok(bytes.iter().map(|byte| format!("{byte:02x}")).collect())
73 }
74 1 => input.string(),
75 _ => Err(DecodeError("unknown answer form")),
76 }
77}
78
79fn decode_hex(value: &str) -> Option<Vec<u8>> {
80 if value.len() != 64
81 || !value
82 .bytes()
83 .all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'))
84 {
85 return None;
86 }
87 (0..32)
88 .map(|index| u8::from_str_radix(&value[index * 2..index * 2 + 2], 16).ok())
89 .collect()
90}
91
92fn query(out: &mut Encoder, value: &ViewSurfaceQuery) {
93 use ViewSurfaceQuery as Q;
94 match value {
95 Q::Language(file) => {
96 out.byte(0);
97 out.str(file);
98 }
99 Q::Module(file, module) => {
100 out.byte(1);
101 out.str(file);
102 out.str(module);
103 }
104 Q::MacroUse(file) => {
105 out.byte(2);
106 out.str(file);
107 }
108 Q::Parent(file) => {
109 out.byte(3);
110 out.str(file);
111 }
112 Q::Reexports(file) => {
113 out.byte(4);
114 out.str(file);
115 }
116 Q::Node(file, symbol) => {
117 out.byte(5);
118 out.str(file);
119 out.str(symbol);
120 }
121 Q::Callable(file, node) => {
122 out.byte(6);
123 out.str(file);
124 out.str(node);
125 }
126 Q::Alias(file, symbol) => {
127 out.byte(7);
128 out.str(file);
129 out.str(symbol);
130 }
131 Q::Export(file, symbol) => {
132 out.byte(8);
133 out.str(file);
134 out.str(symbol);
135 }
136 Q::Default(file) => {
137 out.byte(9);
138 out.str(file);
139 }
140 Q::Contains(file) => {
141 out.byte(10);
142 out.str(file);
143 }
144 Q::Crate(name) => {
145 out.byte(11);
146 out.str(name);
147 }
148 Q::CrateRoot(file) => {
149 out.byte(12);
150 out.str(file);
151 }
152 Q::Inline(file, segments, symbol) => {
153 out.byte(13);
154 out.str(file);
155 out.strs(segments);
156 out.str(symbol);
157 }
158 Q::GoPackage(file, module) => {
159 out.byte(14);
160 out.str(file);
161 out.opt_str(module.as_deref());
162 }
163 }
164}
165
166fn read_query(input: &mut Decoder<'_>) -> Result<ViewSurfaceQuery, DecodeError> {
167 use ViewSurfaceQuery as Q;
168 Ok(match input.byte()? {
169 0 => Q::Language(input.string()?),
170 1 => Q::Module(input.string()?, input.string()?),
171 2 => Q::MacroUse(input.string()?),
172 3 => Q::Parent(input.string()?),
173 4 => Q::Reexports(input.string()?),
174 5 => Q::Node(input.string()?, input.string()?),
175 6 => Q::Callable(input.string()?, input.string()?),
176 7 => Q::Alias(input.string()?, input.string()?),
177 8 => Q::Export(input.string()?, input.string()?),
178 9 => Q::Default(input.string()?),
179 10 => Q::Contains(input.string()?),
180 11 => Q::Crate(input.string()?),
181 12 => Q::CrateRoot(input.string()?),
182 13 => Q::Inline(input.string()?, input.strings()?, input.string()?),
183 14 => Q::GoPackage(input.string()?, input.opt_string()?),
184 _ => return Err(DecodeError("unknown surface query")),
185 })
186}
187
188fn fact_answers(out: &mut Encoder, values: &FactAnswers) {
189 out.usize(values.len());
190 for (asked, answered) in values {
191 let (code, path) = match asked {
192 FactQuestion::IsFile(path) => (0, path),
193 FactQuestion::IsDir(path) => (1, path),
194 FactQuestion::Link(path) => (2, path),
195 FactQuestion::Canonical(path) => (3, path),
196 FactQuestion::ListDir(path) => (4, path),
197 FactQuestion::Config(path) => (5, path),
198 FactQuestion::ConfigField(path, _) => (6, path),
199 };
200 out.byte(code);
201 out.blob(path);
202 if let FactQuestion::ConfigField(_, name) = asked {
203 out.str(name);
204 }
205 match answered {
206 FactAnswer::Bool(value) => {
207 out.byte(0);
208 out.bool(*value);
209 }
210 FactAnswer::Path(None) => out.byte(1),
211 FactAnswer::Path(Some(path)) => {
212 out.byte(2);
213 out.blob(path);
214 }
215 FactAnswer::Digest(digest) => {
216 out.byte(3);
217 out.blob(digest);
218 }
219 }
220 }
221}
222
223fn read_fact_answers(input: &mut Decoder<'_>) -> Result<FactAnswers, DecodeError> {
224 let count = input.count()?;
225 (0..count)
226 .map(|_| {
227 let code = input.byte()?;
228 let path = input.blob()?.to_vec();
229 let asked = match code {
230 0 => FactQuestion::IsFile(path),
231 1 => FactQuestion::IsDir(path),
232 2 => FactQuestion::Link(path),
233 3 => FactQuestion::Canonical(path),
234 4 => FactQuestion::ListDir(path),
235 5 => FactQuestion::Config(path),
236 6 => FactQuestion::ConfigField(path, input.string()?),
237 _ => return Err(DecodeError("unknown fact question")),
238 };
239 let answered = match input.byte()? {
240 0 => FactAnswer::Bool(input.bool()?),
241 1 => FactAnswer::Path(None),
242 2 => FactAnswer::Path(Some(input.blob()?.to_vec())),
243 3 => FactAnswer::Digest(
244 input
245 .blob()?
246 .try_into()
247 .map_err(|_| DecodeError("a fact digest is not 16 bytes"))?,
248 ),
249 _ => return Err(DecodeError("unknown fact answer")),
250 };
251 Ok((asked, answered))
252 })
253 .collect()
254}
255
256fn queries(out: &mut Encoder, values: &[(ViewSurfaceQuery, String)]) {
257 out.usize(values.len());
258 for (asked, answered) in values {
259 query(out, asked);
260 answer(out, answered);
261 }
262}
263
264fn read_queries(input: &mut Decoder<'_>) -> Result<Vec<(ViewSurfaceQuery, String)>, DecodeError> {
265 let count = input.count()?;
266 (0..count)
267 .map(|_| Ok((read_query(input)?, read_answer(input)?)))
268 .collect()
269}
270
271pub fn encode_binding(out: &mut Encoder, value: &ViewBindingDependencies) {
273 out.usize(value.references.len());
274 for (reference, dependencies) in &value.references {
275 out.u32(*reference);
276 strings(out, dependencies);
277 }
278 config(out, &value.binding_facts);
279 config(out, &value.resolution_facts);
280 strings(out, &value.binding_probes);
281 strings(out, &value.resolved_dependencies);
282 queries(out, &value.surface_queries);
283 fact_answers(out, &value.binding_answers);
284 fact_answers(out, &value.resolution_answers);
285}
286
287pub fn decode_binding(input: &mut Decoder<'_>) -> Result<ViewBindingDependencies, DecodeError> {
289 let count = input.count()?;
290 let references = (0..count)
291 .map(|_| Ok((input.u32()?, string_set(input)?)))
292 .collect::<Result<BTreeMap<_, _>, DecodeError>>()?;
293 let mut binding = ViewBindingDependencies {
294 references,
295 binding_facts: read_config(input)?,
296 resolution_facts: read_config(input)?,
297 binding_probes: string_set(input)?,
298 resolved_dependencies: string_set(input)?,
299 surface_queries: read_queries(input)?,
300 binding_answers: read_fact_answers(input)?,
301 resolution_answers: read_fact_answers(input)?,
302 ..ViewBindingDependencies::default()
303 };
304 finish_binding(&mut binding);
305 Ok(binding)
306}
307
308pub fn encode_dispatch(out: &mut Encoder, value: &DispatchTypeTargets) {
310 out.usize(value.targets.len());
311 for (name, (file, symbol)) in &value.targets {
312 out.str(name);
313 out.str(file);
314 out.str(symbol);
315 }
316 queries(out, &value.trace.queries);
317 strings(out, &value.trace.probes);
318 fact_answers(out, &value.trace.answers);
319}
320
321pub fn decode_dispatch(input: &mut Decoder<'_>) -> Result<DispatchTypeTargets, DecodeError> {
323 let count = input.count()?;
324 let targets = (0..count)
325 .map(|_| Ok((input.string()?, (input.string()?, input.string()?))))
326 .collect::<Result<BTreeMap<_, _>, DecodeError>>()?;
327 Ok(DispatchTypeTargets {
328 targets,
329 trace: DispatchTypeTargetTrace {
330 queries: read_queries(input)?,
331 probes: string_set(input)?,
332 answers: read_fact_answers(input)?,
333 },
334 })
335}