> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apich.org/llms.txt
> Use this file to discover all available pages before exploring further.

# virtue

> A sinless, zero-required-dependency derive macro helper for Rust (published as virtue-next).

**virtue** (`virtue-next`) is a lightweight library for writing Rust procedural `#[proc_macro_derive]` macros declaratively, without pulling in heavy parser dependencies.

> [!NOTE]
>
> For full builder methods and trait documentation, visit [docs.rs/virtue-next](https://docs.rs/virtue-next).

> [card] [Full API Reference](https://docs.rs/virtue-next)
>
> Generated rustdoc for `virtue::prelude`, `Parse`, and `Generator`.

> [card] [Project Homepage](https://apich.org/projects/virtue/)
>
> Overview, API summary, and organization funding on apich.org.

> [card] [GitHub Repository](https://github.com/Apich-Organization/virtue)
>
> Source code and derive integration test workspace.

## Why Apich Maintains `virtue-next`

`bincode-next`'s derive crate (`bincode_derive-next`) and other derive-heavy crates in the Apich ecosystem rely on `virtue` to keep macro compile times near zero. There are **zero required dependencies** (`proc-macro2` is strictly optional behind a feature flag), and minor releases preserve strict semver stability.

```mermaid theme={null}
flowchart LR
    Input["proc_macro::TokenStream"] --> Parse["Parse::new (src/parse/)"]
    Parse -->|"Generator + Body (Struct / Enum)"| Gen["Generator::impl_for (src/generate/)"]
    Gen --> Fn["FnBuilder + StreamBuilder"]
    Fn -->|"Ok -> TokenStream / Err -> compile_error!"| Output["Generated impl Trait for Type"]
```

## Repository Structure

| Path | Role |
| - | - |
| `src/parse/` | Token-stream parser: `data_type.rs`, `body.rs` (structs & enums), `attributes.rs`, `generics.rs`, `visibility.rs` |
| `src/generate/` | Typed code builders: `generator.rs`, `impl.rs`, `impl_for.rs`, `gen_struct.rs`, `gen_enum.rs`, `generate_item.rs`, `generate_mod.rs`, `stream_builder.rs` |
| `src/error.rs` & `src/utils.rs` | Spanned `Error` type converting directly to `compile_error!` token streams |
| `test/` & `test/derive/` | End-to-end proc-macro integration test workspace exercising structs, enums, generics, lifetimes, and attributes |

## Core API Surface

| Type / Feature | Role | Notes |
| - | - | - |
| `Parse::new` | Parses struct or enum `TokenStream` | Extracts generics, lifetimes, visibility, and helper attributes |
| `Generator::impl_for` | Starts an `impl Trait for Type` block | Copies generic parameters and `where`-clauses automatically |
| `FnBuilder` | Declares `fn` signature, args, and body | Supports `self`, `&self`, `&mut self`, and custom receivers |
| `StreamBuilder` | Typed token stream accumulator | Replaces `quote!` templates with checked builder calls |
| `Error` | Spanned macro diagnostic | Converts directly to `compile_error!` via `into_token_stream()` |
| `feature: proc-macro2` | Optional `proc-macro2` bridge | Disabled by default for zero required dependencies |

## Quick Example

```rust theme={null}
use virtue::prelude::*;

#[proc_macro_derive(YourDerive, attributes(some))]
pub fn derive_your_derive(input: TokenStream) -> TokenStream {
    derive_inner(input).unwrap_or_else(|e| e.into_token_stream())
}

fn derive_inner(input: TokenStream) -> Result<TokenStream> {
    let parse = Parse::new(input)?;
    let (mut generator, body) = parse.into_generator();
    if let Body::Struct(_s) = body {
        generator
            .impl_for("YourTrait")?
            .generate_fn("your_fn")
            .with_self_arg(FnSelfArg::RefSelf)
            .body(|fn_body| {
                fn_body.push_parsed("println!(\"Hello world\");")?;
                Ok(())
            })?;
    }
    generator.finish()
}
```


## Related topics

- [Stack Architecture & Project Map](https://docs.apich.org/guides/architecture.md)
- [Apich Organization Documentation](https://docs.apich.org/index.md)
- [Community, Governance & Acknowledgments](https://docs.apich.org/guides/community-and-support.md)
- [Quickstart](https://docs.apich.org/quickstart.md)
- [bincode-next](https://docs.apich.org/projects/bincode-next.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.