Skip to main content

fallow_engine/resolved_run/
view.rs

1//! The serializable view of a resolved run.
2
3use fallow_config::{AnalysisSelection, SkipReason, WorkspaceDiagnosticKind};
4use fallow_output::{
5    AnalysesView, AnalysisView, EnvView, RESOLVED_RUN_VIEW_SCHEMA_VERSION, ResolvedRunKind,
6    ResolvedRunView, RunSettingsView, SelectionView,
7};
8use fallow_types::sourced::Sourced;
9
10use super::{AnalysisDecision, ResolvedRun};
11
12const fn selection_view(selection: AnalysisSelection) -> SelectionView {
13    match selection {
14        AnalysisSelection::Run => SelectionView::Run,
15        AnalysisSelection::Skipped(SkipReason::NotRequested) => SelectionView::NotRequested,
16        AnalysisSelection::Skipped(SkipReason::Config(_)) => SelectionView::OffByConfig,
17    }
18}
19
20fn analysis_view(decision: &AnalysisDecision) -> AnalysisView {
21    AnalysisView {
22        selection: decision.selection.clone().map(selection_view),
23        production: decision.production.clone(),
24    }
25}
26
27fn cloned<T: Clone>(value: &Sourced<T>) -> Sourced<T> {
28    value.clone()
29}
30
31impl ResolvedRun {
32    /// The view of every decision of the run, with its source. The
33    /// environment block names the variables and never holds a value.
34    pub fn view(&self) -> ResolvedRunView {
35        let settings = &self.settings;
36        let audit_base = self.audit_base();
37        ResolvedRunView {
38            schema_version: RESOLVED_RUN_VIEW_SCHEMA_VERSION,
39            kind: ResolvedRunKind::ResolvedRun,
40            surface: self.surface,
41            command: self.command,
42            root: cloned(&self.root),
43            config_file: cloned(&self.config_file),
44            analyses: AnalysesView {
45                dead_code: analysis_view(&self.dead_code),
46                duplication: analysis_view(&self.duplication),
47                health: analysis_view(&self.health),
48            },
49            settings: RunSettingsView {
50                threads: cloned(&settings.threads),
51                no_cache: cloned(&settings.no_cache),
52                allow_remote_extends: cloned(&settings.allow_remote_extends),
53                max_file_size_bytes: cloned(&settings.max_file_size_bytes),
54                cache_dir: cloned(&settings.cache_dir),
55                cache_max_size_mb: cloned(&settings.cache_max_size_mb),
56                package_baselines: cloned(&settings.package_baselines),
57                include_entry_exports: cloned(&settings.include_entry_exports),
58                type_aware: self
59                    .type_aware()
60                    .ok()
61                    .map(|type_aware| type_aware.enabled.clone()),
62                audit_base: audit_base.named,
63                audit_base_fallback: audit_base.fallback,
64            },
65            env: self.env_view(),
66            fell_back: self.fallback.is_some(),
67        }
68    }
69
70    /// The variables by name. A variable that the run reads but whose value
71    /// it rejects is in `ignored`, as a variable that the run does not read.
72    fn env_view(&self) -> EnvView {
73        let rejected: Vec<&str> = self
74            .notices
75            .iter()
76            .filter_map(|notice| match &notice.kind {
77                WorkspaceDiagnosticKind::EnvValueIgnored { name } => Some(name.as_str()),
78                _ => None,
79            })
80            .collect();
81        let (ignored_values, read): (Vec<&'static str>, Vec<&'static str>) = self
82            .env_read
83            .iter()
84            .map(|var| var.name())
85            .partition(|name| rejected.contains(name));
86        let mut ignored: Vec<&'static str> =
87            self.env_ignored.iter().map(|var| var.name()).collect();
88        ignored.extend(ignored_values);
89        EnvView { read, ignored }
90    }
91}