impl<'a> Diagnostic<'a> { /// Add a title to the diagnostic and set its type. pub(crate) fn with_title(
&mutself,
title: impl Into<Cow<'a, str>>,
level: Level,
) -> &mutSelf { self.title = Some((title.into(), level)); self
}
/// Add a slice of source code to the diagnostic. pub(crate) fn add_slice(&mutself, slice: Slice<'a>) -> &mut Self { self.slices.push(slice); self
}
/// Add a footer annotation to the diagnostic. This annotation will have its own type. pub(crate) fn add_annotation(
&mutself,
msg: impl Into<Cow<'a, str>>,
level: Level,
) -> &mutSelf { self.footer.push((msg.into(), level)); self
}
/// Print this diagnostic. /// /// The diagnostic is printed using `cargo:warning` if `bindgen` is being invoked by a build /// script or using `eprintln` otherwise. pub(crate) fn display(&self) {
std::thread_local! { static INVOKED_BY_BUILD_SCRIPT: bool = std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
}
for (msg, level) in &self.footer {
footer.push(Annotation {
id: None,
label: Some(msg.as_ref()),
annotation_type: (*level).into(),
});
}
// add additional info that this is generated by bindgen // so as to not confuse with rustc warnings
footer.push(Annotation {
id: None,
label: Some("This diagnostic was generated by bindgen."),
annotation_type: AnnotationType::Info,
});
let snippet = Snippet {
title,
footer,
slices,
opt: FormatOptions {
color: true,
..Default::default()
},
}; let dl = DisplayList::from(snippet);
if INVOKED_BY_BUILD_SCRIPT.with(Clone::clone) { // This is just a hack which hides the `warning:` added by cargo at the beginning of // every line. This should be fine as our diagnostics already have a colorful title. // FIXME (pvdrz): Could it be that this doesn't work in other languages? let hide_warning = "\r \r"; let string = dl.to_string(); for line in string.lines() {
println!("cargo:warning={}{}", hide_warning, line);
}
} else {
eprintln!("{}\n", dl);
}
}
}
/// A slice of source code. #[derive(Default)] pub(crate) struct Slice<'a> {
source: Option<Cow<'a, str>>,
filename: Option<String>,
line: Option<usize>,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.