ohno_macros 0.4.0

Macros for the ohno crate.
Documentation
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![cfg_attr(all(coverage_nightly, test), feature(coverage_attribute))]
#![cfg_attr(docsrs, feature(doc_cfg))]

//! Macros for the [`ohno`](https://docs.rs/ohno) crate.
//!
//! # Macros
//!
//! - `#[derive(Error)]` - Automatically implement error traits
//! - `#[enrich_err("message")]` - Add error enrichment with file/line information to function errors

#![doc(html_logo_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/ohno_macros/logo.png")]
#![doc(html_favicon_url = "https://media.githubusercontent.com/media/microsoft/oxidizer/refs/heads/main/crates/ohno_macros/favicon.ico")]

use proc_macro::TokenStream;

mod derive_error;
mod enrich_err;
mod error_type_attr;
mod utils;

/// Derive macro for automatically implementing error traits.
///
/// Supports the following attributes:
/// - `#[error]` - Mark the field containing the `OhnoCore`. At most one field may be marked. With
///   no marker the macro looks for a single field whose type is named `OhnoCore`, so a core reached
///   through a type alias or a renamed import has to be marked
/// - `#[display("...")]` - Custom display message with field interpolation. Positional arguments
///   are implicitly scoped to `self`, so fields are referenced by their bare name
///   (`path.display()`, not `self.path.display()`)
/// - `#[no_constructors]` - Disable automatic constructor generation
/// - `#[no_debug]` - Disable automatic Debug trait implementation
/// - `#[from(Type1, Type2, ...)]` - Generate From implementations for specified types
///
/// By default, automatically implements `std::fmt::Debug` unless `#[no_debug]` is specified.
/// This means existing code with manual `#[derive(Debug, Error)]` will have conflicts and
/// should either remove the manual Debug derive or add `#[no_debug]` to preserve the manual implementation.
///
/// See the main `ohno` crate documentation for detailed usage examples.
#[proc_macro_derive(Error, attributes(error, display, no_constructors, no_debug, from))]
#[cfg_attr(test, mutants::skip)]
pub fn derive_error(input: TokenStream) -> TokenStream {
    derive_error::derive_error(input)
}

/// Attribute macro for adding error enrichment with file and line info to function errors.
///
/// See the main `ohno` crate documentation for detailed usage examples.
#[proc_macro_attribute]
#[cfg_attr(test, mutants::skip)]
pub fn enrich_err(args: TokenStream, input: TokenStream) -> TokenStream {
    enrich_err::enrich_err(args, input)
}

/// Attribute macro version of `error_type` that preserves documentation comments.
///
/// This allows using regular Rust doc comments with error types:
///
/// ```ignore
/// /// Documentation for MyError
/// #[ohno::error]
/// struct MyError;
/// ```
///
/// See the main `ohno` crate documentation for detailed usage examples.
#[proc_macro_attribute]
#[cfg_attr(test, mutants::skip)]
pub fn error(args: TokenStream, input: TokenStream) -> TokenStream {
    error_type_attr::error(args, input)
}