Skip to main content

cgx_core/messages/
build_cache.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::{builder::BuildOptions, crate_resolver::ResolvedCrate};
7
8/// Messages related to build artifact cache operations.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(tag = "event", rename_all = "snake_case")]
11pub enum BuildCacheMessage {
12    CacheLookup {
13        name: String,
14        version: String,
15        options: BuildOptions,
16    },
17    CacheHit {
18        binary_path: PathBuf,
19        sbom_path: PathBuf,
20    },
21    CacheMiss {
22        name: String,
23        version: String,
24    },
25    CacheStored {
26        binary_path: PathBuf,
27        sbom_path: PathBuf,
28    },
29    SkippingCacheLocalDir,
30}
31
32impl BuildCacheMessage {
33    pub fn cache_lookup(krate: &ResolvedCrate, options: &BuildOptions) -> Self {
34        Self::CacheLookup {
35            name: krate.name.clone(),
36            version: krate.version.to_string(),
37            options: options.clone(),
38        }
39    }
40
41    pub fn cache_hit(binary_path: &std::path::Path, sbom_path: &std::path::Path) -> Self {
42        Self::CacheHit {
43            binary_path: binary_path.to_path_buf(),
44            sbom_path: sbom_path.to_path_buf(),
45        }
46    }
47
48    pub fn cache_miss(krate: &ResolvedCrate) -> Self {
49        Self::CacheMiss {
50            name: krate.name.clone(),
51            version: krate.version.to_string(),
52        }
53    }
54
55    pub fn cache_stored(binary_path: &std::path::Path, sbom_path: &std::path::Path) -> Self {
56        Self::CacheStored {
57            binary_path: binary_path.to_path_buf(),
58            sbom_path: sbom_path.to_path_buf(),
59        }
60    }
61
62    pub fn skipping_cache_local_dir() -> Self {
63        Self::SkippingCacheLocalDir
64    }
65}
66
67impl From<BuildCacheMessage> for Message {
68    fn from(msg: BuildCacheMessage) -> Self {
69        Message::BuildCache(msg)
70    }
71}