use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::Message;
use crate::{
bin_resolver::ResolvedBinary,
builder::{BuildOptions, BuildTarget},
config::BinaryProvider,
crate_resolver::ResolvedCrate,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum CgxMessage {
CratePlan {
resolved: ResolvedCrate,
crate_path: PathBuf,
options: BuildOptions,
target_platform: String,
},
CrateProvenance {
resolved: ResolvedCrate,
crate_path: PathBuf,
options: BuildOptions,
target_platform: String,
provenance: Provenance,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Provenance {
BuiltFromSource {
binary_path: PathBuf,
target_binary: BuildTarget,
},
Prebuilt {
provider: BinaryProvider,
binary_path: PathBuf,
},
}
impl CgxMessage {
pub fn crate_plan(resolved: &ResolvedCrate, crate_path: &Path, options: &BuildOptions) -> Self {
Self::CratePlan {
resolved: resolved.clone(),
crate_path: crate_path.to_path_buf(),
target_platform: options.target_platform().to_string(),
options: options.clone(),
}
}
pub fn crate_provenance_built_from_source(
resolved: &ResolvedCrate,
crate_path: &Path,
options: &BuildOptions,
binary_path: &Path,
target_binary: BuildTarget,
) -> Self {
Self::CrateProvenance {
resolved: resolved.clone(),
crate_path: crate_path.to_path_buf(),
target_platform: options.target_platform().to_string(),
options: options.clone(),
provenance: Provenance::BuiltFromSource {
binary_path: binary_path.to_path_buf(),
target_binary,
},
}
}
pub fn crate_provenance_prebuilt(
resolved: &ResolvedCrate,
crate_path: &Path,
options: &BuildOptions,
binary: &ResolvedBinary,
) -> Self {
Self::CrateProvenance {
resolved: resolved.clone(),
crate_path: crate_path.to_path_buf(),
target_platform: binary.target.clone(),
options: options.clone(),
provenance: Provenance::Prebuilt {
provider: binary.provider,
binary_path: binary.path.clone(),
},
}
}
}
impl From<CgxMessage> for Message {
fn from(msg: CgxMessage) -> Self {
Message::Cgx(msg)
}
}