Skip to main content

fallow_engine/
session_graph.rs

1//! What a dead-code run of an [`AnalysisSession`] keeps of its module graph.
2
3use crate::EngineResult;
4use crate::results::{DeadCodeAnalysisArtifacts, SharedDeadCodeAnalysisArtifacts};
5use crate::session::AnalysisSession;
6
7/// What a dead-code run keeps of its module graph.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum GraphChoice {
10    /// Drop the graph after the analysis.
11    Drop,
12    /// Keep the graph, and collect the trace provenance (with the plugin
13    /// export credits) and the pipeline timings.
14    WithTrace,
15    /// Keep the graph without the trace provenance and the timings.
16    GraphOnly,
17}
18
19/// The choice of a caller that passes the `retain_graph` flag.
20impl From<bool> for GraphChoice {
21    fn from(retain_graph: bool) -> Self {
22        if retain_graph {
23            Self::WithTrace
24        } else {
25            Self::Drop
26        }
27    }
28}
29
30impl GraphChoice {
31    pub const fn keeps_graph(self) -> bool {
32        matches!(self, Self::WithTrace | Self::GraphOnly)
33    }
34
35    /// True when the run collects the trace provenance and the timings.
36    pub const fn traces(self) -> bool {
37        matches!(self, Self::WithTrace)
38    }
39}
40
41impl AnalysisSession {
42    /// Run dead-code analysis and keep the module graph, without the trace
43    /// provenance and pipeline timings that
44    /// [`Self::analyze_dead_code_with_artifacts`] also collects when it keeps
45    /// the graph. `fallow fix` reads graph facts such as the named
46    /// re-exports and needs no trace data.
47    ///
48    /// # Errors
49    ///
50    /// Returns an error if parsing or analysis fails.
51    pub fn analyze_dead_code_keeping_graph(&self) -> EngineResult<DeadCodeAnalysisArtifacts> {
52        self.analyze_dead_code_with_reuse_artifacts(false, GraphChoice::GraphOnly, false)
53            .map(SharedDeadCodeAnalysisArtifacts::into_owned)
54    }
55}