vector_config_macros/lib.rs
1#![deny(warnings)]
2
3use proc_macro::TokenStream;
4
5mod ast;
6mod attrs;
7mod component_name;
8mod configurable;
9mod configurable_component;
10
11/// Designates a type as being part of a Vector configuration.
12///
13/// This will automatically derive the [`Configurable`][vector-config::Configurable] trait for the given struct/enum, as
14/// well as ensuring that serialization/deserialization (via `serde`) is derived.
15///
16/// ## Basics
17///
18/// In its most basic form, this attribute macro can be used to simply derive the aforementioned traits, making it using
19/// in any other type also deriving `Configurable`:
20///
21/// ```no_run
22/// use vector_config::configurable_component;
23/// use serde;
24///
25/// /// Batching configurations.
26/// #[configurable_component]
27/// #[derive(Clone, Debug)]
28/// pub struct BatchSettings {
29/// // ...
30/// }
31/// ```
32///
33/// ## Component-specific modifiers
34///
35/// Additionally, callers can specify the component type, when being used directly on the top-level configuration object
36/// for a component by specifying the component type (`enrichment_table`, `provider`, `sink`,
37/// `source`, or `transform`) and the name of the component:
38///
39/// ```ignore
40/// use vector_config::configurable_component;
41/// use serde;
42///
43/// /// Configuration for the `kafka` source.
44/// #[configurable_component(source("kafka"))]
45/// #[derive(Clone, Debug)]
46/// pub struct KafkaSourceConfig {
47/// // ...
48/// }
49/// ```
50///
51/// This adds special metadata to the generated schema for that type, which indicates that it
52/// represents the top-level configuration object as a component of the given type. Additionally,
53/// relevant traits and annotations will be added to register the component (using the given name)
54/// within Vector, for the purposes of example configuration generation, and so on.
55///
56/// ## Opting out of automatic derives
57///
58/// This macro will also derive the `Deserialize` and `Serialize` traits from `serde` automatically, as a way to clean
59/// up the derives of a type which is already using `#[configurable_component]`. However, some types employ custom
60/// (de)serialization implementations and do not need a standard derivation of those traits. In those cases, callers can
61/// mark the type as not needing automatic derivations in a piecemeal fashion with the `no_ser` and/or `no_deser` modifiers:
62///
63/// ```no_run
64/// use vector_config::configurable_component;
65///
66/// /// Helper type with custom deserialization logic.
67/// #[configurable_component(no_deser)]
68/// # #[derive(::serde::Deserialize)]
69/// pub struct HelperTypeWithCustomDeser {
70/// // This type brings its own implementation of `Deserialize` so we simply avoid bringing it in
71/// // via `#[configurable_component]` but `Serialize` is still being automatically derived for us.
72/// }
73///
74/// /// Helper type with entirely custom (de)serialization logic.
75/// #[configurable_component(no_deser, no_ser)]
76/// # #[derive(::serde::Deserialize, ::serde::Serialize)]
77/// pub struct HelperTypeWithCustomDeserAndSer {
78/// // This type brings its own implementation of `Deserialize` _and_ `Serialize` so we've avoided
79/// // having them automatically derived via `#[configurable_component]`.
80/// }
81/// ```
82#[proc_macro_attribute]
83pub fn configurable_component(attrs: TokenStream, item: TokenStream) -> TokenStream {
84 configurable_component::configurable_component_impl(attrs, item)
85}
86
87/// Generates an implementation of the `Configurable` trait for the given container.
88///
89/// In general, `#[configurable_component]` should be preferred as it ensures the other necessary derives/trait
90/// implementations are provided, and offers other features related to describing specific configuration types, etc.
91#[proc_macro_derive(Configurable, attributes(configurable))]
92pub fn derive_configurable(input: TokenStream) -> TokenStream {
93 configurable::derive_configurable_impl(input)
94}
95
96/// Generates an implementation of the `NamedComponent` trait for the given container.
97#[proc_macro_derive(
98 NamedComponent,
99 attributes(
100 api_component,
101 enrichment_table_component,
102 global_option_component,
103 provider_component,
104 secrets_component,
105 sink_component,
106 source_component,
107 transform_component
108 )
109)]
110pub fn derive_component_name(input: TokenStream) -> TokenStream {
111 component_name::derive_component_name_impl(input)
112}