kore_fileformat 1.1.4

KORE — Killer Optimized Record Exchange: standalone Rust crate (zero deps)
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
//! AWS S3 Connector for Kore FileFormat
//! 
//! Provides native support for reading and writing Kore files directly from/to AWS S3.
//! 
//! # Features
//! 
//! To use this module, enable the `s3` feature in Cargo.toml:
//! ```toml
//! [dependencies]
//! kore_fileformat = { version = "1.0", features = ["s3"] }
//! ```
//! 
//! # Examples
//! 
//! ```ignore
//! use kore_fileformat::s3_reader::S3Reader;
//! 
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let mut reader = S3Reader::new("us-east-1")?;
//!     reader.with_cache("./cache")?;
//!     let data = reader.read_file("my-bucket", "path/to/file.kore").await?;
//!     Ok(())
//! }
//! ```

use std::error::Error;
use std::fmt;

/// S3 Reader Configuration and Operations
#[derive(Debug, Clone)]
pub struct S3Reader {
    region: String,
    cache_enabled: bool,
    cache_dir: Option<String>,
}

/// Error types for S3 operations
#[derive(Debug)]
pub enum S3Error {
    /// AWS SDK error
    AwsError(String),
    /// Invalid S3 path format
    InvalidPath(String),
    /// File not found in S3
    NotFound(String),
    /// Authentication failed
    AuthenticationError(String),
    /// IO error
    IoError(String),
}

impl fmt::Display for S3Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            S3Error::AwsError(e) => write!(f, "AWS Error: {}", e),
            S3Error::InvalidPath(p) => write!(f, "Invalid S3 path: {}", p),
            S3Error::NotFound(p) => write!(f, "File not found in S3: {}", p),
            S3Error::AuthenticationError(e) => write!(f, "Authentication failed: {}", e),
            S3Error::IoError(e) => write!(f, "IO Error: {}", e),
        }
    }
}

impl Error for S3Error {}

impl S3Reader {
    /// Create a new S3Reader with specified AWS region
    /// 
    /// # Arguments
    /// * `region` - AWS region (e.g., "us-east-1", "eu-west-1")
    /// 
    /// # Example
    /// ```rust
    /// let reader = S3Reader::new("us-east-1")?;
    /// ```
    pub fn new(region: &str) -> Result<Self, S3Error> {
        if region.is_empty() {
            return Err(S3Error::InvalidPath("Region cannot be empty".to_string()));
        }

        Ok(S3Reader {
            region: region.to_string(),
            cache_enabled: false,
            cache_dir: None,
        })
    }

    /// Enable caching of downloaded files locally
    /// 
    /// # Arguments
    /// * `cache_dir` - Directory to cache files in
    /// 
    /// # Example
    /// ```rust
    /// let mut reader = S3Reader::new("us-east-1")?;
    /// reader.with_cache("./kore_cache")?;
    /// ```
    pub fn with_cache(&mut self, cache_dir: &str) -> Result<(), S3Error> {
        if cache_dir.is_empty() {
            return Err(S3Error::InvalidPath("Cache directory cannot be empty".to_string()));
        }
        
        self.cache_enabled = true;
        self.cache_dir = Some(cache_dir.to_string());
        Ok(())
    }

    /// Read a Kore file from S3
    /// 
    /// # Arguments
    /// * `bucket` - S3 bucket name
    /// * `key` - S3 object key (path)
    /// 
    /// # Returns
    /// * `Vec<u8>` - File contents as bytes
    /// 
    /// # Example
    /// ```rust
    /// let reader = S3Reader::new("us-east-1")?;
    /// let data = reader.read_file("my-bucket", "data/records.kore").await?;
    /// ```
    pub async fn read_file(&self, bucket: &str, key: &str) -> Result<Vec<u8>, S3Error> {
        // Validate inputs
        if bucket.is_empty() {
            return Err(S3Error::InvalidPath("Bucket name cannot be empty".to_string()));
        }
        if key.is_empty() {
            return Err(S3Error::InvalidPath("Object key cannot be empty".to_string()));
        }

        // Check cache first if enabled
        if self.cache_enabled {
            if let Some(cached) = self.read_from_cache(bucket, key).await {
                return Ok(cached);
            }
        }

        // Read from S3 (placeholder - actual AWS SDK implementation)
        let data = self.read_from_s3(bucket, key).await?;

        // Write to cache if enabled
        if self.cache_enabled {
            let _ = self.write_to_cache(bucket, key, &data).await;
        }

        Ok(data)
    }

    /// Write a Kore file to S3
    /// 
    /// # Arguments
    /// * `bucket` - S3 bucket name
    /// * `key` - S3 object key (path)
    /// * `data` - File contents as bytes
    /// 
    /// # Example
    /// ```rust
    /// let reader = S3Reader::new("us-east-1")?;
    /// let data = vec![0x4b, 0x4f, 0x52, 0x45]; // KORE magic bytes
    /// reader.write_file("my-bucket", "output.kore", &data).await?;
    /// ```
    pub async fn write_file(
        &self,
        bucket: &str,
        key: &str,
        data: &[u8],
    ) -> Result<(), S3Error> {
        if bucket.is_empty() {
            return Err(S3Error::InvalidPath("Bucket name cannot be empty".to_string()));
        }
        if key.is_empty() {
            return Err(S3Error::InvalidPath("Object key cannot be empty".to_string()));
        }

        self.write_to_s3(bucket, key, data).await?;

        // Update cache if enabled
        if self.cache_enabled {
            let _ = self.write_to_cache(bucket, key, data).await;
        }

        Ok(())
    }

    /// List Kore files in an S3 bucket/prefix
    /// 
    /// # Arguments
    /// * `bucket` - S3 bucket name
    /// * `prefix` - S3 prefix (optional)
    /// 
    /// # Returns
    /// * `Vec<String>` - List of object keys
    /// 
    /// # Example
    /// ```rust
    /// let reader = S3Reader::new("us-east-1")?;
    /// let files = reader.list_files("my-bucket", Some("data/")).await?;
    /// ```
    pub async fn list_files(
        &self,
        bucket: &str,
        prefix: Option<&str>,
    ) -> Result<Vec<String>, S3Error> {
        if bucket.is_empty() {
            return Err(S3Error::InvalidPath("Bucket name cannot be empty".to_string()));
        }

        self.list_s3_objects(bucket, prefix).await
    }

    /// Get file metadata from S3
    /// 
    /// # Arguments
    /// * `bucket` - S3 bucket name
    /// * `key` - S3 object key
    /// 
    /// # Returns
    /// * `S3FileMetadata` - File metadata
    pub async fn get_metadata(
        &self,
        bucket: &str,
        key: &str,
    ) -> Result<S3FileMetadata, S3Error> {
        if bucket.is_empty() {
            return Err(S3Error::InvalidPath("Bucket name cannot be empty".to_string()));
        }
        if key.is_empty() {
            return Err(S3Error::InvalidPath("Object key cannot be empty".to_string()));
        }

        self.fetch_s3_metadata(bucket, key).await
    }

    // Private helper methods
    async fn read_from_s3(&self, bucket: &str, key: &str) -> Result<Vec<u8>, S3Error> {
        #[cfg(feature = "s3")]
        {
            use aws_sdk_s3::Client;
            use aws_config::BehaviorVersion;
            
            let config = aws_config::load_defaults(BehaviorVersion::latest())
                .await;
            let client = Client::new(&config);

            let resp = client
                .get_object()
                .bucket(bucket)
                .key(key)
                .send()
                .await
                .map_err(|e| S3Error::AwsError(format!("Failed to read from S3: {}", e)))?;

            let body = resp
                .body
                .collect()
                .await
                .map_err(|e| S3Error::IoError(format!("Failed to read response body: {}", e)))?;

            Ok(body.into_bytes().to_vec())
        }
        
        #[cfg(not(feature = "s3"))]
        {
            Err(S3Error::AwsError(
                "AWS SDK integration not enabled - compile with 's3' feature".to_string(),
            ))
        }
    }

    async fn write_to_s3(&self, bucket: &str, key: &str, data: &[u8]) -> Result<(), S3Error> {
        #[cfg(feature = "s3")]
        {
            use aws_sdk_s3::Client;
            use aws_config::BehaviorVersion;
            use aws_sdk_s3::primitives::ByteStream;
            
            let config = aws_config::load_defaults(BehaviorVersion::latest())
                .await;
            let client = Client::new(&config);

            let byte_stream = ByteStream::from(data.to_vec());

            client
                .put_object()
                .bucket(bucket)
                .key(key)
                .body(byte_stream)
                .send()
                .await
                .map_err(|e| S3Error::AwsError(format!("Failed to write to S3: {}", e)))?;

            Ok(())
        }
        
        #[cfg(not(feature = "s3"))]
        {
            Err(S3Error::AwsError(
                "AWS SDK integration not enabled - compile with 's3' feature".to_string(),
            ))
        }
    }

    async fn list_s3_objects(
        &self,
        bucket: &str,
        prefix: Option<&str>,
    ) -> Result<Vec<String>, S3Error> {
        #[cfg(feature = "s3")]
        {
            use aws_sdk_s3::Client;
            use aws_config::BehaviorVersion;
            
            let config = aws_config::load_defaults(BehaviorVersion::latest())
                .await;
            let client = Client::new(&config);

            let mut request = client
                .list_objects_v2()
                .bucket(bucket);

            if let Some(p) = prefix {
                request = request.prefix(p);
            }

            let resp = request
                .send()
                .await
                .map_err(|e| S3Error::AwsError(format!("Failed to list S3 objects: {}", e)))?;

            let mut objects = Vec::new();
            // resp.contents() returns &[Object], not Option
            for obj in resp.contents() {
                if let Some(key) = obj.key() {
                    objects.push(key.to_string());
                }
            }

            Ok(objects)
        }
        
        #[cfg(not(feature = "s3"))]
        {
            Err(S3Error::AwsError(
                "AWS SDK integration not enabled - compile with 's3' feature".to_string(),
            ))
        }
    }

    async fn fetch_s3_metadata(
        &self,
        bucket: &str,
        key: &str,
    ) -> Result<S3FileMetadata, S3Error> {
        #[cfg(feature = "s3")]
        {
            use aws_sdk_s3::Client;
            use aws_config::BehaviorVersion;
            
            let config = aws_config::load_defaults(BehaviorVersion::latest())
                .await;
            let client = Client::new(&config);

            let resp = client
                .head_object()
                .bucket(bucket)
                .key(key)
                .send()
                .await
                .map_err(|e| S3Error::AwsError(format!("Failed to fetch S3 metadata: {}", e)))?;

            let size = resp.content_length().unwrap_or(0) as u64;
            let last_modified = resp
                .last_modified()
                .map(|dt| dt.to_string())
                .unwrap_or_else(|| "Unknown".to_string());
            let etag = resp
                .e_tag()
                .unwrap_or("Unknown")
                .to_string();
            let content_type = resp
                .content_type()
                .unwrap_or("application/octet-stream")
                .to_string();

            Ok(S3FileMetadata {
                size,
                last_modified,
                etag,
                content_type,
            })
        }
        
        #[cfg(not(feature = "s3"))]
        {
            Err(S3Error::AwsError(
                "AWS SDK integration not enabled - compile with 's3' feature".to_string(),
            ))
        }
    }

    async fn read_from_cache(&self, _bucket: &str, _key: &str) -> Option<Vec<u8>> {
        // Placeholder for cache implementation
        None
    }

    async fn write_to_cache(&self, _bucket: &str, _key: &str, _data: &[u8]) -> Result<(), S3Error> {
        Ok(())
    }

    /// Get the configured region
    pub fn region(&self) -> &str {
        &self.region
    }

    /// Check if caching is enabled
    pub fn cache_enabled(&self) -> bool {
        self.cache_enabled
    }
}

/// File metadata from S3
#[derive(Debug, Clone)]
pub struct S3FileMetadata {
    /// File size in bytes
    pub size: u64,
    /// Last modified timestamp
    pub last_modified: String,
    /// ETag (file version identifier)
    pub etag: String,
    /// Content type
    pub content_type: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_s3_reader_creation() {
        let reader = S3Reader::new("us-east-1");
        assert!(reader.is_ok());

        let reader = reader.unwrap();
        assert_eq!(reader.region(), "us-east-1");
        assert!(!reader.cache_enabled());
    }

    #[test]
    fn test_invalid_region() {
        let reader = S3Reader::new("");
        assert!(reader.is_err());
    }

    #[tokio::test]
    async fn test_invalid_bucket() {
        let reader = S3Reader::new("us-east-1").unwrap();
        let result = reader.read_file("", "key.kore").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_invalid_key() {
        let reader = S3Reader::new("us-east-1").unwrap();
        let result = reader.read_file("bucket", "").await;
        assert!(result.is_err());
    }

    #[test]
    fn test_cache_configuration() {
        let mut reader = S3Reader::new("us-east-1").unwrap();
        assert!(!reader.cache_enabled());

        let result = reader.with_cache("./cache");
        assert!(result.is_ok());
        assert!(reader.cache_enabled());
    }

    #[test]
    fn test_invalid_cache_dir() {
        let mut reader = S3Reader::new("us-east-1").unwrap();
        let result = reader.with_cache("");
        assert!(result.is_err());
    }
}