rivven-connect-derive 0.0.22

Derive macros for Rivven connector development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Derive macros for Rivven connector development
//!
//! This crate provides procedural macros that reduce boilerplate when implementing
//! Rivven connectors.
//!
//! # Example
//!
//! ```rust,ignore
//! use rivven_connect_derive::{SourceConfig, SinkConfig, connector_spec};
//!
//! #[derive(Debug, Deserialize, Validate, JsonSchema, SourceConfig)]
//! #[source(
//!     name = "my-source",
//!     version = "1.0.0",
//!     description = "Custom data source",
//!     author = "Rivven Team",
//!     license = "Apache-2.0",
//!     documentation_url = "https://rivven.dev/docs/connectors/my-source"
//! )]
//! pub struct MySourceConfig {
//!     #[validate(url)]
//!     pub endpoint: String,
//!     #[validate(range(min = 1, max = 100))]
//!     pub batch_size: usize,
//! }
//!
//! #[derive(Debug, Deserialize, Validate, JsonSchema, SinkConfig)]
//! #[sink(
//!     name = "my-sink",
//!     version = "1.0.0",
//!     batching,
//!     batch_size = 1000
//! )]
//! pub struct MySinkConfig {
//!     pub bucket: String,
//! }
//! ```

use darling::{FromDeriveInput, FromMeta};
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

/// Attributes for the Source derive macro
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(source), supports(struct_named))]
struct SourceAttrs {
    ident: syn::Ident,
    /// Connector name (e.g., "postgres-cdc")
    #[darling(default)]
    #[allow(dead_code)]
    name: Option<String>,
    /// Connector version (e.g., "1.0.0")
    #[darling(default)]
    #[allow(dead_code)]
    version: Option<String>,
    /// Description of the connector
    #[darling(default)]
    #[allow(dead_code)]
    description: Option<String>,
    /// Author or maintainer
    #[darling(default)]
    #[allow(dead_code)]
    author: Option<String>,
    /// License identifier (e.g., "Apache-2.0")
    #[darling(default)]
    #[allow(dead_code)]
    license: Option<String>,
    /// Documentation URL
    #[darling(default)]
    #[allow(dead_code)]
    documentation_url: Option<String>,
    /// Whether the source supports incremental sync
    #[darling(default)]
    #[allow(dead_code)]
    incremental: bool,
    /// Supported source types (e.g., full_refresh, incremental)
    #[darling(default)]
    #[allow(dead_code)]
    source_types: Option<SourceTypesAttr>,
}

#[derive(Debug, Default, FromMeta)]
#[allow(dead_code)]
struct SourceTypesAttr {
    full_refresh: bool,
    incremental: bool,
}

/// Attributes for the Sink derive macro
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(sink), supports(struct_named))]
struct SinkAttrs {
    ident: syn::Ident,
    /// Connector name (e.g., "s3-sink")
    #[darling(default)]
    #[allow(dead_code)]
    name: Option<String>,
    /// Connector version (e.g., "1.0.0")
    #[darling(default)]
    #[allow(dead_code)]
    version: Option<String>,
    /// Description of the connector
    #[darling(default)]
    #[allow(dead_code)]
    description: Option<String>,
    /// Author or maintainer
    #[darling(default)]
    #[allow(dead_code)]
    author: Option<String>,
    /// License identifier (e.g., "Apache-2.0")
    #[darling(default)]
    #[allow(dead_code)]
    license: Option<String>,
    /// Documentation URL
    #[darling(default)]
    #[allow(dead_code)]
    documentation_url: Option<String>,
    /// Whether the sink supports batching
    #[darling(default)]
    #[allow(dead_code)]
    batching: bool,
    /// Default batch size
    #[darling(default)]
    #[allow(dead_code)]
    batch_size: Option<usize>,
}

/// Attributes for the Transform derive macro
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(transform), supports(struct_named))]
struct TransformAttrs {
    ident: syn::Ident,
    /// Transform name (e.g., "filter-transform")
    #[darling(default)]
    #[allow(dead_code)]
    name: Option<String>,
    /// Transform version (e.g., "1.0.0")
    #[darling(default)]
    #[allow(dead_code)]
    version: Option<String>,
    /// Description of the transform
    #[darling(default)]
    #[allow(dead_code)]
    description: Option<String>,
}

/// Derive macro for implementing SourceConfig boilerplate
///
/// This macro generates a `*Spec` struct with `spec()`, `name()`, and `version()` methods.
/// The generated spec includes config schema derived from the struct's JsonSchema implementation.
///
/// # Attributes
///
/// - `#[source(name = "...")]` - Connector name (defaults to struct name without "Config")
/// - `#[source(version = "...")]` - Connector version (default: env!("CARGO_PKG_VERSION") or "0.0.1")
/// - `#[source(description = "...")]` - Connector description
/// - `#[source(author = "...")]` - Author or maintainer
/// - `#[source(license = "...")]` - License identifier (e.g., "Apache-2.0")
/// - `#[source(documentation_url = "...")]` - Documentation URL
/// - `#[source(incremental)]` - Enable incremental sync support
///
/// # Example
///
/// ```rust,ignore
/// #[derive(Debug, Deserialize, Validate, JsonSchema, SourceConfig)]
/// #[source(
///     name = "postgres-cdc",
///     version = "1.0.0",
///     description = "PostgreSQL CDC connector",
///     author = "Rivven Team",
///     license = "Apache-2.0",
///     incremental
/// )]
/// pub struct PostgresCdcConfig {
///     pub connection_string: String,
///     pub slot_name: String,
/// }
/// ```
#[proc_macro_derive(SourceConfig, attributes(source))]
pub fn derive_source_config(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let attrs = match SourceAttrs::from_derive_input(&input) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };

    let struct_name = &attrs.ident;
    let spec_struct_name = quote::format_ident!("{}Spec", struct_name);

    let name = attrs.name.unwrap_or_else(|| {
        let name = struct_name.to_string();
        name.strip_suffix("Config").unwrap_or(&name).to_lowercase()
    });
    // Use the calling crate's CARGO_PKG_VERSION when no version
    // is explicitly specified, falling back to "0.0.1" only if that env var
    // is unavailable.
    let version_code = match &attrs.version {
        Some(v) => quote! { #v },
        None => quote! { match option_env!("CARGO_PKG_VERSION") {
            Some(v) => v,
            None => "0.0.1",
        } },
    };

    let description_code = match &attrs.description {
        Some(desc) => quote! { .description(#desc) },
        None => quote! {},
    };

    let author_code = match &attrs.author {
        Some(author) => quote! { .author(#author) },
        None => quote! {},
    };

    let license_code = match &attrs.license {
        Some(license) => quote! { .license(#license) },
        None => quote! {},
    };

    let doc_url_code = match &attrs.documentation_url {
        Some(url) => quote! { .documentation_url(#url) },
        None => quote! {},
    };

    let incremental_code = if attrs.incremental {
        quote! { .incremental(true) }
    } else {
        quote! {}
    };

    let expanded = quote! {
        /// Auto-generated spec holder for this source configuration
        pub struct #spec_struct_name;

        impl #spec_struct_name {
            /// Returns the connector specification with config schema
            pub fn spec() -> rivven_connect::ConnectorSpec {
                rivven_connect::ConnectorSpec::builder(#name, #version_code)
                    #description_code
                    #author_code
                    #license_code
                    #doc_url_code
                    #incremental_code
                    .config_schema::<#struct_name>()
                    .build()
            }

            /// Returns the connector name
            pub const fn name() -> &'static str {
                #name
            }

            /// Returns the connector version
            pub fn version() -> &'static str {
                #version_code
            }
        }
    };

    TokenStream::from(expanded)
}

/// Derive macro for implementing SinkConfig boilerplate
///
/// This macro generates a `*Spec` struct with `spec()`, `name()`, `version()`, and optionally
/// `batch_config()` methods. The generated spec includes config schema derived from the struct's
/// JsonSchema implementation.
///
/// # Attributes
///
/// - `#[sink(name = "...")]` - Connector name (defaults to struct name without "Config")
/// - `#[sink(version = "...")]` - Connector version (default: env!("CARGO_PKG_VERSION") or "0.0.1")
/// - `#[sink(description = "...")]` - Connector description
/// - `#[sink(author = "...")]` - Author or maintainer
/// - `#[sink(license = "...")]` - License identifier (e.g., "Apache-2.0")
/// - `#[sink(documentation_url = "...")]` - Documentation URL
/// - `#[sink(batching)]` - Enable batching support (generates batch_config() method)
/// - `#[sink(batch_size = N)]` - Default batch size (default: 10000)
///
/// # Example
///
/// ```rust,ignore
/// #[derive(Debug, Deserialize, Validate, JsonSchema, SinkConfig)]
/// #[sink(
///     name = "s3-sink",
///     version = "1.0.0",
///     description = "Amazon S3 storage sink",
///     author = "Rivven Team",
///     license = "Apache-2.0",
///     batching,
///     batch_size = 1000
/// )]
/// pub struct S3SinkConfig {
///     pub bucket: String,
///     pub prefix: Option<String>,
/// }
/// ```
#[proc_macro_derive(SinkConfig, attributes(sink))]
pub fn derive_sink_config(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let attrs = match SinkAttrs::from_derive_input(&input) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };

    let struct_name = &attrs.ident;
    let spec_struct_name = quote::format_ident!("{}Spec", struct_name);

    let name = attrs.name.unwrap_or_else(|| {
        let name = struct_name.to_string();
        name.strip_suffix("Config").unwrap_or(&name).to_lowercase()
    });
    let version_code = match &attrs.version {
        Some(v) => quote! { #v },
        None => quote! { match option_env!("CARGO_PKG_VERSION") {
            Some(v) => v,
            None => "0.0.1",
        } },
    };

    let description_code = match &attrs.description {
        Some(desc) => quote! { .description(#desc) },
        None => quote! {},
    };

    let author_code = match &attrs.author {
        Some(author) => quote! { .author(#author) },
        None => quote! {},
    };

    let license_code = match &attrs.license {
        Some(license) => quote! { .license(#license) },
        None => quote! {},
    };

    let doc_url_code = match &attrs.documentation_url {
        Some(url) => quote! { .documentation_url(#url) },
        None => quote! {},
    };

    let batch_config_code = if attrs.batching {
        let batch_size = attrs.batch_size.unwrap_or(10_000);
        quote! {
            /// Returns the default batch configuration
            pub fn batch_config() -> rivven_connect::BatchConfig {
                rivven_connect::BatchConfig {
                    max_records: #batch_size,
                    ..Default::default()
                }
            }
        }
    } else {
        quote! {}
    };

    let expanded = quote! {
        /// Auto-generated spec holder for this sink configuration
        pub struct #spec_struct_name;

        impl #spec_struct_name {
            /// Returns the connector specification with config schema
            pub fn spec() -> rivven_connect::ConnectorSpec {
                rivven_connect::ConnectorSpec::builder(#name, #version_code)
                    #description_code
                    #author_code
                    #license_code
                    #doc_url_code
                    .config_schema::<#struct_name>()
                    .build()
            }

            /// Returns the connector name
            pub const fn name() -> &'static str {
                #name
            }

            /// Returns the connector version
            pub fn version() -> &'static str {
                #version_code
            }

            #batch_config_code
        }
    };

    TokenStream::from(expanded)
}

/// Derive macro for implementing TransformConfig boilerplate
///
/// # Attributes
///
/// - `#[transform(name = "...")]` - Transform name (required)
/// - `#[transform(version = "...")]` - Transform version (default: env!("CARGO_PKG_VERSION") or "0.0.1")
/// - `#[transform(description = "...")]` - Transform description
///
/// # Example
///
/// ```rust,ignore
/// #[derive(Debug, Deserialize, Validate, JsonSchema, TransformConfig)]
/// #[transform(name = "json-filter", version = "1.0.0")]
/// pub struct JsonFilterConfig {
///     pub field: String,
///     pub pattern: String,
/// }
/// ```
#[proc_macro_derive(TransformConfig, attributes(transform))]
pub fn derive_transform_config(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let attrs = match TransformAttrs::from_derive_input(&input) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };

    let struct_name = &attrs.ident;
    let spec_struct_name = quote::format_ident!("{}Spec", struct_name);

    let name = attrs.name.unwrap_or_else(|| {
        let name = struct_name.to_string();
        name.strip_suffix("Config").unwrap_or(&name).to_lowercase()
    });
    let version_code = match &attrs.version {
        Some(v) => quote! { #v },
        None => quote! { match option_env!("CARGO_PKG_VERSION") {
            Some(v) => v,
            None => "0.0.1",
        } },
    };

    // Use `.description()` consistent with SourceConfig/SinkConfig
    let description_code = match attrs.description {
        Some(desc) => quote! { .description(#desc) },
        None => quote! {},
    };

    let expanded = quote! {
        /// Auto-generated spec holder for this transform configuration
        pub struct #spec_struct_name;

        impl #spec_struct_name {
            /// Returns the connector specification with config schema
            pub fn spec() -> rivven_connect::ConnectorSpec {
                rivven_connect::ConnectorSpec::builder(#name, #version_code)
                    #description_code
                    .config_schema::<#struct_name>()
                    .build()
            }

            /// Returns the transform name
            pub const fn name() -> &'static str {
                #name
            }

            /// Returns the transform version
            pub fn version() -> &'static str {
                #version_code
            }
        }
    };

    TokenStream::from(expanded)
}

/// Attributes for connector_spec macro
#[derive(Debug, Default, FromMeta)]
struct ConnectorSpecAttrs {
    /// Connector name (required — compile error if missing)
    #[darling(default)]
    name: Option<String>,
    /// Connector version
    #[darling(default)]
    #[allow(dead_code)]
    version: Option<String>,
    /// Description of the connector
    #[darling(default)]
    #[allow(dead_code)]
    description: Option<String>,
    /// Documentation URL
    #[darling(default)]
    #[allow(dead_code)]
    documentation_url: Option<String>,
}

/// Attribute macro for defining connector specifications inline
///
/// This macro can be applied to a module or struct to generate
/// a `ConnectorSpec` with the given attributes.
///
/// # Example
///
/// ```rust,ignore
/// #[connector_spec(
///     name = "my-connector",
///     version = "1.0.0",
///     description = "A custom connector",
///     documentation_url = "https://docs.example.com"
/// )]
/// pub mod my_connector {
///     // Connector implementation
/// }
/// ```
#[proc_macro_attribute]
pub fn connector_spec(attr: TokenStream, item: TokenStream) -> TokenStream {
    // Parse attributes using darling
    let attr_args = match darling::ast::NestedMeta::parse_meta_list(attr.into()) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(darling::Error::from(e).write_errors()),
    };

    let attrs = match ConnectorSpecAttrs::from_list(&attr_args) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };

    // Emit a compile error when `name` is not provided,
    // instead of silently defaulting to "unknown".
    let name = match attrs.name {
        Some(n) => n,
        None => {
            return TokenStream::from(
                syn::Error::new(
                    proc_macro2::Span::call_site(),
                    "connector_spec requires `name = \"...\"` attribute",
                )
                .to_compile_error(),
            );
        }
    };
    let version_code = match &attrs.version {
        Some(v) => quote! { #v },
        None => quote! { match option_env!("CARGO_PKG_VERSION") {
            Some(v) => v,
            None => "0.0.1",
        } },
    };

    // Use `.description()` and `.documentation_url()` consistent with builder API
    let description_code = match attrs.description {
        Some(desc) => quote! { .description(#desc) },
        None => quote! {},
    };

    let doc_url_code = match attrs.documentation_url {
        Some(url) => quote! { .documentation_url(#url) },
        None => quote! {},
    };

    let item: proc_macro2::TokenStream = item.into();

    let expanded = quote! {
        #item

        /// Auto-generated connector specification
        pub fn connector_spec() -> rivven_connect::ConnectorSpec {
            rivven_connect::ConnectorSpec::builder(#name, #version_code)
                #description_code
                #doc_url_code
                .build()
        }
    };

    TokenStream::from(expanded)
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_derives_compile() {
        // These tests just verify the macros compile
        // Actual behavior is tested in integration tests
    }
}