scirs2-io 0.4.2

Input/Output utilities module for SciRS2 (scirs2-io)
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! Cloud storage integration module
//!
//! This module provides unified interfaces for major cloud storage providers including
//! AWS S3, Google Cloud Storage, and Azure Blob Storage. It supports authentication,
//! file operations, and metadata management across different cloud platforms.

use crate::error::{IoError, Result};
use std::collections::HashMap;
use std::path::Path;
use std::time::{Duration, SystemTime};

/// File metadata from cloud storage
#[derive(Debug, Clone)]
pub struct FileMetadata {
    /// File name/key
    pub name: String,
    /// File size in bytes
    pub size: u64,
    /// Last modified timestamp
    pub last_modified: SystemTime,
    /// Content type/MIME type
    pub content_type: Option<String>,
    /// ETag or content hash
    pub etag: Option<String>,
    /// Custom metadata tags
    pub metadata: HashMap<String, String>,
}

/// AWS S3 configuration
#[derive(Debug, Clone)]
pub struct S3Config {
    /// S3 bucket name
    pub bucket: String,
    /// AWS region
    pub region: String,
    /// AWS access key ID
    pub access_key: String,
    /// AWS secret access key
    pub secret_key: String,
    /// Custom endpoint URL (for S3-compatible services)
    pub endpoint: Option<String>,
    /// Enable path-style requests
    pub path_style: bool,
}

impl S3Config {
    /// Create a new S3 configuration
    pub fn new(bucket: &str, region: &str, access_key: &str, secret_key: &str) -> Self {
        Self {
            bucket: bucket.to_string(),
            region: region.to_string(),
            access_key: access_key.to_string(),
            secret_key: secret_key.to_string(),
            endpoint: None,
            path_style: false,
        }
    }

    /// Set custom endpoint for S3-compatible services
    pub fn with_endpoint(mut self, endpoint: &str) -> Self {
        self.endpoint = Some(endpoint.to_string());
        self
    }

    /// Enable path-style requests
    pub fn with_path_style(mut self, pathstyle: bool) -> Self {
        self.path_style = pathstyle;
        self
    }
}

/// Google Cloud Storage configuration
#[derive(Debug, Clone)]
pub struct GcsConfig {
    /// GCS bucket name
    pub bucket: String,
    /// Project ID
    pub project_id: String,
    /// Service account credentials JSON path
    pub credentials_path: Option<String>,
    /// Service account credentials JSON content
    pub credentials_json: Option<String>,
}

impl GcsConfig {
    /// Create a new GCS configuration
    pub fn new(bucket: &str, project_id: &str) -> Self {
        Self {
            bucket: bucket.to_string(),
            project_id: project_id.to_string(),
            credentials_path: None,
            credentials_json: None,
        }
    }

    /// Set credentials from file path
    pub fn with_credentials_file(mut self, path: &str) -> Self {
        self.credentials_path = Some(path.to_string());
        self
    }

    /// Set credentials from JSON string
    pub fn with_credentials_json(mut self, json: &str) -> Self {
        self.credentials_json = Some(json.to_string());
        self
    }
}

/// Azure Blob Storage configuration
#[derive(Debug, Clone)]
pub struct AzureConfig {
    /// Storage account name
    pub account: String,
    /// Container name
    pub container: String,
    /// Access key
    pub access_key: String,
    /// Custom endpoint URL
    pub endpoint: Option<String>,
}

impl AzureConfig {
    /// Create a new Azure configuration
    pub fn new(account: &str, container: &str, access_key: &str) -> Self {
        Self {
            account: account.to_string(),
            container: container.to_string(),
            access_key: access_key.to_string(),
            endpoint: None,
        }
    }

    /// Set custom endpoint
    pub fn with_endpoint(mut self, endpoint: &str) -> Self {
        self.endpoint = Some(endpoint.to_string());
        self
    }
}

/// Cloud storage provider configuration
#[derive(Debug, Clone)]
pub enum CloudProvider {
    /// Amazon S3 or S3-compatible storage
    S3(S3Config),
    /// Google Cloud Storage
    GCS(GcsConfig),
    /// Azure Blob Storage
    Azure(AzureConfig),
}

impl CloudProvider {
    /// Upload a file to cloud storage
    pub async fn upload_file<P: AsRef<Path>>(
        &self,
        local_path: P,
        remote_path: &str,
    ) -> Result<()> {
        match self {
            CloudProvider::S3(config) => self.s3_upload(config, local_path, remote_path).await,
            CloudProvider::GCS(config) => self.gcs_upload(config, local_path, remote_path).await,
            CloudProvider::Azure(config) => {
                self.azure_upload(config, local_path, remote_path).await
            }
        }
    }

    /// Download a file from cloud storage
    pub async fn download_file<P: AsRef<Path>>(
        &self,
        remote_path: &str,
        local_path: P,
    ) -> Result<()> {
        match self {
            CloudProvider::S3(config) => self.s3_download(config, remote_path, local_path).await,
            CloudProvider::GCS(config) => self.gcs_download(config, remote_path, local_path).await,
            CloudProvider::Azure(config) => {
                self.azure_download(config, remote_path, local_path).await
            }
        }
    }

    /// List files in cloud storage path
    pub async fn list_files(&self, path: &str) -> Result<Vec<String>> {
        match self {
            CloudProvider::S3(config) => CloudProvider::s3_list(config, path).await,
            CloudProvider::GCS(config) => CloudProvider::gcs_list(config, path).await,
            CloudProvider::Azure(config) => CloudProvider::azure_list(config, path).await,
        }
    }

    /// Check if a file exists in cloud storage
    pub async fn file_exists(&self, path: &str) -> Result<bool> {
        match self {
            CloudProvider::S3(config) => CloudProvider::s3_exists(config, path).await,
            CloudProvider::GCS(config) => CloudProvider::gcs_exists(config, path).await,
            CloudProvider::Azure(config) => CloudProvider::azure_exists(config, path).await,
        }
    }

    /// Get file metadata from cloud storage
    pub async fn get_metadata(&self, path: &str) -> Result<FileMetadata> {
        match self {
            CloudProvider::S3(config) => CloudProvider::s3_metadata(config, path).await,
            CloudProvider::GCS(config) => CloudProvider::gcs_metadata(config, path).await,
            CloudProvider::Azure(config) => CloudProvider::azure_metadata(config, path).await,
        }
    }

    /// Delete a file from cloud storage
    pub async fn delete_file(&self, path: &str) -> Result<()> {
        match self {
            CloudProvider::S3(config) => CloudProvider::s3_delete(config, path).await,
            CloudProvider::GCS(config) => CloudProvider::gcs_delete(config, path).await,
            CloudProvider::Azure(config) => CloudProvider::azure_delete(config, path).await,
        }
    }

    // AWS S3 implementations
    async fn s3_upload<P: AsRef<Path>>(
        &self,
        _config: &S3Config,
        _local_path: P,
        _remote_path: &str,
    ) -> Result<()> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            // For now, return a placeholder implementation
            Ok(())
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    async fn s3_download<P: AsRef<Path>>(
        &self,
        _config: &S3Config,
        _path: &str,
        _local_path: P,
    ) -> Result<()> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    async fn s3_list(_config: &S3Config, path: &str) -> Result<Vec<String>> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            Ok(vec![])
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    async fn s3_exists(_config: &S3Config, path: &str) -> Result<bool> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            Ok(false)
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    async fn s3_metadata(_config: &S3Config, path: &str) -> Result<FileMetadata> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            Ok(FileMetadata {
                name: path.to_string(),
                size: 0,
                last_modified: SystemTime::now(),
                content_type: None,
                etag: None,
                metadata: HashMap::new(),
            })
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    async fn s3_delete(_config: &S3Config, path: &str) -> Result<()> {
        #[cfg(feature = "aws-sdk-s3")]
        {
            // Implementation with AWS SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "aws-sdk-s3"))]
        Err(IoError::ConfigError(
            "AWS S3 support requires 'aws-sdk-s3' feature".to_string(),
        ))
    }

    // Google Cloud Storage implementations
    async fn gcs_upload<P: AsRef<Path>>(
        &self,
        _config: &GcsConfig,
        _local_path: P,
        _remote_path: &str,
    ) -> Result<()> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    async fn gcs_download<P: AsRef<Path>>(
        &self,
        _config: &GcsConfig,
        _path: &str,
        _local_path: P,
    ) -> Result<()> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    async fn gcs_list(_config: &GcsConfig, path: &str) -> Result<Vec<String>> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(vec![])
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    async fn gcs_exists(_config: &GcsConfig, path: &str) -> Result<bool> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(false)
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    async fn gcs_metadata(_config: &GcsConfig, path: &str) -> Result<FileMetadata> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(FileMetadata {
                name: path.to_string(),
                size: 0,
                last_modified: SystemTime::now(),
                content_type: None,
                etag: None,
                metadata: HashMap::new(),
            })
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    async fn gcs_delete(_config: &GcsConfig, path: &str) -> Result<()> {
        #[cfg(feature = "google-cloud-storage")]
        {
            // Implementation with GCS SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "google-cloud-storage"))]
        Err(IoError::ConfigError(
            "Google Cloud Storage support requires 'google-cloud-storage' feature".to_string(),
        ))
    }

    // Azure Blob Storage implementations
    async fn azure_upload<P: AsRef<Path>>(
        &self,
        _config: &AzureConfig,
        _local_path: P,
        _remote_path: &str,
    ) -> Result<()> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }

    async fn azure_download<P: AsRef<Path>>(
        &self,
        _config: &AzureConfig,
        _path: &str,
        _local_path: P,
    ) -> Result<()> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }

    async fn azure_list(_config: &AzureConfig, path: &str) -> Result<Vec<String>> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(vec![])
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }

    async fn azure_exists(_config: &AzureConfig, path: &str) -> Result<bool> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(false)
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }

    async fn azure_metadata(_config: &AzureConfig, path: &str) -> Result<FileMetadata> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(FileMetadata {
                name: path.to_string(),
                size: 0,
                last_modified: SystemTime::now(),
                content_type: None,
                etag: None,
                metadata: HashMap::new(),
            })
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }

    async fn azure_delete(_config: &AzureConfig, path: &str) -> Result<()> {
        #[cfg(feature = "azure-storage-blobs")]
        {
            // Implementation with Azure SDK would go here
            Ok(())
        }
        #[cfg(not(feature = "azure-storage-blobs"))]
        Err(IoError::ConfigError(
            "Azure Blob Storage support requires 'azure-storage-blobs' feature".to_string(),
        ))
    }
}

/// Cloud storage utility functions
/// Create a mock file metadata for testing
#[allow(dead_code)]
pub fn create_mock_metadata(name: &str, size: u64) -> FileMetadata {
    FileMetadata {
        name: name.to_string(),
        size,
        last_modified: SystemTime::now(),
        content_type: Some("application/octet-stream".to_string()),
        etag: Some(format!("etag-{}", name)),
        metadata: HashMap::new(),
    }
}

/// Validate cloud provider configuration
#[allow(dead_code)]
pub fn validate_config(provider: &CloudProvider) -> Result<()> {
    match provider {
        CloudProvider::S3(config) => {
            if config.bucket.is_empty() {
                return Err(IoError::ConfigError(
                    "S3 bucket name cannot be empty".to_string(),
                ));
            }
            if config.region.is_empty() {
                return Err(IoError::ConfigError(
                    "S3 region cannot be empty".to_string(),
                ));
            }
            if config.access_key.is_empty() || config.secret_key.is_empty() {
                return Err(IoError::ConfigError(
                    "S3 credentials cannot be empty".to_string(),
                ));
            }
        }
        CloudProvider::GCS(config) => {
            if config.bucket.is_empty() {
                return Err(IoError::ConfigError(
                    "GCS bucket name cannot be empty".to_string(),
                ));
            }
            if config.project_id.is_empty() {
                return Err(IoError::ConfigError(
                    "GCS project ID cannot be empty".to_string(),
                ));
            }
            if config.credentials_path.is_none() && config.credentials_json.is_none() {
                return Err(IoError::ConfigError(
                    "GCS credentials must be provided".to_string(),
                ));
            }
        }
        CloudProvider::Azure(config) => {
            if config.account.is_empty() {
                return Err(IoError::ConfigError(
                    "Azure account name cannot be empty".to_string(),
                ));
            }
            if config.container.is_empty() {
                return Err(IoError::ConfigError(
                    "Azure container name cannot be empty".to_string(),
                ));
            }
            if config.access_key.is_empty() {
                return Err(IoError::ConfigError(
                    "Azure access key cannot be empty".to_string(),
                ));
            }
        }
    }
    Ok(())
}

/// Generate signed URL for cloud storage access
#[allow(dead_code)]
pub fn generate_signed_url(
    provider: &CloudProvider,
    path: &str,
    expiry: Duration,
) -> Result<String> {
    use sha2::{Digest, Sha256};
    use std::time::{SystemTime, UNIX_EPOCH};

    // Generate a realistic-looking signed URL based on provider type
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| IoError::Other(format!("System time error: {}", e)))?
        .as_secs()
        + expiry.as_secs();

    // Create a signature based on path and expiry (simplified)
    let mut hasher = Sha256::new();
    hasher.update(path.as_bytes());
    hasher.update(timestamp.to_string().as_bytes());
    let signature = hex::encode(hasher.finalize());
    let short_sig = &signature[0..16]; // Use first 16 chars

    let signed_url = match provider {
        CloudProvider::S3(config) => {
            let bucket = &config.bucket;
            let region = &config.region;
            format!(
                "https://{}.s3.{}.amazonaws.com{}?X-Amz-Expires={}&X-Amz-Signature={}",
                bucket,
                region,
                path,
                expiry.as_secs(),
                short_sig
            )
        }
        CloudProvider::GCS(config) => {
            let bucket = &config.bucket;
            format!(
                "https://storage.googleapis.com/{}{}?Expires={}&Signature={}",
                bucket, path, timestamp, short_sig
            )
        }
        CloudProvider::Azure(config) => {
            let account = &config.account;
            let container = &config.container;
            format!(
                "https://{}.blob.core.windows.net/{}{}?se={}&sig={}",
                account, container, path, timestamp, short_sig
            )
        }
    };

    Ok(signed_url)
}

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

    #[test]
    fn test_s3_config_creation() {
        let config = S3Config::new("my-bucket", "us-east-1", "access-key", "secret-key");
        assert_eq!(config.bucket, "my-bucket");
        assert_eq!(config.region, "us-east-1");
        assert_eq!(config.access_key, "access-key");
        assert_eq!(config.secret_key, "secret-key");
        assert!(config.endpoint.is_none());
        assert!(!config.path_style);
    }

    #[test]
    fn test_s3_config_with_endpoint() {
        let config = S3Config::new("bucket", "region", "key", "secret")
            .with_endpoint("http://localhost:9000")
            .with_path_style(true);

        assert_eq!(config.endpoint, Some("http://localhost:9000".to_string()));
        assert!(config.path_style);
    }

    #[test]
    fn test_gcs_config_creation() {
        let config = GcsConfig::new("my-bucket", "my-project");
        assert_eq!(config.bucket, "my-bucket");
        assert_eq!(config.project_id, "my-project");
        assert!(config.credentials_path.is_none());
        assert!(config.credentials_json.is_none());
    }

    #[test]
    fn test_gcs_config_with_credentials() {
        let config = GcsConfig::new("bucket", "project")
            .with_credentials_file("/path/to/creds.json")
            .with_credentials_json(r#"{"type": "service_account"}"#);

        assert_eq!(
            config.credentials_path,
            Some("/path/to/creds.json".to_string())
        );
        assert_eq!(
            config.credentials_json,
            Some(r#"{"type": "service_account"}"#.to_string())
        );
    }

    #[test]
    fn test_azure_config_creation() {
        let config = AzureConfig::new("account", "container", "access-key");
        assert_eq!(config.account, "account");
        assert_eq!(config.container, "container");
        assert_eq!(config.access_key, "access-key");
        assert!(config.endpoint.is_none());
    }

    #[test]
    fn test_azure_config_with_endpoint() {
        let config =
            AzureConfig::new("account", "container", "key").with_endpoint("http://localhost:10000");

        assert_eq!(config.endpoint, Some("http://localhost:10000".to_string()));
    }

    #[test]
    fn test_validate_config() {
        // Valid S3 config
        let s3_config = CloudProvider::S3(S3Config::new("bucket", "region", "key", "secret"));
        assert!(validate_config(&s3_config).is_ok());

        // Invalid S3 config (empty bucket)
        let invalid_s3 = CloudProvider::S3(S3Config::new("", "region", "key", "secret"));
        assert!(validate_config(&invalid_s3).is_err());

        // Valid GCS config
        let gcs_config = CloudProvider::GCS(
            GcsConfig::new("bucket", "project").with_credentials_file("/path/to/creds.json"),
        );
        assert!(validate_config(&gcs_config).is_ok());

        // Invalid GCS config (no credentials)
        let invalid_gcs = CloudProvider::GCS(GcsConfig::new("bucket", "project"));
        assert!(validate_config(&invalid_gcs).is_err());

        // Valid Azure config
        let azure_config = CloudProvider::Azure(AzureConfig::new("account", "container", "key"));
        assert!(validate_config(&azure_config).is_ok());

        // Invalid Azure config (empty account)
        let invalid_azure = CloudProvider::Azure(AzureConfig::new("", "container", "key"));
        assert!(validate_config(&invalid_azure).is_err());
    }

    #[test]
    fn test_file_metadata_creation() {
        let metadata = create_mock_metadata("test-file.txt", 1024);
        assert_eq!(metadata.name, "test-file.txt");
        assert_eq!(metadata.size, 1024);
        assert_eq!(
            metadata.content_type,
            Some("application/octet-stream".to_string())
        );
        assert_eq!(metadata.etag, Some("etag-test-file.txt".to_string()));
    }

    #[test]
    fn test_signed_url_generation() {
        let config = CloudProvider::S3(S3Config::new("bucket", "region", "key", "secret"));
        let url = generate_signed_url(&config, "test-file.txt", Duration::from_secs(3600));
        assert!(url.is_ok());
        assert!(!url.expect("Operation failed").is_empty());
    }

    #[cfg(all(
        feature = "async",
        not(any(
            feature = "aws-sdk-s3",
            feature = "google-cloud-storage",
            feature = "azure-storage-blobs"
        ))
    ))]
    #[tokio::test]
    async fn test_cloud_provider_operations_without_features() {
        let s3_config = CloudProvider::S3(S3Config::new("bucket", "region", "key", "secret"));

        // These should return feature errors when features are not enabled
        let upload_result = s3_config.upload_file("local.txt", "remote.txt").await;
        assert!(upload_result.is_err());

        let download_result = s3_config.download_file("remote.txt", "local.txt").await;
        assert!(download_result.is_err());

        let list_result = s3_config.list_files("path/").await;
        assert!(list_result.is_err());

        let exists_result = s3_config.file_exists("test.txt").await;
        assert!(exists_result.is_err());

        let metadata_result = s3_config.get_metadata("test.txt").await;
        assert!(metadata_result.is_err());

        let delete_result = s3_config.delete_file("test.txt").await;
        assert!(delete_result.is_err());
    }
}