Skip to main content

maybe_fatal_derive/
lib.rs

1//! Code generation for the [maybe-fatal] crate.
2//!
3//! See [maybe-fatal]'s documentation for examples of how to use this crate.
4//!
5//! [maybe-fatal]: https://docs.rs/maybe-fatal/latest/maybe_fatal/
6
7extern crate proc_macro;
8
9mod attribute;
10mod diagnose;
11mod diagnostic_group;
12mod diagnostic_info;
13mod partial_diagnose;
14mod utils;
15
16use syn::{DeriveInput, parse_macro_input};
17
18/// Implements [`Diagnose`] for a `struct` with a span and an info field.
19///
20/// [`Diagnose`]: https://docs.rs/maybe-fatal/latest/maybe_fatal/traits/trait.Diagnose.html
21#[proc_macro_derive(Diagnose, attributes(maybe_fatal))]
22pub fn diagnose_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
23    let input = parse_macro_input!(input as DeriveInput);
24    diagnose::parse(input)
25        .unwrap_or_else(syn::Error::into_compile_error)
26        .into()
27}
28
29/// Implements [`PartialDiagnose`] for a `struct` or an `enum` with `struct` variants.
30///
31/// [`PartialDiagnose`]: https://docs.rs/maybe-fatal/latest/maybe_fatal/traits/trait.PartialDiagnose.html
32#[proc_macro_derive(PartialDiagnose, attributes(maybe_fatal))]
33pub fn partial_diagnose_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
34    let input = parse_macro_input!(input as DeriveInput);
35    partial_diagnose::parse(input)
36        .unwrap_or_else(syn::Error::into_compile_error)
37        .into()
38}
39
40/// Implements [`DiagnosticGroup`] for an `enum`.
41///
42/// [`DiagnosticGroup`]: https://docs.rs/maybe-fatal/latest/maybe_fatal/traits/trait.DiagnosticGroup.html
43#[proc_macro_derive(DiagnosticGroup, attributes(maybe_fatal))]
44pub fn diagnostic_group_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
45    let input = parse_macro_input!(input as DeriveInput);
46    diagnostic_group::parse(input)
47        .unwrap_or_else(syn::Error::into_compile_error)
48        .into()
49}
50
51/// Implements [`DiagnosticGroup`] and [`PartialDiagnose`] for an `enum` with newtype variants that
52/// also implement [`DiagnosticGroup`] and [`PartialDiagnose`].
53///
54/// [`DiagnosticGroup`]: https://docs.rs/maybe-fatal/latest/maybe_fatal/traits/trait.DiagnosticGroup.html
55/// [`PartialDiagnose`]: https://docs.rs/maybe-fatal/latest/maybe_fatal/traits/trait.PartialDiagnose.html
56#[proc_macro_derive(DiagnosticInfoWrapper, attributes(maybe_fatal))]
57pub fn diagnostic_info_wrapper_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
58    let input = parse_macro_input!(input as DeriveInput);
59    diagnostic_info::parse(input)
60        .unwrap_or_else(syn::Error::into_compile_error)
61        .into()
62}