pedant_core/resolution/rust/snapshot/
target.rs1use std::sync::Arc;
9
10use crate::resolution::rust::identity::TargetId;
11use crate::resolution::rust::project::RustProject;
12
13use super::authority;
14use super::closure::{self, ClosureEntry};
15use super::error::RustSnapshotError;
16use super::source::{self, RustSource};
17use super::store::{SourceStore, refuse};
18
19#[derive(Debug)]
21pub struct RustTargetSnapshot {
22 target: TargetId,
23 crate_root: Arc<str>,
24 sources: Box<[RustSource]>,
25}
26
27impl RustTargetSnapshot {
28 pub fn target(&self) -> TargetId {
30 self.target
31 }
32
33 pub fn crate_root(&self) -> &str {
35 &self.crate_root
36 }
37
38 pub fn sources(&self) -> &[RustSource] {
40 &self.sources
41 }
42
43 pub fn source(&self, path: &str) -> Option<&RustSource> {
45 source::find(&self.sources, path)
46 }
47}
48
49pub(in crate::resolution::rust) fn build(
51 project: &RustProject,
52 id: TargetId,
53) -> Result<RustTargetSnapshot, RustSnapshotError> {
54 let target = authority::validated_target(project, id)?;
55 let mut store = SourceStore::new(project.root(), project.limits());
56 let mut failures = Vec::new();
57 let entry = ClosureEntry {
58 target_name: target.name(),
59 entry_path: target.entry_path(),
60 edition: target.edition(),
61 };
62 let closure = closure::walk_unit(&mut store, &entry, &mut failures);
63 match (closure, failures.is_empty()) {
64 (Some(_), true) => Ok(RustTargetSnapshot {
65 target: id,
66 crate_root: Arc::clone(&target.entry_path),
67 sources: store.finish(),
68 }),
69 (None, _) | (Some(_), false) => Err(refuse(&store, failures)),
70 }
71}