Skip to main content

csharp_rs_macros/
lib.rs

1// Rust guideline compliant 2026-02-10
2//! Proc macro implementation for the `csharp-rs` crate.
3//!
4//! This crate provides the `#[derive(CSharp)]` macro that generates C# type
5//! definitions from Rust structs and enums. It is not intended to be used
6//! directly; use `csharp-rs` instead, which re-exports the derive macro.
7
8mod attr;
9mod codegen;
10mod config;
11mod types;
12
13use proc_macro::TokenStream;
14
15/// Derives a C# type definition for the annotated Rust type.
16///
17/// Supports `#[csharp(...)]` attributes for customization and respects
18/// `#[serde(...)]` attributes for JSON serialization compatibility.
19///
20/// # Examples
21///
22/// ```ignore
23/// use csharp_rs::CSharp;
24///
25/// #[derive(CSharp)]
26/// #[csharp(export)]
27/// struct Player {
28///     name: String,
29///     score: i32,
30/// }
31/// ```
32#[proc_macro_derive(CSharp, attributes(csharp, serde))]
33pub fn derive_csharp(input: TokenStream) -> TokenStream {
34    let input = syn::parse_macro_input!(input as syn::DeriveInput);
35    match types::process_input(&input) {
36        Ok(derived) => derived.into_token_stream().into(),
37        Err(err) => err.to_compile_error().into(),
38    }
39}