1use super::{Diagnostic, PackageId, Target};
2use camino::Utf8PathBuf;
3#[cfg(feature = "builder")]
4use derive_builder::Builder;
5use serde::{de, ser, Deserialize, Serialize};
6use std::fmt::{self, Write};
7use std::io::{self, BufRead, Read};
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "builder", derive(Builder))]
13#[non_exhaustive]
14#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
15pub struct ArtifactProfile {
16 pub opt_level: String,
18 #[serde(default)]
20 pub debuginfo: ArtifactDebuginfo,
21 pub debug_assertions: bool,
24 pub overflow_checks: bool,
26 pub test: bool,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
32#[non_exhaustive]
33pub enum ArtifactDebuginfo {
34 None,
36 LineDirectivesOnly,
38 LineTablesOnly,
40 Limited,
42 Full,
44 UnknownInt(i64),
50 UnknownString(String),
56}
57
58impl Default for ArtifactDebuginfo {
59 fn default() -> Self {
60 ArtifactDebuginfo::None
61 }
62}
63
64impl ser::Serialize for ArtifactDebuginfo {
65 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
66 where
67 S: ser::Serializer,
68 {
69 match self {
70 Self::None => 0.serialize(serializer),
71 Self::LineDirectivesOnly => "line-directives-only".serialize(serializer),
72 Self::LineTablesOnly => "line-tables-only".serialize(serializer),
73 Self::Limited => 1.serialize(serializer),
74 Self::Full => 2.serialize(serializer),
75 Self::UnknownInt(n) => n.serialize(serializer),
76 Self::UnknownString(s) => s.serialize(serializer),
77 }
78 }
79}
80
81impl<'de> de::Deserialize<'de> for ArtifactDebuginfo {
82 fn deserialize<D>(d: D) -> Result<ArtifactDebuginfo, D::Error>
83 where
84 D: de::Deserializer<'de>,
85 {
86 struct Visitor;
87
88 impl<'de> de::Visitor<'de> for Visitor {
89 type Value = ArtifactDebuginfo;
90
91 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
92 formatter.write_str("an integer or string")
93 }
94
95 fn visit_i64<E>(self, value: i64) -> Result<ArtifactDebuginfo, E>
96 where
97 E: de::Error,
98 {
99 let debuginfo = match value {
100 0 => ArtifactDebuginfo::None,
101 1 => ArtifactDebuginfo::Limited,
102 2 => ArtifactDebuginfo::Full,
103 n => ArtifactDebuginfo::UnknownInt(n),
104 };
105 Ok(debuginfo)
106 }
107
108 fn visit_u64<E>(self, value: u64) -> Result<ArtifactDebuginfo, E>
109 where
110 E: de::Error,
111 {
112 self.visit_i64(value as i64)
113 }
114
115 fn visit_str<E>(self, value: &str) -> Result<ArtifactDebuginfo, E>
116 where
117 E: de::Error,
118 {
119 let debuginfo = match value {
120 "none" => ArtifactDebuginfo::None,
121 "limited" => ArtifactDebuginfo::Limited,
122 "full" => ArtifactDebuginfo::Full,
123 "line-directives-only" => ArtifactDebuginfo::LineDirectivesOnly,
124 "line-tables-only" => ArtifactDebuginfo::LineTablesOnly,
125 s => ArtifactDebuginfo::UnknownString(s.to_string()),
126 };
127 Ok(debuginfo)
128 }
129
130 fn visit_unit<E>(self) -> Result<ArtifactDebuginfo, E>
131 where
132 E: de::Error,
133 {
134 Ok(ArtifactDebuginfo::None)
135 }
136 }
137
138 d.deserialize_any(Visitor)
139 }
140}
141
142impl fmt::Display for ArtifactDebuginfo {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 match self {
145 ArtifactDebuginfo::None => f.write_char('0'),
146 ArtifactDebuginfo::Limited => f.write_char('1'),
147 ArtifactDebuginfo::Full => f.write_char('2'),
148 ArtifactDebuginfo::LineDirectivesOnly => f.write_str("line-directives-only"),
149 ArtifactDebuginfo::LineTablesOnly => f.write_str("line-tables-only"),
150 ArtifactDebuginfo::UnknownInt(n) => write!(f, "{}", n),
151 ArtifactDebuginfo::UnknownString(s) => f.write_str(s),
152 }
153 }
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
158#[cfg_attr(feature = "builder", derive(Builder))]
159#[non_exhaustive]
160#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
161pub struct Artifact {
162 pub package_id: PackageId,
164 #[serde(default)]
166 pub manifest_path: Utf8PathBuf,
167 pub target: Target,
169 pub profile: ArtifactProfile,
171 pub features: Vec<String>,
173 pub filenames: Vec<Utf8PathBuf>,
176 pub executable: Option<Utf8PathBuf>,
178 pub fresh: bool,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
185#[cfg_attr(feature = "builder", derive(Builder))]
186#[non_exhaustive]
187#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
188pub struct CompilerMessage {
189 pub package_id: PackageId,
191 pub target: Target,
193 pub message: Diagnostic,
195}
196
197#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
199#[cfg_attr(feature = "builder", derive(Builder))]
200#[non_exhaustive]
201#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
202pub struct BuildScript {
203 pub package_id: PackageId,
205 pub linked_libs: Vec<Utf8PathBuf>,
207 pub linked_paths: Vec<Utf8PathBuf>,
209 pub cfgs: Vec<String>,
211 pub env: Vec<(String, String)>,
213 #[serde(default)]
217 pub out_dir: Utf8PathBuf,
218}
219
220#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
222#[cfg_attr(feature = "builder", derive(Builder))]
223#[non_exhaustive]
224#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
225pub struct BuildFinished {
226 pub success: bool,
228}
229
230#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
232#[non_exhaustive]
233#[serde(tag = "reason", rename_all = "kebab-case")]
234pub enum Message {
235 CompilerArtifact(Artifact),
237 CompilerMessage(CompilerMessage),
239 BuildScriptExecuted(BuildScript),
241 BuildFinished(BuildFinished),
246 #[serde(skip)]
249 TextLine(String),
250}
251
252impl Message {
253 pub fn parse_stream<R: Read>(input: R) -> MessageIter<R> {
256 MessageIter { input }
257 }
258}
259
260impl fmt::Display for CompilerMessage {
261 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262 write!(f, "{}", self.message)
263 }
264}
265
266pub struct MessageIter<R> {
268 input: R,
269}
270
271impl<R: BufRead> Iterator for MessageIter<R> {
272 type Item = io::Result<Message>;
273 fn next(&mut self) -> Option<Self::Item> {
274 let mut line = String::new();
275 self.input
276 .read_line(&mut line)
277 .map(|n| {
278 if n == 0 {
279 None
280 } else {
281 if line.ends_with('\n') {
282 line.truncate(line.len() - 1);
283 }
284 let mut deserializer = serde_json::Deserializer::from_str(&line);
285 deserializer.disable_recursion_limit();
286 Some(Message::deserialize(&mut deserializer).unwrap_or(Message::TextLine(line)))
287 }
288 })
289 .transpose()
290 }
291}
292
293type MessageIterator<R> =
295 serde_json::StreamDeserializer<'static, serde_json::de::IoRead<R>, Message>;
296
297#[deprecated(note = "Use Message::parse_stream instead")]
300pub fn parse_messages<R: Read>(input: R) -> MessageIterator<R> {
301 serde_json::Deserializer::from_reader(input).into_iter::<Message>()
302}