fieldwork-macro 0.1.0

Proc-macro implementation for fieldwork crate
Documentation
//! Proc-macro implementation for the fieldwork crate.
//!
//! This crate provides the implementation of the [`with_!`](with_) macro.
//! Users should depend on the `fieldwork` crate instead of this one directly.

use proc_macro::TokenStream;

mod expand;
mod parse;

/// Field-level borrow splitting macro.
///
/// See the [`fieldwork`](https://docs.rs/fieldwork) crate documentation for
/// detailed usage information.
///
/// # Syntax
///
/// ```text
/// with_!(receiver, {
///     methods {
///         method_name(&field1, mut field2, copy field3, arg: Type) -> ReturnType {
///             // method body
///         }
///     }
///     fields {
///         field_a
///         mut field_b
///     }
///     body {
///         // code that uses receiver.method_name() and receiver.field_x
///     }
/// });
/// ```
///
/// # Field Modes
///
/// - `field` or `&field` — shared reference (`&receiver.field`)
/// - `mut field` — mutable reference (`&mut receiver.field`)
/// - `copy field` — by value, requires `Copy` trait
///
/// # How It Works
///
/// The macro:
/// 1. Extracts fields from the receiver as separate local bindings
/// 2. Replaces `receiver.method()` calls with inlined method bodies
/// 3. Replaces `receiver.field` accesses with the local bindings
///
/// This allows the borrow checker to see that the borrows are disjoint.
#[proc_macro]
pub fn with_(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as parse::WithInput);
    match expand::expand(input) {
        Ok(tokens) => tokens.into(),
        Err(err) => err.to_compile_error().into(),
    }
}