Skip to main content

shardline_protocol_adapters/
bazel.rs

1use shardline_protocol::RepositoryScope;
2use shardline_storage::ObjectKey;
3
4use crate::{ProtocolError, object_key, scope_namespace, validate_content_hash};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum BazelCacheKind {
8    Ac,
9    Cas,
10}
11
12impl BazelCacheKind {
13    const fn as_str(self) -> &'static str {
14        match self {
15            Self::Ac => "ac",
16            Self::Cas => "cas",
17        }
18    }
19}
20
21/// Returns the storage object key for a Bazel cache entry.
22///
23/// # Errors
24///
25/// Returns [`ProtocolError::InvalidContentHash`] when `hash_hex` is malformed
26/// or the constructed key is invalid.
27pub fn bazel_cache_object_key(
28    kind: BazelCacheKind,
29    hash_hex: &str,
30    repository_scope: Option<&RepositoryScope>,
31) -> Result<ObjectKey, ProtocolError> {
32    validate_content_hash(hash_hex)?;
33    object_key(&format!(
34        "protocols/bazel/{}/{}/{}",
35        scope_namespace(repository_scope),
36        kind.as_str(),
37        hash_hex
38    ))
39}