Trait Emitter

Source
pub trait Emitter<T>
where T: Clone + PartialEq + Eq + Style + Debug,
{ // Required methods fn format_diagnostic( &mut self, diag: &Diagnostic<T>, ) -> Result<StyledBuffer<T>, ComponentError>; fn emit_diagnostic(&mut self, diag: &Diagnostic<T>) -> Result<()>; // Provided method fn supports_color(&self) -> bool { ... } }
Expand description

trait Emitter for emitting diagnostic.

T: Clone + PartialEq + Eq + Style is responsible for the theme style when diaplaying diagnostic. Builtin DiagnosticStyle provided in ‘compiler_base/error/diagnostic/style.rs’.

To customize your own Emitter, you could do the following steps:

§Examples

  1. Define your Emitter:

// create a new `Emitter`
struct DummyEmitter {
    support_color: bool
}

// `Dummy_Emitter` can use `DiagnosticStyle` or other style user-defined.
impl Emitter<DiagnosticStyle> for DummyEmitter {
    fn supports_color(&self) -> bool {
        // Does `Dummy_Emitter` support color ?
        self.support_color
    }

    fn emit_diagnostic(&mut self, diag: &Diagnostic<DiagnosticStyle>) {
        // Format `Diagnostic` into `String`.
        let styled_string = self.format_diagnostic(diag);
        todo!("displaying the 'styled_string'");
    }

    fn format_diagnostic(&mut self, diag: &Diagnostic<DiagnosticStyle>) -> StyledBuffer<DiagnosticStyle> {
        // Format `Diagnostic` into `String`.
        // This part can format `Diagnostic` into a `String`, but it does not automatically diaplay,
        // and the `String` can be sent to an external port such as RPC.
        let mut sb = StyledBuffer::<DiagnosticStyle>::new();
        diag.format(&mut sb);
        sb
    }
}
  1. Use your Emitter with diagnostic:

// Create a diagnostic for emitting.
let mut diagnostic = Diagnostic::<DiagnosticStyle>::new();

// Create a string component wrapped by `Box<>`.
let msg = Box::new(": this is an error!".to_string());

// Add it to `Diagnostic`.
diagnostic.append_component(msg);

// Create the emitter and emit it.
let mut emitter = DummyEmitter {};
emitter.emit_diagnostic(&diagnostic);

Required Methods§

Source

fn format_diagnostic( &mut self, diag: &Diagnostic<T>, ) -> Result<StyledBuffer<T>, ComponentError>

Format struct Diagnostic into String and render String into StyledString, and save StyledString in StyledBuffer.

Source

fn emit_diagnostic(&mut self, diag: &Diagnostic<T>) -> Result<()>

Emit a structured diagnostic.

Provided Methods§

Source

fn supports_color(&self) -> bool

Checks if we can use colors in the current output stream. false by default.

Implementors§

Source§

impl<'a, T> Emitter<T> for EmitterWriter<'a>
where T: Clone + PartialEq + Eq + Style + Debug,