#
cargo/util/
machine_message.rs

    1use std::path::{Path, PathBuf};
    2
    3use cargo_util_schemas::core::PackageIdSpec;
    4use serde::Serialize;
    5use serde::ser;
    6use serde_json::{json, value::RawValue};
    7
    8use crate::core::Target;
    9use crate::core::compiler::{CompilationSection, CompileMode};
    10
    11pub trait Message: ser::Serialize {
    12    fn reason(&self) -> &str;
    13
    14    fn to_json_string(&self) -> String {
    15        let json = serde_json::to_string(self).unwrap();
    16        assert!(json.starts_with("{\""));
    17        let reason = json!(self.reason());
    18        format!("{{\"reason\":{},{}", reason, &json[1..])
    19    }
    20}
    21
    22#[derive(Serialize)]
    23pub struct FromCompiler<'a> {
    24    pub package_id: PackageIdSpec,
    25    pub manifest_path: &'a Path,
    26    pub target: &'a Target,
    27    pub message: Box<RawValue>,
    28}
    29
    30impl<'a> Message for FromCompiler<'a> {
    31    fn reason(&self) -> &str {
    32        "compiler-message"
    33    }
    34}
    35
    36#[derive(Serialize)]
    37pub struct Artifact<'a> {
    38    pub package_id: PackageIdSpec,
    39    pub manifest_path: PathBuf,
    40    pub target: &'a Target,
    41    pub profile: ArtifactProfile,
    42    pub features: Vec<String>,
    43    pub filenames: Vec<PathBuf>,
    44    pub executable: Option<PathBuf>,
    45    pub fresh: bool,
    46}
    47
    48impl<'a> Message for Artifact<'a> {
    49    fn reason(&self) -> &str {
    50        "compiler-artifact"
    51    }
    52}
    53
    54/// This is different from the regular `Profile` to maintain backwards
    55/// compatibility (in particular, `test` is no longer in `Profile`, but we
    56/// still want it to be included here).
    57#[derive(Serialize)]
    58pub struct ArtifactProfile {
    59    pub opt_level: &'static str,
    60    pub debuginfo: Option<ArtifactDebuginfo>,
    61    pub debug_assertions: bool,
    62    pub overflow_checks: bool,
    63    pub test: bool,
    64}
    65
    66/// Internally this is an enum with different variants, but keep using 0/1/2 as integers for compatibility.
    67#[derive(Serialize)]
    68#[serde(untagged)]
    69pub enum ArtifactDebuginfo {
    70    Int(u32),
    71    Named(&'static str),
    72}
    73
    74#[derive(Serialize)]
    75pub struct BuildScript<'a> {
    76    pub package_id: PackageIdSpec,
    77    pub linked_libs: &'a [String],
    78    pub linked_paths: &'a [String],
    79    pub cfgs: &'a [String],
    80    pub env: &'a [(String, String)],
    81    pub out_dir: &'a Path,
    82}
    83
    84impl<'a> Message for BuildScript<'a> {
    85    fn reason(&self) -> &str {
    86        "build-script-executed"
    87    }
    88}
    89
    90#[derive(Serialize)]
    91pub struct TimingInfo<'a> {
    92    pub package_id: PackageIdSpec,
    93    pub target: &'a Target,
    94    pub mode: CompileMode,
    95    pub duration: f64,
    96    #[serde(skip_serializing_if = "Option::is_none")]
    97    pub rmeta_time: Option<f64>,
    98    #[serde(skip_serializing_if = "Vec::is_empty")]
    99    pub sections: Vec<(String, CompilationSection)>,
    100}
    101
    102impl<'a> Message for TimingInfo<'a> {
    103    fn reason(&self) -> &str {
    104        "timing-info"
    105    }
    106}
    107
    108#[derive(Serialize)]
    109pub struct BuildFinished {
    110    pub success: bool,
    111}
    112
    113impl Message for BuildFinished {
    114    fn reason(&self) -> &str {
    115        "build-finished"
    116    }
    117}