Skip to main content

opal/model/
cache.rs

1use std::path::PathBuf;
2
3#[derive(Debug, Clone)]
4pub struct CacheSpec {
5    pub key: CacheKeySpec,
6    pub fallback_keys: Vec<String>,
7    pub paths: Vec<PathBuf>,
8    pub policy: CachePolicySpec,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum CacheKeySpec {
13    Literal(String),
14    Files {
15        files: Vec<PathBuf>,
16        prefix: Option<String>,
17    },
18}
19
20impl Default for CacheKeySpec {
21    fn default() -> Self {
22        Self::Literal("default".to_string())
23    }
24}
25
26impl CacheKeySpec {
27    pub fn describe(&self) -> String {
28        match self {
29            CacheKeySpec::Literal(value) => value.clone(),
30            CacheKeySpec::Files { files, prefix } => {
31                let files_text = files
32                    .iter()
33                    .map(|path| path.display().to_string())
34                    .collect::<Vec<_>>()
35                    .join(", ");
36                if let Some(prefix) = prefix {
37                    format!("{{ files: [{files_text}], prefix: {prefix} }}")
38                } else {
39                    format!("{{ files: [{files_text}] }}")
40                }
41            }
42        }
43    }
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum CachePolicySpec {
48    Pull,
49    Push,
50    PullPush,
51}
52
53impl CachePolicySpec {
54    pub fn allows_pull(self) -> bool {
55        matches!(self, Self::Pull | Self::PullPush)
56    }
57
58    pub fn allows_push(self) -> bool {
59        matches!(self, Self::Push | Self::PullPush)
60    }
61}