Skip to main content

polars_structpath_derive/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod enumpath;
4mod structpath;
5
6use proc_macro::TokenStream;
7use syn::{parse_macro_input, DeriveInput};
8
9use enumpath::derive_enum_path_impl;
10use structpath::derive_struct_path_impl;
11
12/// Derive macro for generating Arrow buffer implementations for structs.
13///
14/// This macro automatically generates code that calls `impl_struct_buffer!` from
15/// `polars-structpath`, which creates:
16///
17/// - A buffer struct (e.g., `UserBuffer`) implementing `ArrowBuffer`
18/// - `IntoArrow` implementation for the struct
19/// - `FromArrow` implementation for the struct
20///
21/// # Requirements
22///
23/// - The struct must have named fields (not tuple structs or unit structs)
24/// - All field types must implement `IntoArrow` and `FromArrow` from `polars-structpath`
25///
26/// # Attributes
27///
28/// - `#[type_hint(...)]`: Currently accepted but not used. Reserved for future use.
29///
30/// # Example
31///
32/// ```ignore
33/// use polars_structpath::StructPath;
34/// use polars_structpath::{IntoArrow, ArrowBuffer};
35///
36/// #[derive(StructPath)]
37/// struct User {
38///     name: String,
39///     age: i64,
40/// }
41///
42/// // Now User implements IntoArrow and FromArrow
43/// let mut buffer = User::new_buffer(1);
44/// buffer.push(User {
45///     name: "Alice".to_string(),
46///     age: 30,
47/// });
48/// let array = buffer.to_arrow().unwrap();
49/// ```
50#[proc_macro_derive(StructPath, attributes(type_hint))]
51pub fn derive_struct_path(input: TokenStream) -> TokenStream {
52    let input = parse_macro_input!(input as DeriveInput);
53    derive_struct_path_impl(input).into()
54}
55
56/// Derive macro for generating Arrow buffer implementations for enums.
57///
58/// This macro automatically generates code that calls `impl_enum_buffer!` from
59/// `polars-structpath`, which creates:
60///
61/// - A buffer struct (e.g., `StatusBuffer`) implementing `ArrowBuffer`
62/// - Helper methods for index conversion (`from_arrow_idx`, `rust_idx_to_arrow_idx`)
63/// - `IntoArrow` implementation for the enum
64/// - `FromArrow` implementation for the enum
65///
66/// # Requirements
67///
68/// - The enum must be a unit enum (no fields in variants)
69/// - All variants must have explicit discriminant values
70///
71/// # Attributes
72///
73/// - `#[type_hint(...)]`: Currently accepted but not used. Reserved for future use.
74/// - `#[enum_path(...)]`: Currently accepted but not used. Reserved for future use.
75///
76/// # Example
77///
78/// ```ignore
79/// use polars_structpath::EnumPath;
80/// use polars_structpath::{IntoArrow, ArrowBuffer};
81///
82/// #[derive(EnumPath)]
83/// enum Status {
84///     Active = 1,
85///     Inactive = 2,
86/// }
87///
88/// // Now Status implements IntoArrow and FromArrow
89/// let mut buffer = Status::new_buffer(2);
90/// buffer.push(Status::Active);
91/// buffer.push(Status::Inactive);
92/// let array = buffer.to_arrow().unwrap();
93/// ```
94#[proc_macro_derive(EnumPath, attributes(type_hint, enum_path))]
95pub fn derive_enum_path(input: TokenStream) -> TokenStream {
96    let input = parse_macro_input!(input as DeriveInput);
97    derive_enum_path_impl(input).into()
98}