Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  diagnostics.rs

  Sprache: Rust
 

//! Types and function used to emit pretty diagnostics for `bindgen`.
//!
//! The entry point of this module is the [`Diagnostic`] type.

use std::fmt::Write;
use std::io::{self, BufRead, BufReader};
use std::{borrow::Cow, fs::File};

use annotate_snippets::{
    display_list::{DisplayList, FormatOptions},
    snippet::{Annotation, Slice as ExtSlice, Snippet},
};

use annotate_snippets::snippet::AnnotationType;

#[derive(Clone, Copy, Debug)]
pub(crateenum Level {
    Error,
    Warn,
    Info,
    Note,
    Help,
}

impl From<Level> for AnnotationType {
    fn from(level: Level) -> Self {
        match level {
            Level::Error => Self::Error,
            Level::Warn => Self::Warning,
            Level::Info => Self::Info,
            Level::Note => Self::Note,
            Level::Help => Self::Help,
        }
    }
}

/// A `bindgen` diagnostic.
#[derive(Default)]
pub(cratestruct Diagnostic<'a> {
    title: Option<(Cow<'a, str>, Level)>,
    slices: Vec<Slice<'a>>,
    footer: Vec<(Cow<'a, str>, Level)>,
}

impl<'a> Diagnostic<'a> {
    /// Add a title to the diagnostic and set its type.
    pub(cratefn with_title(
        &mut self,
        title: impl Into<Cow<'a, str>>,
        level: Level,
    ) -> &mut Self {
        self.title = Some((title.into(), level));
        self
    }

    /// Add a slice of source code to the diagnostic.
    pub(cratefn add_slice(&mut self, 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(cratefn add_annotation(
        &mut self,
        msg: impl Into<Cow<'a, str>>,
        level: Level,
    ) -> &mut Self {
        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(cratefn display(&self) {
        std::thread_local! {
            static INVOKED_BY_BUILD_SCRIPT: bool =  std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
        }

        let mut title = None;
        let mut footer = vec![];
        let mut slices = vec![];
        if let Some((msg, level)) = &self.title {
            title = Some(Annotation {
                id: Some("bindgen"),
                label: Some(msg.as_ref()),
                annotation_type: (*level).into(),
            })
        }

        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,
        });

        for slice in &self.slices {
            if let Some(source) = &slice.source {
                slices.push(ExtSlice {
                    source: source.as_ref(),
                    line_start: slice.line.unwrap_or_default(),
                    origin: slice.filename.as_deref(),
                    annotations: vec![],
                    fold: false,
                })
            }
        }

        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(cratestruct Slice<'a> {
    source: Option<Cow<'a, str>>,
    filename: Option<String>,
    line: Option<usize>,
}

impl<'a> Slice<'a> {
    /// Set the source code.
    pub(cratefn with_source(
        &mut self,
        source: impl Into<Cow<'a, str>>,
    ) -> &mut Self {
        self.source = Some(source.into());
        self
    }

    /// Set the file, line and column.
    pub(cratefn with_location(
        &mut self,
        mut name: String,
        line: usize,
        col: usize,
    ) -> &mut Self {
        write!(name, ":{}:{}", line, col)
            .expect("Writing to a string cannot fail");
        self.filename = Some(name);
        self.line = Some(line);
        self
    }
}

pub(cratefn get_line(
    filename: &str,
    line: usize,
) -> io::Result<Option<String>> {
    let file = BufReader::new(File::open(filename)?);
    if let Some(line) = file.lines().nth(line.wrapping_sub(1)) {
        return line.map(Some);
    }

    Ok(None)
}

Messung V0.5 in Prozent
C=90 H=100 G=95

¤ Dauer der Verarbeitung: 0.10 Sekunden  (vorverarbeitet am  2026-06-19) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

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.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik