dfir_lang/
pretty_span.rs

1//! Pretty, human-readable printing of [`proc_macro2::Span`]s.
2
3extern crate proc_macro;
4
5/// Helper struct which displays the span as `path:row:col` for human reading/IDE linking.
6/// Example: `dfir\tests\surface_syntax.rs:42:18`.
7pub struct PrettySpan(pub proc_macro2::Span);
8impl std::fmt::Display for PrettySpan {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        #[cfg(nightly)]
11        if proc_macro::is_available() {
12            let span = self.0.unwrap();
13            write!(
14                f,
15                "{}:{}:{}",
16                span.source_file().path().display(),
17                span.start().line(),
18                span.start().column(),
19            )?;
20            return Ok(());
21        }
22
23        write!(
24            f,
25            "unknown:{}:{}",
26            self.0.start().line,
27            self.0.start().column
28        )
29    }
30}
31
32/// Helper struct which displays the span as `row:col` for human reading.
33pub struct PrettyRowCol(pub proc_macro2::Span);
34impl std::fmt::Display for PrettyRowCol {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        let span = self.0;
37        write!(f, "{}:{}", span.start().line, span.start().column)
38    }
39}