use crate::Sha256Hash;
#[derive(Debug, Clone)]
pub enum Artifact<'a> {
Bytes(&'a [u8]),
Digest(Sha256Hash),
}
impl<'a> Artifact<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Self {
Artifact::Bytes(bytes)
}
pub fn from_digest(digest: Sha256Hash) -> Self {
Artifact::Digest(digest)
}
pub fn has_bytes(&self) -> bool {
matches!(self, Artifact::Bytes(_))
}
pub fn bytes(&self) -> Option<&[u8]> {
match self {
Artifact::Bytes(bytes) => Some(bytes),
Artifact::Digest(_) => None,
}
}
pub fn pre_computed_digest(&self) -> Option<Sha256Hash> {
match self {
Artifact::Bytes(_) => None,
Artifact::Digest(hash) => Some(*hash),
}
}
}
impl<'a> From<&'a [u8]> for Artifact<'a> {
fn from(bytes: &'a [u8]) -> Self {
Artifact::Bytes(bytes)
}
}
impl<'a> From<&'a Vec<u8>> for Artifact<'a> {
fn from(bytes: &'a Vec<u8>) -> Self {
Artifact::Bytes(bytes.as_slice())
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for Artifact<'a> {
fn from(bytes: &'a [u8; N]) -> Self {
Artifact::Bytes(bytes.as_slice())
}
}
impl From<Sha256Hash> for Artifact<'static> {
fn from(hash: Sha256Hash) -> Self {
Artifact::Digest(hash)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_artifact_from_bytes() {
let bytes = b"hello world";
let artifact = Artifact::from(bytes.as_slice());
assert!(artifact.has_bytes());
assert_eq!(artifact.bytes(), Some(bytes.as_slice()));
}
#[test]
fn test_artifact_from_digest() {
let digest = Sha256Hash::from_hex(
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
)
.unwrap();
let artifact = Artifact::from(digest);
assert!(!artifact.has_bytes());
assert_eq!(artifact.bytes(), None);
}
}