Skip to main content

entrenar/storage/cloud/
gcs.rs

1//! Google Cloud Storage configuration
2
3use serde::{Deserialize, Serialize};
4
5/// Google Cloud Storage configuration
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct GCSConfig {
8    /// GCS bucket name
9    pub bucket: String,
10    /// Object prefix
11    pub prefix: String,
12    /// Project ID
13    pub project_id: Option<String>,
14    /// Service account JSON key path
15    pub service_account_key: Option<String>,
16}
17
18impl GCSConfig {
19    /// Create a new GCS configuration
20    pub fn new(bucket: &str) -> Self {
21        Self {
22            bucket: bucket.to_string(),
23            prefix: String::new(),
24            project_id: None,
25            service_account_key: None,
26        }
27    }
28
29    /// Set object prefix
30    pub fn with_prefix(mut self, prefix: &str) -> Self {
31        self.prefix = prefix.to_string();
32        self
33    }
34
35    /// Set project ID
36    pub fn with_project(mut self, project_id: &str) -> Self {
37        self.project_id = Some(project_id.to_string());
38        self
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_gcs_config_new() {
48        let config = GCSConfig::new("my-bucket");
49        assert_eq!(config.bucket, "my-bucket");
50    }
51
52    #[test]
53    fn test_gcs_config_with_project() {
54        let config = GCSConfig::new("bucket").with_project("my-project");
55        assert_eq!(config.project_id, Some("my-project".to_string()));
56    }
57
58    #[test]
59    fn test_gcs_config_with_prefix() {
60        let config = GCSConfig::new("bucket").with_prefix("models/v1/");
61        assert_eq!(config.prefix, "models/v1/");
62    }
63
64    #[test]
65    fn test_gcs_config_serde() {
66        let config = GCSConfig::new("bucket").with_prefix("artifacts/").with_project("my-project");
67
68        let json = serde_json::to_string(&config).expect("JSON serialization should succeed");
69        let parsed: GCSConfig =
70            serde_json::from_str(&json).expect("JSON deserialization should succeed");
71
72        assert_eq!(config.bucket, parsed.bucket);
73        assert_eq!(config.prefix, parsed.prefix);
74        assert_eq!(config.project_id, parsed.project_id);
75    }
76}