backhopper_core/compat/
target_tree_index.rs1use std::collections::{BTreeMap, BTreeSet};
11use std::path::{Path, PathBuf};
12use std::str::FromStr;
13
14use crate::model::names::{CommitSha, GitRef, ModuleName};
15
16#[derive(Debug, Clone)]
17pub struct TargetTreeIndex {
18 target_repo: PathBuf,
19 target_ref: GitRef,
20 resolved_commit: CommitSha,
21 present_paths: BTreeSet<PathBuf>,
22 erl_modules: BTreeMap<ModuleName, PathBuf>,
23}
24
25impl TargetTreeIndex {
26 #[must_use]
27 pub fn from_parts(
28 target_repo: PathBuf,
29 target_ref: GitRef,
30 resolved_commit: CommitSha,
31 present_paths: BTreeSet<PathBuf>,
32 ) -> Self {
33 let mut by_module: BTreeMap<ModuleName, Option<PathBuf>> = BTreeMap::new();
35 for path in &present_paths {
36 if path.extension().and_then(|e| e.to_str()) == Some("erl")
37 && let Some(stem) = path.file_stem().and_then(|s| s.to_str())
38 && let Ok(module) = ModuleName::from_str(stem)
39 {
40 by_module
41 .entry(module)
42 .and_modify(|slot| *slot = None)
43 .or_insert_with(|| Some(path.clone()));
44 }
45 }
46 let erl_modules = by_module
47 .into_iter()
48 .filter_map(|(m, p)| p.map(|p| (m, p)))
49 .collect();
50 Self {
51 target_repo,
52 target_ref,
53 resolved_commit,
54 present_paths,
55 erl_modules,
56 }
57 }
58
59 pub fn target_repo(&self) -> &Path {
60 &self.target_repo
61 }
62
63 pub fn target_ref(&self) -> &GitRef {
64 &self.target_ref
65 }
66
67 pub fn resolved_commit(&self) -> &CommitSha {
68 &self.resolved_commit
69 }
70
71 pub fn contains_path(&self, p: &Path) -> bool {
72 self.present_paths.contains(p)
73 }
74
75 pub fn present_paths(&self) -> &BTreeSet<PathBuf> {
76 &self.present_paths
77 }
78
79 pub fn module_erl_path(&self, module: &ModuleName) -> Option<&Path> {
82 self.erl_modules.get(module).map(PathBuf::as_path)
83 }
84}