shardline-protocol-adapters 1.0.0

Shared protocol adapters for Git LFS, Bazel HTTP cache, and related object-key mapping.
Documentation
use shardline_protocol::RepositoryScope;
use shardline_storage::ObjectKey;

use crate::{ProtocolError, object_key, scope_namespace, validate_content_hash};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BazelCacheKind {
    Ac,
    Cas,
}

impl BazelCacheKind {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Ac => "ac",
            Self::Cas => "cas",
        }
    }
}

/// Returns the storage object key for a Bazel cache entry.
///
/// # Errors
///
/// Returns [`ProtocolError::InvalidContentHash`] when `hash_hex` is malformed
/// or the constructed key is invalid.
pub fn bazel_cache_object_key(
    kind: BazelCacheKind,
    hash_hex: &str,
    repository_scope: Option<&RepositoryScope>,
) -> Result<ObjectKey, ProtocolError> {
    validate_content_hash(hash_hex)?;
    object_key(&format!(
        "protocols/bazel/{}/{}/{}",
        scope_namespace(repository_scope),
        kind.as_str(),
        hash_hex
    ))
}