polykit_core/remote_cache/
config.rs

1//! Configuration for remote cache.
2
3use std::collections::BTreeSet;
4
5use serde::{Deserialize, Serialize};
6
7/// Configuration for remote cache.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct RemoteCacheConfig {
10    /// Backend URL (HTTP URL or filesystem path).
11    pub url: String,
12    /// Authentication token (for HTTP backends).
13    pub token: Option<String>,
14    /// Environment variables to include in cache keys.
15    ///
16    /// Only explicitly listed environment variables will be hashed into cache keys.
17    pub env_vars: BTreeSet<String>,
18    /// Input files to track for cache key generation.
19    ///
20    /// Patterns are relative to package root. Supports glob patterns.
21    pub input_files: Vec<String>,
22    /// Maximum artifact size in bytes (default: 1GB).
23    pub max_artifact_size: Option<u64>,
24    /// Read-only mode (disable uploads).
25    pub read_only: bool,
26}
27
28impl Default for RemoteCacheConfig {
29    fn default() -> Self {
30        Self {
31            url: String::new(),
32            token: None,
33            env_vars: BTreeSet::new(),
34            input_files: Vec::new(),
35            max_artifact_size: Some(1024 * 1024 * 1024), // 1GB
36            read_only: false,
37        }
38    }
39}
40
41impl RemoteCacheConfig {
42    /// Creates a new remote cache configuration.
43    pub fn new(url: impl Into<String>) -> Self {
44        Self {
45            url: url.into(),
46            ..Default::default()
47        }
48    }
49
50    /// Sets the authentication token.
51    pub fn with_token(mut self, token: impl Into<String>) -> Self {
52        self.token = Some(token.into());
53        self
54    }
55
56    /// Adds an environment variable to track.
57    pub fn add_env_var(mut self, var: impl Into<String>) -> Self {
58        self.env_vars.insert(var.into());
59        self
60    }
61
62    /// Adds multiple environment variables to track.
63    pub fn add_env_vars<I>(mut self, vars: I) -> Self
64    where
65        I: IntoIterator<Item = String>,
66    {
67        self.env_vars.extend(vars);
68        self
69    }
70
71    /// Adds an input file pattern to track.
72    pub fn add_input_file(mut self, pattern: impl Into<String>) -> Self {
73        self.input_files.push(pattern.into());
74        self
75    }
76
77    /// Sets read-only mode.
78    pub fn read_only(mut self, read_only: bool) -> Self {
79        self.read_only = read_only;
80        self
81    }
82
83    /// Sets maximum artifact size.
84    pub fn max_artifact_size(mut self, size: u64) -> Self {
85        self.max_artifact_size = Some(size);
86        self
87    }
88
89    /// Checks if this is an HTTP backend.
90    pub fn is_http(&self) -> bool {
91        self.url.starts_with("http://") || self.url.starts_with("https://")
92    }
93
94    /// Checks if this is a filesystem backend.
95    pub fn is_filesystem(&self) -> bool {
96        !self.is_http()
97    }
98}