sigstore_types/
artifact.rs1use crate::Sha256Hash;
8
9#[derive(Debug, Clone)]
30pub enum Artifact<'a> {
31 Bytes(&'a [u8]),
33 Digest(Sha256Hash),
35}
36
37impl<'a> Artifact<'a> {
38 pub fn from_bytes(bytes: &'a [u8]) -> Self {
40 Artifact::Bytes(bytes)
41 }
42
43 pub fn from_digest(digest: Sha256Hash) -> Self {
45 Artifact::Digest(digest)
46 }
47
48 pub fn has_bytes(&self) -> bool {
50 matches!(self, Artifact::Bytes(_))
51 }
52
53 pub fn bytes(&self) -> Option<&[u8]> {
55 match self {
56 Artifact::Bytes(bytes) => Some(bytes),
57 Artifact::Digest(_) => None,
58 }
59 }
60
61 pub fn pre_computed_digest(&self) -> Option<Sha256Hash> {
63 match self {
64 Artifact::Bytes(_) => None,
65 Artifact::Digest(hash) => Some(*hash),
66 }
67 }
68}
69
70impl<'a> From<&'a [u8]> for Artifact<'a> {
71 fn from(bytes: &'a [u8]) -> Self {
72 Artifact::Bytes(bytes)
73 }
74}
75
76impl<'a> From<&'a Vec<u8>> for Artifact<'a> {
77 fn from(bytes: &'a Vec<u8>) -> Self {
78 Artifact::Bytes(bytes.as_slice())
79 }
80}
81
82impl<'a, const N: usize> From<&'a [u8; N]> for Artifact<'a> {
83 fn from(bytes: &'a [u8; N]) -> Self {
84 Artifact::Bytes(bytes.as_slice())
85 }
86}
87
88impl From<Sha256Hash> for Artifact<'static> {
89 fn from(hash: Sha256Hash) -> Self {
90 Artifact::Digest(hash)
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_artifact_from_bytes() {
100 let bytes = b"hello world";
101 let artifact = Artifact::from(bytes.as_slice());
102 assert!(artifact.has_bytes());
103 assert_eq!(artifact.bytes(), Some(bytes.as_slice()));
104 }
105
106 #[test]
107 fn test_artifact_from_digest() {
108 let digest = Sha256Hash::from_hex(
109 "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
110 )
111 .unwrap();
112 let artifact = Artifact::from(digest);
113 assert!(!artifact.has_bytes());
114 assert_eq!(artifact.bytes(), None);
115 }
116}