rigatoni-destinations 0.2.0

Destination implementations for Rigatoni CDC/Data Replication: S3 with multiple formats and compression
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
// Copyright 2025 Rigatoni Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! S3 destination configuration.
//!
//! This module provides configuration options for the S3 destination, including:
//! - Bucket and region configuration
//! - Serialization format selection
//! - Compression options
//! - Key generation strategies
//! - Retry and performance tuning

use crate::s3::key_gen::KeyGenerationStrategy;

/// Errors that can occur during S3 configuration.
#[derive(Debug, thiserror::Error)]
pub enum S3ConfigError {
    /// Bucket name is required.
    #[error("bucket is required")]
    MissingBucket,

    /// Bucket name is empty.
    #[error("bucket cannot be empty")]
    EmptyBucket,

    /// Bucket name is invalid.
    #[error("invalid bucket name: {name} ({reason})")]
    InvalidBucket {
        /// The invalid bucket name
        name: String,
        /// Reason why it's invalid
        reason: &'static str,
    },

    /// Region is required.
    #[error("region is required")]
    MissingRegion,

    /// Region is empty.
    #[error("region cannot be empty")]
    EmptyRegion,

    /// Invalid prefix (contains invalid characters).
    #[error("invalid prefix: {prefix} ({reason})")]
    InvalidPrefix {
        /// The invalid prefix
        prefix: String,
        /// Reason why it's invalid
        reason: &'static str,
    },
}

/// Serialization format for S3 objects.
///
/// Different formats have different trade-offs:
/// - **JSON**: Human-readable, easy to query with S3 Select, moderate size
/// - **Parquet**: Columnar format, excellent compression, fast queries, requires schema
/// - **CSV**: Simple, widely supported, but limited type support
/// - **Avro**: Schema evolution support, compact, good for streaming
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SerializationFormat {
    /// Newline-delimited JSON (JSONL) - one JSON object per line.
    ///
    /// Best for: Human readability, S3 Select queries, mixed schemas
    /// File extension: `.jsonl` or `.ndjson`
    Json,

    /// Apache Parquet columnar format.
    ///
    /// Best for: Analytics, compression ratio, fast queries
    /// File extension: `.parquet`
    /// Requires: Schema definition
    #[cfg(feature = "parquet")]
    Parquet,

    /// Comma-separated values.
    ///
    /// Best for: Excel compatibility, simple data
    /// File extension: `.csv`
    #[cfg(feature = "csv")]
    Csv,

    /// Apache Avro binary format.
    ///
    /// Best for: Schema evolution, streaming, Kafka integration
    /// File extension: `.avro`
    #[cfg(feature = "avro")]
    Avro,
}

impl SerializationFormat {
    /// Returns the file extension for this format (without the dot).
    #[must_use]
    pub const fn extension(&self) -> &'static str {
        match self {
            Self::Json => "jsonl",
            #[cfg(feature = "parquet")]
            Self::Parquet => "parquet",
            #[cfg(feature = "csv")]
            Self::Csv => "csv",
            #[cfg(feature = "avro")]
            Self::Avro => "avro",
        }
    }

    /// Returns the MIME type for this format.
    #[must_use]
    pub const fn content_type(&self) -> &'static str {
        match self {
            Self::Json => "application/x-ndjson",
            #[cfg(feature = "parquet")]
            Self::Parquet => "application/octet-stream",
            #[cfg(feature = "csv")]
            Self::Csv => "text/csv",
            #[cfg(feature = "avro")]
            Self::Avro => "application/avro",
        }
    }
}

/// Compression algorithm for S3 objects.
///
/// Compression reduces storage costs and bandwidth but adds CPU overhead.
///
/// **Benchmarks** (approximate, data-dependent):
/// - **None**: 0ms CPU, 100MB storage, 100MB bandwidth
/// - **Gzip**: 50ms CPU, 20MB storage, 20MB bandwidth, wide compatibility
/// - **Zstd**: 30ms CPU, 18MB storage, 18MB bandwidth, better ratio & speed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Compression {
    /// No compression - fastest writes, largest files.
    #[default]
    None,

    /// Gzip compression (RFC 1952).
    ///
    /// Compression level: 6 (default balance of speed/size)
    /// File extension: `.gz`
    /// Best for: Wide compatibility, moderate compression
    #[cfg(feature = "gzip")]
    Gzip,

    /// Zstandard compression.
    ///
    /// Compression level: 3 (default, good balance)
    /// File extension: `.zst`
    /// Best for: Better compression ratio and speed than gzip
    #[cfg(feature = "zstandard")]
    Zstd,
}

impl Compression {
    /// Returns the file extension suffix for this compression (with the dot).
    ///
    /// This is appended to the serialization format extension.
    /// Example: `events.jsonl.gz`
    #[must_use]
    pub const fn extension(&self) -> &'static str {
        match self {
            Self::None => "",
            #[cfg(feature = "gzip")]
            Self::Gzip => ".gz",
            #[cfg(feature = "zstandard")]
            Self::Zstd => ".zst",
        }
    }

    /// Returns the Content-Encoding header value.
    #[must_use]
    pub const fn encoding(&self) -> Option<&'static str> {
        match self {
            Self::None => None,
            #[cfg(feature = "gzip")]
            Self::Gzip => Some("gzip"),
            #[cfg(feature = "zstandard")]
            Self::Zstd => Some("zstd"),
        }
    }
}

/// AWS credentials for S3 access.
///
/// Used primarily for testing with LocalStack or other S3-compatible services.
/// In production, prefer using IAM roles or the AWS credential chain.
#[derive(Debug, Clone)]
pub struct AwsCredentials {
    /// AWS Access Key ID
    pub access_key_id: String,
    /// AWS Secret Access Key
    pub secret_access_key: String,
    /// Optional session token for temporary credentials
    pub session_token: Option<String>,
}

impl AwsCredentials {
    /// Creates new AWS credentials.
    #[must_use]
    pub fn new(access_key_id: impl Into<String>, secret_access_key: impl Into<String>) -> Self {
        Self {
            access_key_id: access_key_id.into(),
            secret_access_key: secret_access_key.into(),
            session_token: None,
        }
    }

    /// Creates new AWS credentials with a session token.
    #[must_use]
    pub fn with_session_token(
        access_key_id: impl Into<String>,
        secret_access_key: impl Into<String>,
        session_token: impl Into<String>,
    ) -> Self {
        Self {
            access_key_id: access_key_id.into(),
            secret_access_key: secret_access_key.into(),
            session_token: Some(session_token.into()),
        }
    }
}

/// Configuration for S3 destination.
///
/// # Examples
///
/// ## Basic configuration
///
/// ```rust,ignore
/// use rigatoni_destinations::s3::S3Config;
///
/// let config = S3Config::builder()
///     .bucket("my-data-lake")
///     .region("us-east-1")
///     .prefix("mongodb/events")
///     .build()
///     .unwrap();
/// ```
///
/// ## With compression and custom key strategy
///
/// ```rust,ignore
/// use rigatoni_destinations::s3::{S3Config, Compression, KeyGenerationStrategy};
///
/// let config = S3Config::builder()
///     .bucket("my-data-lake")
///     .region("us-west-2")
///     .prefix("events")
///     .compression(Compression::Zstd)
///     .key_strategy(KeyGenerationStrategy::HivePartitioned)
///     .build()
///     .unwrap();
/// ```
///
/// ## LocalStack with explicit credentials
///
/// ```rust,ignore
/// use rigatoni_destinations::s3::{S3Config, AwsCredentials};
///
/// let config = S3Config::builder()
///     .bucket("test-bucket")
///     .region("us-east-1")
///     .endpoint_url("http://localhost:4566")
///     .force_path_style(true)
///     .credentials(AwsCredentials::new("test", "test"))
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct S3Config {
    /// S3 bucket name (required).
    pub bucket: String,

    /// AWS region (required).
    ///
    /// Examples: "us-east-1", "eu-west-1", "ap-southeast-2"
    pub region: String,

    /// Key prefix (optional).
    ///
    /// All generated keys will start with this prefix.
    /// Example: "mongodb/prod/events" → keys like "mongodb/prod/events/users/2025/01/15/..."
    pub prefix: Option<String>,

    /// Serialization format (default: JSON).
    pub format: SerializationFormat,

    /// Compression algorithm (default: None).
    pub compression: Compression,

    /// Key generation strategy (default: `DateHourPartitioned`).
    pub key_strategy: KeyGenerationStrategy,

    /// Maximum retries for S3 operations (default: 3).
    ///
    /// The SDK will retry on throttling errors (429, 503) with exponential backoff.
    pub max_retries: u32,

    /// Custom endpoint URL for S3-compatible storage (e.g., MinIO, LocalStack).
    ///
    /// Example: "http://localhost:4566" for LocalStack
    pub endpoint_url: Option<String>,

    /// Whether to use path-style addressing (default: false).
    ///
    /// Path-style: `https://s3.region.amazonaws.com/bucket/key`
    /// Virtual-hosted: `https://bucket.s3.region.amazonaws.com/key`
    ///
    /// Required for: LocalStack, MinIO
    pub force_path_style: bool,

    /// Optional explicit credentials (for testing/LocalStack).
    ///
    /// If not provided, the AWS SDK will use the default credential chain
    /// (environment variables, instance profiles, etc.).
    pub credentials: Option<AwsCredentials>,
}

impl Default for S3Config {
    fn default() -> Self {
        Self {
            bucket: String::new(),
            region: String::from("us-east-1"),
            prefix: None,
            format: SerializationFormat::Json,
            compression: Compression::None,
            key_strategy: KeyGenerationStrategy::DateHourPartitioned,
            max_retries: 3,
            endpoint_url: None,
            force_path_style: false,
            credentials: None,
        }
    }
}

/// Builder for `S3Config`.
///
/// Provides a fluent API for constructing S3 configuration with validation.
#[derive(Debug, Default)]
pub struct S3ConfigBuilder {
    bucket: Option<String>,
    region: Option<String>,
    prefix: Option<String>,
    format: Option<SerializationFormat>,
    compression: Option<Compression>,
    key_strategy: Option<KeyGenerationStrategy>,
    max_retries: Option<u32>,
    endpoint_url: Option<String>,
    force_path_style: Option<bool>,
    credentials: Option<AwsCredentials>,
}

impl S3ConfigBuilder {
    /// Sets the S3 bucket name (required).
    #[must_use]
    pub fn bucket(mut self, bucket: impl Into<String>) -> Self {
        self.bucket = Some(bucket.into());
        self
    }

    /// Sets the AWS region (required).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// builder.region("us-east-1")
    /// builder.region("eu-west-1")
    /// builder.region("ap-southeast-2")
    /// ```
    #[must_use]
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = Some(region.into());
        self
    }

    /// Sets the key prefix (optional).
    ///
    /// All S3 keys will start with this prefix.
    #[must_use]
    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = Some(prefix.into());
        self
    }

    /// Sets the serialization format (default: JSON).
    #[must_use]
    pub fn format(mut self, format: SerializationFormat) -> Self {
        self.format = Some(format);
        self
    }

    /// Sets the compression algorithm (default: None).
    #[must_use]
    pub fn compression(mut self, compression: Compression) -> Self {
        self.compression = Some(compression);
        self
    }

    /// Sets the key generation strategy (default: `DateHourPartitioned`).
    #[must_use]
    pub fn key_strategy(mut self, strategy: KeyGenerationStrategy) -> Self {
        self.key_strategy = Some(strategy);
        self
    }

    /// Sets the maximum number of retries (default: 3).
    #[must_use]
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = Some(retries);
        self
    }

    /// Sets a custom S3 endpoint URL (for S3-compatible storage).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// // LocalStack
    /// builder.endpoint_url("http://localhost:4566")
    ///
    /// // MinIO
    /// builder.endpoint_url("http://minio:9000")
    /// ```
    #[must_use]
    pub fn endpoint_url(mut self, url: impl Into<String>) -> Self {
        self.endpoint_url = Some(url.into());
        self
    }

    /// Forces path-style addressing (required for LocalStack/MinIO).
    ///
    /// When enabled, URLs will be: `https://s3.region.amazonaws.com/bucket/key`
    /// instead of `https://bucket.s3.region.amazonaws.com/key`
    #[must_use]
    pub fn force_path_style(mut self, force: bool) -> Self {
        self.force_path_style = Some(force);
        self
    }

    /// Sets explicit AWS credentials (for testing/LocalStack).
    ///
    /// If not provided, the AWS SDK will use the default credential chain.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use rigatoni_destinations::s3::{S3Config, AwsCredentials};
    ///
    /// // LocalStack credentials
    /// let config = S3Config::builder()
    ///     .bucket("test-bucket")
    ///     .region("us-east-1")
    ///     .endpoint_url("http://localhost:4566")
    ///     .credentials(AwsCredentials::new("test", "test"))
    ///     .build()?;
    /// ```
    #[must_use]
    pub fn credentials(mut self, credentials: AwsCredentials) -> Self {
        self.credentials = Some(credentials);
        self
    }

    /// Builds the `S3Config`.
    ///
    /// # Errors
    ///
    /// Returns an error if required fields are missing or invalid:
    /// - `bucket` is required and must not be empty
    /// - `bucket` must be 3-63 characters long
    /// - `bucket` must contain only lowercase letters, numbers, hyphens, and periods
    /// - `region` is required and must not be empty
    /// - `prefix` cannot contain `..` (path traversal)
    /// - `prefix` cannot start with `/`
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let config = S3Config::builder()
    ///     .bucket("my-bucket")
    ///     .region("us-east-1")
    ///     .build()?;
    /// ```
    pub fn build(self) -> Result<S3Config, S3ConfigError> {
        let bucket = self.bucket.ok_or(S3ConfigError::MissingBucket)?;

        if bucket.is_empty() {
            return Err(S3ConfigError::EmptyBucket);
        }

        // Validate bucket name (RFC 1035-like: 3-63 chars, lowercase, no underscores)
        if bucket.len() < 3 || bucket.len() > 63 {
            return Err(S3ConfigError::InvalidBucket {
                name: bucket,
                reason: "must be 3-63 characters",
            });
        }

        if !bucket
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '.')
        {
            return Err(S3ConfigError::InvalidBucket {
                name: bucket,
                reason: "must contain only lowercase letters, numbers, hyphens, and periods",
            });
        }

        let region = self.region.ok_or(S3ConfigError::MissingRegion)?;

        if region.is_empty() {
            return Err(S3ConfigError::EmptyRegion);
        }

        // Validate prefix if provided
        if let Some(ref prefix) = self.prefix {
            if prefix.contains("..") {
                return Err(S3ConfigError::InvalidPrefix {
                    prefix: prefix.clone(),
                    reason: "prefix cannot contain '..' (path traversal)",
                });
            }

            if prefix.starts_with('/') {
                return Err(S3ConfigError::InvalidPrefix {
                    prefix: prefix.clone(),
                    reason: "prefix cannot start with '/'",
                });
            }
        }

        Ok(S3Config {
            bucket,
            region,
            prefix: self.prefix,
            format: self.format.unwrap_or(SerializationFormat::Json),
            compression: self.compression.unwrap_or_default(),
            key_strategy: self
                .key_strategy
                .unwrap_or(KeyGenerationStrategy::DateHourPartitioned),
            max_retries: self.max_retries.unwrap_or(3),
            endpoint_url: self.endpoint_url,
            force_path_style: self.force_path_style.unwrap_or(false),
            credentials: self.credentials,
        })
    }
}

impl S3Config {
    /// Creates a new builder for `S3Config`.
    #[must_use]
    pub fn builder() -> S3ConfigBuilder {
        S3ConfigBuilder::default()
    }
}