1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::pedantic, missing_docs)]
#![allow(clippy::module_name_repetitions)]
//! Proc **m**acro **anyhow**, a combination of ideas from
//! [`anyhow`](docs.rs/anyhow) and
//! [`proc-macro-error`](docs.rs/proc-macro-error) to improve proc macro
//! development, especially focused on the error handling.
//!
//! # Motivation
//! Error handling in proc-macros is unideal, as the top level functions of proc
//! macros can only return `TokenStreams` both in success and failure case. This
//! means that I often write code like this, moving the actual impelemtation in
//! a seperate function to be able to use the ergonomic rust error handling with
//! e.g. `?`.
//! ```
//! # use proc_macro2::TokenStream;
//! # use quote::quote;
//! # use syn2 as syn;
//! use proc_macro2::TokenStream as TokenStream2;
//!
//! # let _ = quote!{
//! #[proc_macro]
//! # };
//! pub fn my_macro(input: TokenStream) -> TokenStream {
//! match actual_implementation(input.into()) {
//! Ok(output) => output,
//! Err(error) => error.into_compile_error(),
//! }
//! .into()
//! }
//!
//! fn actual_implementation(input: TokenStream2) -> syn::Result<TokenStream2> {
//! // ..
//! # Ok(quote!())
//! }
//! ```
//!
//! # Using the `#[manyhow]` macro
//! To activate the error hadling, just add [`#[manyhow]`](manyhow) above any
//! proc macro implementation, reducing the above example to:
//!
//! ```
//! # use quote::quote;
//! # use syn2 as syn;
//! use manyhow::manyhow;
//! use proc_macro2::TokenStream as TokenStream2;
//!
//! # let _ = quote!{
//! #[manyhow]
//! #[proc_macro]
//! # };
//! fn my_macro(input: TokenStream2) -> syn::Result<TokenStream2> {
//! // ..
//! # Ok(quote!())
//! }
//! ```
//!
//! See [Without macros](#without-macros) to see what this expands to under the
//! hood.
//!
//! A proc macro function marked as `#[manyhow]` can take and return any
//! [`TokenStream`](AnyTokenStream), and can also return `Result<TokenStream,
//! E>` where `E` implments [`ToTokensError`]. As additional paramters a
//! [dummy](#dummy-mut-tokenstream) and/or [emitter](#emitter-mut-emitter) can
//! be specified.
//!
//! The `manyhow` attribute takes one optional flag when used for `proc_macro`
//! and `proc_macro_attribute`. `#[manyhow(input_as_dummy)]` will take the input
//! of a function like `proc_macro` to initialize the [dummy `&mut
//! TokenStream`](#dummy-mut-tokenstream) while `#[manyhow(item_as_dummy)]` on
//! `proc_macro_attribute` will initialize the dummy with the annotated item.
//!
//! # Without macros
//! `manyhow` can be used without proc macros, and they can be disabled by
//! adding `manyhow` with `default-features=false`.
//!
//! The usage is more or less the same, though with some added boilerplate from
//! needing to invoke one of [`function`], [`attribute`] or [`derive`](derive())
//! directly.
//!
//! While the examples use closures, functions can be passed in as well. The
//! above example would then change to:
//! ```
//! # use proc_macro2::TokenStream;
//! # use quote::quote;
//! # use syn2 as syn;
//! use proc_macro2::TokenStream as TokenStream2;
//!
//! # let _ = quote!{
//! #[proc_macro]
//! # };
//! pub fn my_macro(input: TokenStream) -> TokenStream {
//! manyhow::function(
//! input,
//! false,
//! |input: TokenStream2| -> syn::Result<TokenStream2> {
//! // ..
//! # Ok(quote!())
//! },
//! )
//! }
//! ```
//! [`Emitter`](#emitter-mut-emitter) and [dummy
//! `TokenStream`](#dummy-mut-tokenstream) can also be used. [`function`] and
//! [`attribute`] take an additional boolean parameter controlling whether the
//! input/item will be used as initial dummy.
//!
//! # `emitter: &mut Emitter`
//! [`MacroHandler`]s (the trait defining what closures/functions can be used
//! with `manyhow`) can take a mutable reference to an [`Emitter`]. This
//! allows to collect errors, but not fail imidiatly.
//!
//! [`Emitter::fail_if_dirty`] can be used to return if an [`Emitter`] contains
//! any values.
//!
//! ```
//! # use quote::quote;
//! # use syn2 as syn;
//! use manyhow::{manyhow, Emitter, ErrorMessage};
//! use proc_macro2::TokenStream as TokenStream2;
//!
//! # let _ = quote!{
//! #[manyhow]
//! #[proc_macro]
//! # };
//! fn my_macro(input: TokenStream2, emitter: &mut Emitter) -> manyhow::Result<TokenStream2> {
//! // ..
//! emitter.emit(ErrorMessage::call_site("A fun error!"));
//! emitter.fail_if_dirty()?;
//! // ..
//! # Ok(quote!())
//! }
//! ```
//!
//! # `dummy: &mut TokenStream`
//! [`MacroHandler`]s also take a mutable reference to a `TokenStream`, to
//! enable emitting some dummy code to be used in case the macro errors.
//!
//! This allows either appending tokens e.g. with [`ToTokens::to_tokens`] or
//! directly setting the dummy code e.g. `*dummy = quote!{some tokens}`.
use std::convert::Infallible;
pub use macros::manyhow;
use proc_macro2::TokenStream;
#[cfg(doc)]
use quote::ToTokens;
extern crate proc_macro;
#[macro_use]
mod span_ranged;
pub use span_ranged::{to_tokens_span_range, SpanRanged};
#[macro_use]
mod macro_rules;
mod error;
pub use error::*;
#[doc(hidden)]
pub mod __private {
pub use crate::span_ranged::{SpanRangedToSpanRange, ToTokensToSpanRange};
}
/// Marker trait for [`proc_macro::TokenStream`] and
/// [`proc_macro2::TokenStream`]
pub trait AnyTokenStream: Clone + From<TokenStream> + Into<TokenStream> {}
impl AnyTokenStream for TokenStream {}
impl AnyTokenStream for proc_macro::TokenStream {}
/// Handles [`proc_macro_attribute`](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
/// implementation
///
/// Takes any `TokenStream` for `input` and `item` and returns any
/// `TokenStream`. If `item_as_dummy = true` the item input will be used as
/// default dummy code on error. `body` takes a [`MacroHandler`] with two
/// `TokenStream` parameters. And an optional [`&mut Emitter`](Emitter) and a
/// `&mut TokenStream` for storing a dummy output.
///
/// ```
/// # use proc_macro_utils::assert_tokens;
/// # use quote::{quote, ToTokens};
/// use manyhow::{attribute, Emitter, Result};
/// use proc_macro2::TokenStream;
/// # let input = quote!();
/// # let item = quote!();
/// # let output: TokenStream =
/// attribute(
/// input,
/// item,
/// false,
/// |input: TokenStream,
/// item: TokenStream,
/// dummy: &mut TokenStream,
/// emitter: &mut Emitter|
/// -> Result {
/// // ..
/// # Ok(quote!())
/// },
/// );
/// ```
///
/// *Note:* When `item_as_dummy = true` the `dummy: &mut TokenStream` will be
/// initialized with `item`. To override assign a new `TokenStream`:
/// ```
/// # use proc_macro_utils::assert_tokens;
/// use manyhow::{attribute, Result, SilentError};
/// use proc_macro2::TokenStream;
/// use quote::{quote, ToTokens};
/// # let input = quote!(input);
/// let item = quote!(
/// struct Struct;
/// );
/// let output: TokenStream = attribute(
/// input,
/// item,
/// true,
/// |input: TokenStream,
/// item: TokenStream,
/// dummy: &mut TokenStream|
/// -> Result<TokenStream, SilentError> {
/// assert_tokens!(dummy.to_token_stream(), {
/// struct Struct;
/// });
/// *dummy = quote! {
/// struct Struct(HelloWorld);
/// };
/// // ..
/// Err(SilentError)
/// },
/// );
///
/// assert_tokens! {output, {struct Struct(HelloWorld);}};
/// ```
pub fn attribute<
Input: AnyTokenStream,
Item: AnyTokenStream,
Output: AnyTokenStream,
Return: AnyTokenStream,
Error: ToTokensError,
Function,
>(
input: impl AnyTokenStream,
item: impl AnyTokenStream,
item_as_dummy: bool,
body: impl MacroHandler<(Input, Item), Output, Function, Error>,
) -> Return {
let mut tokens: Output = if item_as_dummy {
item.clone().into().into()
} else {
TokenStream::default().into()
};
let mut emitter = Emitter::new();
let output = body.call(
(input.into().into(), item.into().into()),
&mut tokens,
&mut emitter,
);
let mut tokens = tokens.into();
let mut tokens = match output {
Ok(tokens) => tokens.into(),
Err(error) => {
error.to_tokens(&mut tokens);
tokens
}
};
emitter.to_tokens(&mut tokens);
tokens.into()
}
/// Handles [`proc_macro_derive`](https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros)
/// implementation
///
/// Takes any `TokenStream` for `item` and returns any `TokenStream`. `body`
/// takes a [`MacroHandler`] with one `TokenStream` parameter. And an optional
/// [`&mut Emitter`](Emitter) and `&mut TokenStream` for storing a dummy
/// output.
///
/// ```
/// # use proc_macro_utils::assert_tokens;
/// # use quote::{quote, ToTokens};
/// use manyhow::{derive, Emitter, Result};
/// use proc_macro2::TokenStream;
/// # let item = quote!();
/// # let output: TokenStream =
/// derive(
/// item,
/// |item: TokenStream, dummy: &mut TokenStream, emitter: &mut Emitter| -> Result {
/// // ..
/// # Ok(quote!())
/// },
/// );
/// ```
pub fn derive<
Item: AnyTokenStream,
Output: AnyTokenStream,
Return: AnyTokenStream,
Error: ToTokensError,
Function,
>(
item: impl AnyTokenStream,
body: impl MacroHandler<(Item,), Output, Function, Error>,
) -> Return {
let mut tokens = TokenStream::default().into();
let mut emitter = Emitter::new();
let output = body.call((item.into().into(),), &mut tokens, &mut emitter);
let mut tokens = tokens.into();
let mut tokens = match output {
Ok(tokens) => tokens.into(),
Err(error) => {
error.to_tokens(&mut tokens);
tokens
}
};
emitter.to_tokens(&mut tokens);
tokens.into()
}
/// Handles function like [`proc_macro`](https://doc.rust-lang.org/reference/procedural-macros.html#function-like-procedural-macros)
/// implementation
///
/// Takes any `TokenStream` for `input` and returns any
/// `TokenStream`. If `input_as_dummy = true` the item input will be used as
/// default dummy code on error. `body` takes a [`MacroHandler`] with one
/// `TokenStream` parameter. And an optional [`&mut Emitter`](Emitter) and a
/// `&mut TokenStream` for storing a dummy output.
///
/// ```
/// # use proc_macro_utils::assert_tokens;
/// # use quote::{quote, ToTokens};
/// use manyhow::{function, Emitter, Result};
/// use proc_macro2::TokenStream;
/// # let input = quote!();
/// # let output: TokenStream =
/// function(
/// input,
/// false,
/// |input: TokenStream, dummy: &mut TokenStream, emitter: &mut Emitter| -> Result {
/// // ..
/// # Ok(quote!())
/// },
/// );
/// ```
///
/// *Note:* When `input_as_dummy = true` the `dummy: &mut TokenStream` will be
/// initialized with `input`. To override assign a new `TokenStream`:
/// ```
/// # use proc_macro_utils::assert_tokens;
/// use manyhow::{function, Result, SilentError};
/// use proc_macro2::TokenStream;
/// use quote::{quote, ToTokens};
/// let input = quote!(some input);
/// let output: TokenStream = function(
/// input,
/// true,
/// |input: TokenStream,
/// dummy: &mut TokenStream|
/// -> Result<TokenStream, SilentError> {
/// assert_tokens!(dummy.to_token_stream(), {
/// some input
/// });
/// *dummy = quote! {
/// another input
/// };
/// // ..
/// Err(SilentError)
/// },
/// );
///
/// assert_tokens! {output, {another input}};
/// ```
pub fn function<
Input: AnyTokenStream,
Output: AnyTokenStream,
Return: AnyTokenStream,
Error: ToTokensError,
Function,
>(
input: impl AnyTokenStream,
input_as_dummy: bool,
body: impl MacroHandler<(Input,), Output, Function, Error>,
) -> Return {
let mut tokens: Output = if input_as_dummy {
input.clone().into().into()
} else {
TokenStream::default().into()
};
let mut emitter = Emitter::new();
let output = body.call((input.into().into(),), &mut tokens, &mut emitter);
let mut tokens = tokens.into();
let mut tokens = match output {
Ok(tokens) => tokens.into(),
Err(error) => {
error.to_tokens(&mut tokens);
tokens
}
};
emitter.to_tokens(&mut tokens);
tokens.into()
}
/// Implementation of a proc-macro
///
/// Note: for `TokenStream` either [`proc_macro::TokenStream`] or
/// [`proc_macro2::TokenStream`] can be used.
///
/// Trait is implemented for any [`function`](FnOnce), taking in either one (for
/// derive or function like macros) or two (for attribute macros) `TokenStream`s
/// and returns either a `TokenStream` or a [`Result<TokenStream, impl
/// ToTokensError>`](ToTokensError).
///
/// Additionally they can take optionally in any order a [`&mut
/// Emitter`](Emitter) which allows emitting errors without returning early. And
/// a `&mut TokenStream` to return a dummy `TokenStream` on failure, note that
/// this `TokenStream` must be the same type as the one returned.
pub trait MacroHandler<Input, Output, Function, Error = Infallible> {
#[allow(clippy::missing_errors_doc, missing_docs)]
fn call(self, input: Input, dummy: &mut Output, emitter: &mut Emitter)
-> Result<Output, Error>;
}
macro_rules! impl_attribute_macro {
($dummy:ident, $emitter:ident =>
$(<$($Inputs:ident),+>(($($in_id:ident),+):($($in_ty:ty),+)$(, $ident:ident:$ty:ty)*);)+) => {
$(
impl<$($Inputs,)+ Output: AnyTokenStream, F> MacroHandler<($($Inputs,)+), Output, ($($in_ty,)* $($ty,)* Output)> for F
where
F: FnOnce($($in_ty,)+ $($ty,)*) -> Output
{
#[allow(unused)]
fn call(self, ($($in_id,)+): ($($in_ty,)+), $dummy: &mut Output, $emitter: &mut Emitter) -> Result<Output, Infallible> {
Ok(self($($in_id,)+ $($ident),*))
}
}
impl<$($Inputs,)+ Output: AnyTokenStream, F, Error> MacroHandler<($($Inputs,)+), Output, ($($in_ty,)* $($ty,)* Result<Output, Error>), Error> for F
where
F: FnOnce($($in_ty,)+ $($ty,)*) -> Result<Output, Error>
{
#[allow(unused)]
fn call(self, ($($in_id,)+): ($($in_ty,)+), $dummy: &mut Output, $emitter: &mut Emitter) -> Result<Output, Error> {
self($($in_id,)+ $($ident),*)
}
}
)*
};
}
impl_attribute_macro! {
dummy, emitter =>
<Input, Item> ((input, item): (Input, Item), dummy: &mut Output);
<Input, Item> ((input, item): (Input, Item));
<Input, Item> ((input, item): (Input, Item), dummy: &mut Output, emitter: &mut Emitter);
<Input, Item> ((input, item): (Input, Item), emitter: &mut Emitter);
<Input, Item> ((input, item): (Input, Item), emitter: &mut Emitter, dummy: &mut Output);
<Input> ((input): (Input), dummy: &mut Output);
<Input> ((input): (Input));
<Input> ((input): (Input), dummy: &mut Output, emitter: &mut Emitter);
<Input> ((input): (Input), emitter: &mut Emitter);
<Input> ((input): (Input), emitter: &mut Emitter, dummy: &mut Output);
}