Skip to main content

cgx_core/messages/
cgx.rs

1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::{
7    bin_resolver::ResolvedBinary,
8    builder::{BuildOptions, BuildTarget},
9    config::BinaryProvider,
10    crate_resolver::ResolvedCrate,
11};
12
13/// Engine-level messages emitted by [`crate::Cgx`] around the pre-built-vs-source decision.
14///
15/// These capture the full set of facts about a crate invocation - its resolved identity and source,
16/// where its code lives on disk, the chosen build options and target, and how its binary was
17/// obtained - so that callers (and tests) can observe what cgx decided without inspecting the built
18/// binary itself.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(tag = "event", rename_all = "snake_case")]
21pub enum CgxMessage {
22    /// The fully-resolved crate and chosen build options, emitted *before* checking for a pre-built
23    /// binary.
24    ///
25    /// Whether a pre-built binary will actually be found is not yet known, so this is the crate
26    /// that is planned to be used, although whether it's to be built from source or obtained
27    /// pre-built is not yet determined.
28    CratePlan {
29        /// The resolved crate identity (name, version, source).
30        resolved: ResolvedCrate,
31        /// Path to the crate's source code on disk (a cache path, or a local dir for `--path`).
32        crate_path: PathBuf,
33        /// The build options cgx chose for this invocation.
34        options: BuildOptions,
35        /// The Rust target triple this build targets (the explicit `--target`, or the host triple).
36        target_platform: String,
37    },
38    /// How the crate's binary was ultimately obtained, emitted *after* the pre-built-vs-source
39    /// decision.
40    CrateProvenance {
41        /// The resolved crate identity (name, version, source).
42        resolved: ResolvedCrate,
43        /// Path to the crate's source code on disk.
44        crate_path: PathBuf,
45        /// The build options cgx chose for this invocation.
46        options: BuildOptions,
47        /// The Rust target triple this binary is for (the explicit `--target`, or the host triple).
48        ///
49        /// The reported target platform pertains to the resolved binary itself, not from the build
50        /// options: the binary may be for an ABI-compatible fallback of the host (a musl binary on
51        /// a glibc host, an msvc PE on a windows-gnu host, etc)
52        target_platform: String,
53        /// Whether the binary was built from source or downloaded pre-built, and where it is.
54        provenance: Provenance,
55    },
56}
57
58/// How a crate's runnable binary was obtained.
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(tag = "kind", rename_all = "snake_case")]
61pub enum Provenance {
62    /// Compiled from source by cargo.
63    BuiltFromSource {
64        /// Path to the compiled binary.
65        binary_path: PathBuf,
66        /// The concrete bin/example that was built (a `DefaultBin` request is resolved to the
67        /// actual target).
68        target_binary: BuildTarget,
69    },
70    /// Downloaded as a pre-built binary from the given provider.
71    Prebuilt {
72        /// The provider the binary was obtained from.
73        provider: BinaryProvider,
74        /// Path to the downloaded binary.
75        binary_path: PathBuf,
76    },
77}
78
79impl CgxMessage {
80    /// Construct a [`Self::CratePlan`] from the resolved crate, its source path, chosen options,
81    /// and enabled pre-built sources.
82    pub fn crate_plan(resolved: &ResolvedCrate, crate_path: &Path, options: &BuildOptions) -> Self {
83        Self::CratePlan {
84            resolved: resolved.clone(),
85            crate_path: crate_path.to_path_buf(),
86            target_platform: options.target_platform().to_string(),
87            options: options.clone(),
88        }
89    }
90
91    /// Construct a [`Self::CrateProvenance`] recording that the binary was built from source.
92    pub fn crate_provenance_built_from_source(
93        resolved: &ResolvedCrate,
94        crate_path: &Path,
95        options: &BuildOptions,
96        binary_path: &Path,
97        target_binary: BuildTarget,
98    ) -> Self {
99        Self::CrateProvenance {
100            resolved: resolved.clone(),
101            crate_path: crate_path.to_path_buf(),
102            target_platform: options.target_platform().to_string(),
103            options: options.clone(),
104            provenance: Provenance::BuiltFromSource {
105                binary_path: binary_path.to_path_buf(),
106                target_binary,
107            },
108        }
109    }
110
111    /// Construct a [`Self::CrateProvenance`] recording that a pre-built binary was used.
112    pub fn crate_provenance_prebuilt(
113        resolved: &ResolvedCrate,
114        crate_path: &Path,
115        options: &BuildOptions,
116        binary: &ResolvedBinary,
117    ) -> Self {
118        Self::CrateProvenance {
119            resolved: resolved.clone(),
120            crate_path: crate_path.to_path_buf(),
121            target_platform: binary.target.clone(),
122            options: options.clone(),
123            provenance: Provenance::Prebuilt {
124                provider: binary.provider,
125                binary_path: binary.path.clone(),
126            },
127        }
128    }
129}
130
131impl From<CgxMessage> for Message {
132    fn from(msg: CgxMessage) -> Self {
133        Message::Cgx(msg)
134    }
135}