Skip to main content

cgx_core/messages/
prebuilt_binary.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::{bin_resolver::ResolvedBinary, config::BinaryProvider, crate_resolver::ResolvedCrate};
7
8/// Why a cached binary-resolution entry was discarded because the enabled set of binary providers
9/// changed since the entry was written.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum ProviderChangeReason {
13    /// A provider is now enabled that was not enabled when the cache entry was created, so the
14    /// cached outcome might no longer be correct (this provider was never consulted).
15    RequiredProviderNotEnabled(BinaryProvider),
16    /// The provider that produced the cached binary is no longer enabled, so the binary must not be
17    /// served.
18    SourceProviderDisabled(BinaryProvider),
19}
20
21/// Messages related to prebuilt binary resolution and binary resolution cache operations.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(tag = "event", rename_all = "snake_case")]
24pub enum PrebuiltBinaryMessage {
25    /// Looking up prebuilt binary resolution in cache
26    CacheLookup { krate: ResolvedCrate },
27    /// A positive prebuilt binary cache hit: a previously-resolved binary was found in the cache.
28    PositiveCacheHit {
29        krate: ResolvedCrate,
30        path: PathBuf,
31        provider: BinaryProvider,
32    },
33    /// A negative prebuilt binary cache hit: we previously determined, conclusively, that no
34    /// prebuilt binary is available for this crate.
35    NegativeCacheHit { krate: ResolvedCrate },
36    /// No cached prebuilt binary resolution found
37    CacheMiss { krate: ResolvedCrate },
38    /// A cached prebuilt binary resolution existed but was discarded because the enabled set of
39    /// binary providers changed, so the crate will be re-resolved.
40    CacheInvalidatedByProviderChange {
41        krate: ResolvedCrate,
42        reason: ProviderChangeReason,
43    },
44    /// A cached positive prebuilt binary resolution existed but the binary it points to is no
45    /// longer on disk, so the entry was discarded and the crate will be re-resolved.
46    CacheInvalidatedByMissingBinary { krate: ResolvedCrate, path: PathBuf },
47    /// Checking a specific binary provider for prebuilt binaries
48    CheckingProvider {
49        krate: ResolvedCrate,
50        provider: BinaryProvider,
51    },
52    /// A provider was checked but had no binary available
53    ProviderHasNoBinary {
54        provider: BinaryProvider,
55        reason: String,
56    },
57    /// A provider failed before it could make a conclusive determination.
58    ProviderFailed {
59        provider: BinaryProvider,
60        reason: String,
61    },
62    /// Downloading a prebuilt binary from a provider
63    DownloadingBinary { url: String, provider: BinaryProvider },
64    /// Verifying checksum of downloaded binary
65    VerifyingChecksum { expected: String },
66    /// Checksum verification successful
67    ChecksumVerified,
68    /// Successfully resolved a prebuilt binary
69    Resolved { binary: ResolvedBinary },
70    /// Stored resolved binary information in cache
71    CacheStored { path: PathBuf },
72    /// No prebuilt binary found from any provider
73    NoBinaryFound {
74        krate: ResolvedCrate,
75        reasons: Vec<String>,
76    },
77    /// Prebuilt binary resolution was inconclusive: a transient failure (such as a rate limit or
78    /// network error) prevented at least one provider from providing a definitive answer, so the
79    /// result is not cached and the crate falls back to building from source.
80    ResolutionInconclusive { reason: String },
81    /// Prebuilt binary cannot be used due to build customization
82    DisqualifiedDueToCustomization { reason: String },
83    /// Prebuilt binaries are disabled in config
84    PrebuiltBinariesDisabled,
85}
86
87impl PrebuiltBinaryMessage {
88    pub fn cache_lookup(krate: &ResolvedCrate) -> Self {
89        Self::CacheLookup { krate: krate.clone() }
90    }
91
92    pub fn positive_cache_hit(
93        krate: &ResolvedCrate,
94        path: &std::path::Path,
95        provider: BinaryProvider,
96    ) -> Self {
97        Self::PositiveCacheHit {
98            krate: krate.clone(),
99            path: path.to_path_buf(),
100            provider,
101        }
102    }
103
104    pub fn negative_cache_hit(krate: &ResolvedCrate) -> Self {
105        Self::NegativeCacheHit { krate: krate.clone() }
106    }
107
108    pub fn cache_miss(krate: &ResolvedCrate) -> Self {
109        Self::CacheMiss { krate: krate.clone() }
110    }
111
112    pub fn cache_invalidated_by_provider_change(krate: &ResolvedCrate, reason: ProviderChangeReason) -> Self {
113        Self::CacheInvalidatedByProviderChange {
114            krate: krate.clone(),
115            reason,
116        }
117    }
118
119    pub fn cache_invalidated_by_missing_binary(krate: &ResolvedCrate, path: &std::path::Path) -> Self {
120        Self::CacheInvalidatedByMissingBinary {
121            krate: krate.clone(),
122            path: path.to_path_buf(),
123        }
124    }
125
126    pub fn checking_provider(krate: &ResolvedCrate, provider: BinaryProvider) -> Self {
127        Self::CheckingProvider {
128            krate: krate.clone(),
129            provider,
130        }
131    }
132
133    pub fn provider_has_no_binary(provider: BinaryProvider, reason: impl Into<String>) -> Self {
134        Self::ProviderHasNoBinary {
135            provider,
136            reason: reason.into(),
137        }
138    }
139
140    pub fn provider_failed(provider: BinaryProvider, reason: impl Into<String>) -> Self {
141        Self::ProviderFailed {
142            provider,
143            reason: reason.into(),
144        }
145    }
146
147    pub fn downloading_binary(url: impl Into<String>, provider: BinaryProvider) -> Self {
148        Self::DownloadingBinary {
149            url: url.into(),
150            provider,
151        }
152    }
153
154    pub fn verifying_checksum(expected: impl Into<String>) -> Self {
155        Self::VerifyingChecksum {
156            expected: expected.into(),
157        }
158    }
159
160    pub fn checksum_verified() -> Self {
161        Self::ChecksumVerified
162    }
163
164    pub fn resolved(binary: &ResolvedBinary) -> Self {
165        Self::Resolved {
166            binary: binary.clone(),
167        }
168    }
169
170    pub fn cache_stored(path: &std::path::Path) -> Self {
171        Self::CacheStored {
172            path: path.to_path_buf(),
173        }
174    }
175
176    pub fn no_binary_found(krate: &ResolvedCrate, reasons: Vec<String>) -> Self {
177        Self::NoBinaryFound {
178            krate: krate.clone(),
179            reasons,
180        }
181    }
182
183    pub fn resolution_inconclusive(reason: impl Into<String>) -> Self {
184        Self::ResolutionInconclusive {
185            reason: reason.into(),
186        }
187    }
188
189    pub fn disqualified_due_to_customization(reason: impl Into<String>) -> Self {
190        Self::DisqualifiedDueToCustomization {
191            reason: reason.into(),
192        }
193    }
194
195    pub fn prebuilt_binaries_disabled() -> Self {
196        Self::PrebuiltBinariesDisabled
197    }
198}
199
200impl From<PrebuiltBinaryMessage> for Message {
201    fn from(msg: PrebuiltBinaryMessage) -> Self {
202        Message::PrebuiltBinary(msg)
203    }
204}