Skip to main content

FormatRustcStyle

Trait FormatRustcStyle 

Source
pub trait FormatRustcStyle {
    // Required method
    fn format_rustc_style(
        &self,
        source_code: &str,
        filename: &str,
        start_line: u32,
        renderer: &Renderer,
    ) -> String;
}
Expand description

Extension trait that adds rustc-style formatting to anyhow::Error.

Import this trait to call .format_rustc_style(...) directly on an anyhow::Error. If the error carries a SpanContext (added during op execution with the span-diagnostics feature), the output includes a source-location annotation. Otherwise it falls back to self.to_string().

§Examples

use annotate_snippets::Renderer;
use cel_parser::FormatRustcStyle;

let err = anyhow::anyhow!("something went wrong");
let output = err.format_rustc_style("1 + 2", "example.cel", 1, &Renderer::plain());
assert_eq!(output, "something went wrong");

Required Methods§

Source

fn format_rustc_style( &self, source_code: &str, filename: &str, start_line: u32, renderer: &Renderer, ) -> String

Formats in rustc diagnostic style.

If the error carries a SpanContext, produces a multi-line caret diagnostic. Uses the root failure as the diagnostic title, regardless of textual context wrappers. Otherwise returns self.to_string().

  • Complexity: O(c + s) traversal work for c error-chain links and s source bytes, plus error-display and renderer-layout costs; O(c + s + b) auxiliary/output space for b output bytes. Unlocated errors do not inspect the source.
§Examples
use annotate_snippets::Renderer;
use cel_parser::{FormatRustcStyle, SourceSpan, SpanContext};
let error = anyhow::anyhow!("division by zero")
    .context(SpanContext::new(SourceSpan::new(1, 0, 1, 1)));
let text = error.format_rustc_style("1", "input.cel", 1, &Renderer::plain());
assert!(text.contains("error: division by zero"));
assert!(text.contains("input.cel:1:1"));

See the FormatRustcStyle example for the unlocated fallback.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FormatRustcStyle for Error

Source§

fn format_rustc_style( &self, source_code: &str, filename: &str, start_line: u32, renderer: &Renderer, ) -> String

Renders the located root failure, or the outer display text when no location exists.

Implementors§