MCPcopy Index your code
hub / github.com/carllerche/assert-struct / fmt

Method fmt

assert-struct/src/error.rs:317–379  ·  view source on GitHub ↗
(&self, f: &mut fmt::Formatter<'_>)

Source from the content-addressed store, hash-verified

315
316impl fmt::Display for ErrorReport {
317 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
318 if self.errors.is_empty() {
319 return Ok(());
320 }
321
322 let source_content = cached_source(&self.abs_path);
323
324 // Pre-compute labels so their lifetimes outlive the report construction.
325 let labels: Vec<String> = self.errors.iter().map(error_label).collect();
326
327 let renderer = if PLAIN_OUTPUT.with(|c| c.get())
328 || std::env::var_os("NO_COLOR").is_some()
329 || !std::io::IsTerminal::is_terminal(&std::io::stderr())
330 {
331 Renderer::plain()
332 } else {
333 Renderer::styled()
334 };
335
336 if let Some(source) = &source_content {
337 let annotations: Vec<_> = self
338 .errors
339 .iter()
340 .zip(labels.iter())
341 .map(|(error, label)| {
342 let start = byte_offset_of(
343 source,
344 error.error_node.line_start,
345 error.error_node.col_start,
346 );
347 let end =
348 byte_offset_of(source, error.error_node.line_end, error.error_node.col_end)
349 .max(start + 1);
350 AnnotationKind::Primary
351 .span(start..end)
352 .label(label.as_str())
353 })
354 .collect();
355
356 let snippet = Snippet::source(&**source)
357 .line_start(1)
358 .path(&self.rel_path)
359 .annotations(annotations);
360
361 let report = Level::ERROR
362 .primary_title("assert_struct! failed")
363 .element(snippet);
364
365 write!(f, "{}", renderer.render(&[report]))?;
366 } else {
367 // Fallback when the source file cannot be read: show location + description.
368 write!(f, "assert_struct! failed:")?;
369 for (error, label) in self.errors.iter().zip(labels.iter()) {
370 write!(
371 f,
372 "\n --> {}:{}\n {label}",
373 self.rel_path, error.error_node.line_start
374 )?;

Callers

nothing calls this directly

Calls 6

cached_sourceFunction · 0.85
byte_offset_ofFunction · 0.85
is_emptyMethod · 0.80
getMethod · 0.80
as_strMethod · 0.80
spanMethod · 0.45

Tested by

no test coverage detected