Skip to main content

noyalib/
macros.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) 2026 Noyalib. All rights reserved.
3
4//! Declarative builders for the public config types.
5//!
6//! Pairs with the chained-setter builders on [`crate::ParserConfig`]
7//! and [`crate::SerializerConfig`]: each `field: value` entry is
8//! translated to a call to the same-named builder method on the
9//! `new()` baseline, so the result is byte-identical to writing the
10//! chain by hand. Zero runtime overhead — the expansion is a single
11//! struct construction the optimiser folds away.
12//!
13//! # Examples
14//!
15//! ```
16//! use noyalib::parser_config;
17//!
18//! let cfg = parser_config! {
19//!     max_depth: 64,
20//!     strict_booleans: true,
21//! };
22//! assert_eq!(cfg.max_depth, 64);
23//! assert!(cfg.strict_booleans);
24//! ```
25//!
26//! ```
27//! use noyalib::serializer_config;
28//!
29//! let cfg = serializer_config! {
30//!     indent: 4,
31//!     quote_all: true,
32//! };
33//! ```
34//!
35//! Both macros accept zero entries (yielding the same value as
36//! `ParserConfig::new()` / `SerializerConfig::new()`) and a
37//! trailing comma after the last entry.
38
39/// Construct a [`crate::ParserConfig`] from a field-value list.
40///
41/// Equivalent to chaining the named builder methods on
42/// [`crate::ParserConfig::new()`]. Empty input returns the
43/// `new()` baseline. Trailing comma is permitted.
44///
45/// # Examples
46///
47/// ```
48/// use noyalib::parser_config;
49///
50/// // Empty form — equivalent to `ParserConfig::new()`.
51/// let _ = parser_config! {};
52///
53/// // Full chain.
54/// let cfg = parser_config! {
55///     max_depth: 32,
56///     max_alias_expansions: 200,
57///     strict_booleans: true,
58/// };
59/// assert_eq!(cfg.max_depth, 32);
60/// assert_eq!(cfg.max_alias_expansions, 200);
61/// assert!(cfg.strict_booleans);
62/// ```
63#[macro_export]
64macro_rules! parser_config {
65    () => { $crate::ParserConfig::new() };
66    ( $( $field:ident : $value:expr ),+ $(,)? ) => {{
67        $crate::ParserConfig::new()
68            $( .$field($value) )+
69    }};
70}
71
72/// Construct a [`crate::SerializerConfig`] from a field-value list.
73///
74/// Equivalent to chaining the named builder methods on
75/// [`crate::SerializerConfig::new()`]. Empty input returns the
76/// `new()` baseline. Trailing comma is permitted.
77///
78/// # Examples
79///
80/// ```
81/// use noyalib::serializer_config;
82///
83/// // Empty form — equivalent to `SerializerConfig::new()`.
84/// let _ = serializer_config! {};
85///
86/// // Full chain.
87/// let cfg = serializer_config! {
88///     indent: 4,
89///     quote_all: true,
90/// };
91/// ```
92#[macro_export]
93macro_rules! serializer_config {
94    () => { $crate::SerializerConfig::new() };
95    ( $( $field:ident : $value:expr ),+ $(,)? ) => {{
96        $crate::SerializerConfig::new()
97            $( .$field($value) )+
98    }};
99}