cargo/util/
machine_message.rs
1use std::path::{Path, PathBuf};
2
3use cargo_util_schemas::core::PackageIdSpec;
4use serde::ser;
5use serde::Serialize;
6use serde_json::{json, value::RawValue};
7
8use crate::core::compiler::CompileMode;
9use crate::core::Target;
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#[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#[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}
99
100impl<'a> Message for TimingInfo<'a> {
101 fn reason(&self) -> &str {
102 "timing-info"
103 }
104}
105
106#[derive(Serialize)]
107pub struct BuildFinished {
108 pub success: bool,
109}
110
111impl Message for BuildFinished {
112 fn reason(&self) -> &str {
113 "build-finished"
114 }
115}