cargo_metadata/
messages.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use super::{Diagnostic, PackageId, Target};
use camino::Utf8PathBuf;
#[cfg(feature = "builder")]
use derive_builder::Builder;
use serde::{de, ser, Deserialize, Serialize};
use std::fmt::{self, Write};
use std::io::{self, BufRead, Read};

/// Profile settings used to determine which compiler flags to use for a
/// target.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
pub struct ArtifactProfile {
    /// Optimization level. Possible values are 0-3, s or z.
    pub opt_level: String,
    /// The kind of debug information.
    #[serde(default)]
    pub debuginfo: ArtifactDebuginfo,
    /// State of the `cfg(debug_assertions)` directive, enabling macros like
    /// `debug_assert!`
    pub debug_assertions: bool,
    /// State of the overflow checks.
    pub overflow_checks: bool,
    /// Whether this profile is a test
    pub test: bool,
}

/// The kind of debug information included in the artifact.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ArtifactDebuginfo {
    /// No debug information.
    #[default]
    None,
    /// Line directives only.
    LineDirectivesOnly,
    /// Line tables only.
    LineTablesOnly,
    /// Debug information without type or variable-level information.
    Limited,
    /// Full debug information.
    Full,
    /// An unknown integer level.
    ///
    /// This may be produced by a version of rustc in the future that has
    /// additional levels represented by an integer that are not known by this
    /// version of `cargo_metadata`.
    UnknownInt(i64),
    /// An unknown string level.
    ///
    /// This may be produced by a version of rustc in the future that has
    /// additional levels represented by a string that are not known by this
    /// version of `cargo_metadata`.
    UnknownString(String),
}

impl ser::Serialize for ArtifactDebuginfo {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        match self {
            Self::None => 0.serialize(serializer),
            Self::LineDirectivesOnly => "line-directives-only".serialize(serializer),
            Self::LineTablesOnly => "line-tables-only".serialize(serializer),
            Self::Limited => 1.serialize(serializer),
            Self::Full => 2.serialize(serializer),
            Self::UnknownInt(n) => n.serialize(serializer),
            Self::UnknownString(s) => s.serialize(serializer),
        }
    }
}

impl<'de> de::Deserialize<'de> for ArtifactDebuginfo {
    fn deserialize<D>(d: D) -> Result<ArtifactDebuginfo, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = ArtifactDebuginfo;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("an integer or string")
            }

            fn visit_i64<E>(self, value: i64) -> Result<ArtifactDebuginfo, E>
            where
                E: de::Error,
            {
                let debuginfo = match value {
                    0 => ArtifactDebuginfo::None,
                    1 => ArtifactDebuginfo::Limited,
                    2 => ArtifactDebuginfo::Full,
                    n => ArtifactDebuginfo::UnknownInt(n),
                };
                Ok(debuginfo)
            }

            fn visit_u64<E>(self, value: u64) -> Result<ArtifactDebuginfo, E>
            where
                E: de::Error,
            {
                self.visit_i64(value as i64)
            }

            fn visit_str<E>(self, value: &str) -> Result<ArtifactDebuginfo, E>
            where
                E: de::Error,
            {
                let debuginfo = match value {
                    "none" => ArtifactDebuginfo::None,
                    "limited" => ArtifactDebuginfo::Limited,
                    "full" => ArtifactDebuginfo::Full,
                    "line-directives-only" => ArtifactDebuginfo::LineDirectivesOnly,
                    "line-tables-only" => ArtifactDebuginfo::LineTablesOnly,
                    s => ArtifactDebuginfo::UnknownString(s.to_string()),
                };
                Ok(debuginfo)
            }

            fn visit_unit<E>(self) -> Result<ArtifactDebuginfo, E>
            where
                E: de::Error,
            {
                Ok(ArtifactDebuginfo::None)
            }
        }

        d.deserialize_any(Visitor)
    }
}

impl fmt::Display for ArtifactDebuginfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ArtifactDebuginfo::None => f.write_char('0'),
            ArtifactDebuginfo::Limited => f.write_char('1'),
            ArtifactDebuginfo::Full => f.write_char('2'),
            ArtifactDebuginfo::LineDirectivesOnly => f.write_str("line-directives-only"),
            ArtifactDebuginfo::LineTablesOnly => f.write_str("line-tables-only"),
            ArtifactDebuginfo::UnknownInt(n) => write!(f, "{}", n),
            ArtifactDebuginfo::UnknownString(s) => f.write_str(s),
        }
    }
}

/// A compiler-generated file.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
pub struct Artifact {
    /// The package this artifact belongs to
    pub package_id: PackageId,
    /// Path to the `Cargo.toml` file
    #[serde(default)]
    pub manifest_path: Utf8PathBuf,
    /// The target this artifact was compiled for
    pub target: Target,
    /// The profile this artifact was compiled with
    pub profile: ArtifactProfile,
    /// The enabled features for this artifact
    pub features: Vec<String>,
    /// The full paths to the generated artifacts
    /// (e.g. binary file and separate debug info)
    pub filenames: Vec<Utf8PathBuf>,
    /// Path to the executable file
    pub executable: Option<Utf8PathBuf>,
    /// If true, then the files were already generated
    pub fresh: bool,
}

/// Message left by the compiler
// TODO: Better name. This one comes from machine_message.rs
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
pub struct CompilerMessage {
    /// The package this message belongs to
    pub package_id: PackageId,
    /// The target this message is aimed at
    pub target: Target,
    /// The message the compiler sent.
    pub message: Diagnostic,
}

/// Output of a build script execution.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
pub struct BuildScript {
    /// The package this build script execution belongs to
    pub package_id: PackageId,
    /// The libs to link
    pub linked_libs: Vec<Utf8PathBuf>,
    /// The paths to search when resolving libs
    pub linked_paths: Vec<Utf8PathBuf>,
    /// Various `--cfg` flags to pass to the compiler
    pub cfgs: Vec<String>,
    /// The environment variables to add to the compilation
    pub env: Vec<(String, String)>,
    /// The `OUT_DIR` environment variable where this script places its output
    ///
    /// Added in Rust 1.41.
    #[serde(default)]
    pub out_dir: Utf8PathBuf,
}

/// Final result of a build.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
pub struct BuildFinished {
    /// Whether or not the build finished successfully.
    pub success: bool,
}

/// A cargo message
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[serde(tag = "reason", rename_all = "kebab-case")]
pub enum Message {
    /// The compiler generated an artifact
    CompilerArtifact(Artifact),
    /// The compiler wants to display a message
    CompilerMessage(CompilerMessage),
    /// A build script successfully executed.
    BuildScriptExecuted(BuildScript),
    /// The build has finished.
    ///
    /// This is emitted at the end of the build as the last message.
    /// Added in Rust 1.44.
    BuildFinished(BuildFinished),
    /// A line of text which isn't a cargo or compiler message.
    /// Line separator is not included
    #[serde(skip)]
    TextLine(String),
}

impl Message {
    /// Creates an iterator of Message from a Read outputting a stream of JSON
    /// messages. For usage information, look at the top-level documentation.
    pub fn parse_stream<R: Read>(input: R) -> MessageIter<R> {
        MessageIter { input }
    }
}

impl fmt::Display for CompilerMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

/// An iterator of Messages.
pub struct MessageIter<R> {
    input: R,
}

impl<R: BufRead> Iterator for MessageIter<R> {
    type Item = io::Result<Message>;
    fn next(&mut self) -> Option<Self::Item> {
        let mut line = String::new();
        self.input
            .read_line(&mut line)
            .map(|n| {
                if n == 0 {
                    None
                } else {
                    if line.ends_with('\n') {
                        line.truncate(line.len() - 1);
                    }
                    let mut deserializer = serde_json::Deserializer::from_str(&line);
                    deserializer.disable_recursion_limit();
                    Some(Message::deserialize(&mut deserializer).unwrap_or(Message::TextLine(line)))
                }
            })
            .transpose()
    }
}

/// An iterator of Message.
type MessageIterator<R> =
    serde_json::StreamDeserializer<'static, serde_json::de::IoRead<R>, Message>;

/// Creates an iterator of Message from a Read outputting a stream of JSON
/// messages. For usage information, look at the top-level documentation.
#[deprecated(note = "Use Message::parse_stream instead")]
pub fn parse_messages<R: Read>(input: R) -> MessageIterator<R> {
    serde_json::Deserializer::from_reader(input).into_iter::<Message>()
}