GitXplorerGitXplorer
c

codespan-derive

public
4 stars
3 forks
6 issues

Commits

List of commits on branch trunk.
Unverified
c7e289937aba99a8ab7e2d535dfb357cc127830d

Implement render function attribute

ccompiler-errors committed 3 years ago
Unverified
868f55d6b461e71d91643610b4889fa0e7d73ca0

Fixup for distribution

ccompiler-errors committed 3 years ago
Unverified
2fbaf7a986fc4d496584873194d81c719649fccd

Support types in file_id attr and update README

ppencels committed 4 years ago
Unverified
1c3eef81d59bc865b0acf8c03f835d527cab875b

Add missing argument

ppencels committed 4 years ago
Unverified
1a50c8cdc2792d8b2e9d1def74f87e5b1f739234

Fixes, split Span style into primary/secondary

ccompiler-errors committed 4 years ago
Unverified
7943b22fb15e41b577c4794cb28fbb02eb8da8c2

Clean up

ccompiler-errors committed 4 years ago

README

The README file for this repository.

codespan-derive

Derive macro for ergonomically creating a Diagnostic from an error macro

Usage

  1. Add #[derive(IntoDiagnostic)] onto your error macro type.
  2. Add a #[file_id(Type)] to signal what the FileId generic type of the Diagnostic will be.
  3. Tag every variant with a #[message = ""] signalling what the error message should read.
  4. Span-like values that implement IntoLabel can be tagged with #[primary] or #[secondary] to be marked in the generated error, with an optional message like #[primary = ""].
#[derive(IntoDiagnostic)]
#[file_id(SomeFileIdType)]
enum Error {
  #[message = "Compiler found the number `{0}` is too large"]
  NumberTooLarge(usize),

  #[message = "Cannot parse string {string}"]
  BadString {
    string: String,
    #[primary = "The bad string appears here"]
    span: Span,
  },
}

Then handle it somewhere like:

if let Some(err) = result {
  // IntoDiagnostic derived from macro
  let diagnostic = err.into_diagnostic();

  // Basic codespan-diagnostic printing to terminal
  let writer = StandardStream::stderr(ColorChoice::Always);
  let config = codespan_reporting::term::Config::default();
  term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;
}