Module pretty

Source
Expand description

Pretty-printing diagnostic messages containing references to source code

This module defines some data types for constructing intermediate data structures for printing diagnostic messages referencing source code fragments. When you have an Error, you can convert it to a Message. Then, you can in turn convert it into annotate_snippets::Snippet, for example, and finally format a printable diagnostic message string.

When the yash_syntax crate is built with the annotate-snippets feature enabled, it supports conversion from Message to Snippet. If you would like to use another formatter instead, you can provide your own conversion for yourself.

§Printing an error

This example shows how to format an Error instance into a human-readable string.

let error = Error {
    cause: ErrorCause::Syntax(SyntaxError::EmptyParam),
    location: Location::dummy(""),
};
let message = Message::from(&error);
// The lines below require the `annotate-snippets` feature.
let message = annotate_snippets::Message::from(&message);
eprint!("{}", annotate_snippets::Renderer::plain().render(message));

You can also implement conversion from your custom error object to a Message, which then can be used in the same way to format a diagnostic message. To do this, you can either directly implement From<YourError> for Message, or implement MessageBase for YourError thereby deriving From<&YourError> for Message.

Structs§

Annotation
Source code fragment annotated with a label
Footer
Additional text without associated source code
Message
Entire diagnostic message

Enums§

AnnotationType
Type of annotation.

Traits§

MessageBase
Helper for constructing a Message