assemble_core/dependencies/
resolved_dependency.rs

1use crate::file_collection::FileSet;
2use crate::flow::shared::{Artifact, ImmutableArtifact, IntoArtifact};
3
4use crate::project::buildable::{BuiltByContainer, IntoBuildable};
5
6use std::collections::HashSet;
7use std::ops::{Add, AddAssign};
8use std::path::PathBuf;
9
10/// A resolved dependency contains information on the artifacts it stores and the downloaded files
11/// it refers to
12#[derive(Debug, Clone)]
13pub struct ResolvedDependency {
14    artifacts: HashSet<ImmutableArtifact>,
15    files: HashSet<PathBuf>,
16}
17
18impl ResolvedDependency {
19    /// Gets the files that are associated with this resolved dependency
20    pub fn artifact_files(&self) -> FileSet {
21        self.files.iter().fold(FileSet::new(), |fc, file| fc + file)
22    }
23
24    /// Gets artifact that are associated with this resolved dependency
25    pub fn artifacts(&self) -> HashSet<impl Artifact> {
26        self.artifacts.clone()
27    }
28
29    /// Joins two resolved dependency into one
30    pub fn join(self, other: Self) -> Self {
31        Self {
32            artifacts: self.artifacts.union(&other.artifacts).cloned().collect(),
33            files: self.files.union(&other.files).cloned().collect(),
34        }
35    }
36}
37
38pub struct ResolvedDependencyBuilder {
39    artifacts: HashSet<ImmutableArtifact>,
40    built_by: BuiltByContainer,
41}
42
43impl ResolvedDependencyBuilder {
44    /// Ensures that there's always at least one artifact in the resolved dependency
45    pub fn new<A: IntoArtifact>(artifact: A) -> Self {
46        let mut output = Self {
47            artifacts: HashSet::new(),
48            built_by: Default::default(),
49        };
50        output.add(artifact);
51        output
52    }
53
54    /// Add an object of type that can be turned into an artifact
55    pub fn add<A: IntoArtifact>(&mut self, artifact: A) {
56        let artifact = artifact.into_artifact();
57        if let Some(buildable) = artifact.buildable() {
58            self.built_by.add(buildable);
59        }
60        self.artifacts.insert(ImmutableArtifact::new(artifact));
61    }
62
63    /// Add objects of type that can be turned into an artifact
64    pub fn add_many<I, A: IntoArtifact>(&mut self, artifacts: I)
65    where
66        I: IntoIterator<Item = A>,
67    {
68        for artifact in artifacts {
69            self.add(artifact);
70        }
71    }
72
73    /// Add something that builds this dependency
74    pub fn built_by<B: IntoBuildable>(mut self, buildable: B) -> Self
75    where
76        <B as IntoBuildable>::Buildable: 'static,
77    {
78        self.built_by.add(buildable);
79        self
80    }
81
82    pub fn finish(self) -> ResolvedDependency {
83        let files = self
84            .artifacts
85            .iter()
86            .map(|i| i.file())
87            .collect::<HashSet<_>>();
88
89        ResolvedDependency {
90            artifacts: self.artifacts,
91            files,
92        }
93    }
94}
95
96impl<A: IntoArtifact> AddAssign<A> for ResolvedDependencyBuilder {
97    fn add_assign(&mut self, rhs: A) {
98        self.add(rhs)
99    }
100}