Skip to main content

cgx_core/messages/
build.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use super::Message;
6use crate::builder::BuildOptions;
7
8/// Messages related to build operations.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(tag = "event", rename_all = "snake_case")]
11pub enum BuildMessage {
12    Started {
13        options: BuildOptions,
14    },
15    CargoMessage {
16        message: cargo_metadata::Message,
17    },
18    /// Some stderr output directly from `cargo build`.  We do not make assumptions about whether
19    /// or not `cargo build` output is UTF-8 clean or is line-oriented (its progress bar mechanism
20    /// uses `\r` without `\n` for example), so instead we just pass the raw byte chunks.
21    /// In `cgx_main` this output will be rendered to the parent process's stderr
22    CargoStderr {
23        bytes: Vec<u8>,
24    },
25    Completed {
26        binary_path: PathBuf,
27    },
28}
29
30impl BuildMessage {
31    pub fn started(options: &BuildOptions) -> Self {
32        Self::Started {
33            options: options.clone(),
34        }
35    }
36
37    pub fn cargo_message(message: cargo_metadata::Message) -> Self {
38        Self::CargoMessage { message }
39    }
40
41    pub fn cargo_stderr(chunk: Vec<u8>) -> Self {
42        Self::CargoStderr { bytes: chunk }
43    }
44
45    pub fn completed(binary_path: &std::path::Path) -> Self {
46        Self::Completed {
47            binary_path: binary_path.to_path_buf(),
48        }
49    }
50}
51
52impl From<BuildMessage> for Message {
53    fn from(msg: BuildMessage) -> Self {
54        Message::Build(msg)
55    }
56}