use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkloadShape {
OciImage,
NixClosure,
Qcow2,
Wasm,
StaticBinary {
triple: String,
},
HelmChart,
Custom {
name: String,
},
}
impl WorkloadShape {
#[must_use]
pub fn from_realisation(_r: &crate::derivation::Realisation) -> Self {
WorkloadShape::NixClosure
}
#[must_use]
pub fn tag(&self) -> String {
match self {
Self::OciImage => "oci_image".into(),
Self::NixClosure => "nix_closure".into(),
Self::Qcow2 => "qcow2".into(),
Self::Wasm => "wasm".into(),
Self::StaticBinary { triple } => format!("static_binary:{triple}"),
Self::HelmChart => "helm_chart".into(),
Self::Custom { name } => format!("custom:{name}"),
}
}
#[must_use]
pub fn shape_hash(&self, drv_hash_hex: &str) -> [u8; 32] {
let composed = format!("{}|{drv_hash_hex}", self.tag());
*blake3::hash(composed.as_bytes()).as_bytes()
}
#[must_use]
pub fn file_extension(&self) -> Option<&'static str> {
match self {
Self::OciImage => Some("tar"),
Self::NixClosure => None, Self::Qcow2 => Some("qcow2"),
Self::Wasm => Some("wasm"),
Self::StaticBinary { .. } => None, Self::HelmChart => Some("tgz"),
Self::Custom { .. } => None,
}
}
#[must_use]
pub fn media_type(&self) -> Option<&'static str> {
match self {
Self::OciImage => Some("application/vnd.oci.image.manifest.v1+json"),
Self::Wasm => Some("application/wasm"),
Self::HelmChart => Some("application/vnd.cncf.helm.chart.content.v1.tar+gzip"),
_ => None,
}
}
}
impl std::fmt::Display for WorkloadShape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.tag())
}
}
#[async_trait::async_trait]
pub trait ShapeRenderer: Send + Sync {
fn name(&self) -> &'static str;
fn shape(&self) -> WorkloadShape;
async fn render(&self, drv: &crate::derivation::Drv) -> Result<RenderedArtifact, ShapeError>;
}
#[derive(Clone, Debug)]
pub struct RenderedArtifact {
pub shape: WorkloadShape,
pub bytes: Vec<u8>,
pub evidence_hash: [u8; 32],
}
impl RenderedArtifact {
#[must_use]
pub fn from_bytes(shape: WorkloadShape, bytes: Vec<u8>) -> Self {
let evidence_hash = *blake3::hash(&bytes).as_bytes();
Self {
shape,
bytes,
evidence_hash,
}
}
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum ShapeError {
#[error("backend: {0}")]
Backend(String),
#[error("invalid drv: {0}")]
InvalidDrv(String),
#[error("unsupported shape: {0}")]
UnsupportedShape(String),
}
crate::impl_error_kind! {
ShapeError {
(Backend(_)) => "backend",
(InvalidDrv(_)) => "invalid_drv",
(UnsupportedShape(_)) => "unsupported_shape",
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tags_are_stable_snake_case() {
assert_eq!(WorkloadShape::OciImage.tag(), "oci_image");
assert_eq!(WorkloadShape::NixClosure.tag(), "nix_closure");
assert_eq!(WorkloadShape::Qcow2.tag(), "qcow2");
assert_eq!(WorkloadShape::Wasm.tag(), "wasm");
assert_eq!(WorkloadShape::HelmChart.tag(), "helm_chart");
}
#[test]
fn parametric_shapes_include_param_in_tag() {
let s = WorkloadShape::StaticBinary {
triple: "x86_64-unknown-linux-musl".into(),
};
assert_eq!(s.tag(), "static_binary:x86_64-unknown-linux-musl");
let c = WorkloadShape::Custom {
name: "openraft-state".into(),
};
assert_eq!(c.tag(), "custom:openraft-state");
}
#[test]
fn display_matches_tag() {
let s = WorkloadShape::OciImage;
assert_eq!(format!("{s}"), s.tag());
}
#[test]
fn shape_hash_is_deterministic() {
let s = WorkloadShape::OciImage;
let h1 = s.shape_hash("abc123");
let h2 = s.shape_hash("abc123");
assert_eq!(h1, h2);
}
#[test]
fn shape_hash_diverges_per_shape() {
let drv_hash = "abc123";
let oci = WorkloadShape::OciImage.shape_hash(drv_hash);
let qcow = WorkloadShape::Qcow2.shape_hash(drv_hash);
assert_ne!(oci, qcow);
}
#[test]
fn shape_hash_diverges_per_drv() {
let s = WorkloadShape::OciImage;
let h1 = s.shape_hash("abc");
let h2 = s.shape_hash("def");
assert_ne!(h1, h2);
}
#[test]
fn file_extension_correctness() {
assert_eq!(WorkloadShape::OciImage.file_extension(), Some("tar"));
assert_eq!(WorkloadShape::Qcow2.file_extension(), Some("qcow2"));
assert_eq!(WorkloadShape::Wasm.file_extension(), Some("wasm"));
assert_eq!(WorkloadShape::HelmChart.file_extension(), Some("tgz"));
assert_eq!(WorkloadShape::NixClosure.file_extension(), None);
assert_eq!(
WorkloadShape::StaticBinary { triple: "x".into() }.file_extension(),
None
);
}
#[test]
fn media_type_correctness() {
assert_eq!(
WorkloadShape::OciImage.media_type(),
Some("application/vnd.oci.image.manifest.v1+json")
);
assert_eq!(WorkloadShape::Wasm.media_type(), Some("application/wasm"));
assert_eq!(WorkloadShape::Qcow2.media_type(), None);
assert_eq!(WorkloadShape::NixClosure.media_type(), None);
}
#[test]
fn shape_round_trips_via_serde() {
for shape in [
WorkloadShape::OciImage,
WorkloadShape::NixClosure,
WorkloadShape::Qcow2,
WorkloadShape::Wasm,
WorkloadShape::StaticBinary {
triple: "aarch64-apple-darwin".into(),
},
WorkloadShape::HelmChart,
WorkloadShape::Custom {
name: "custom-thing".into(),
},
] {
let bytes = serde_json::to_vec(&shape).unwrap();
let back: WorkloadShape = serde_json::from_slice(&bytes).unwrap();
assert_eq!(back, shape);
}
}
#[test]
fn rendered_artifact_from_bytes_computes_hash() {
let a = RenderedArtifact::from_bytes(WorkloadShape::Wasm, b"hello".to_vec());
assert_eq!(a.bytes, b"hello");
assert_eq!(a.evidence_hash, *blake3::hash(b"hello").as_bytes());
assert_eq!(a.shape, WorkloadShape::Wasm);
}
#[test]
fn from_realisation_projects_to_nix_closure() {
use crate::derivation::{DrvHash, OutputPath, Realisation};
let r = Realisation {
drv_hash: DrvHash::from_bytes(b"d"),
output_name: "out".into(),
output_path: OutputPath::new("/nix/store/a-out"),
nar_hash: None,
};
assert_eq!(
WorkloadShape::from_realisation(&r),
WorkloadShape::NixClosure
);
}
#[test]
fn shape_error_kinds_are_stable() {
assert_eq!(ShapeError::Backend("x".into()).kind(), "backend");
assert_eq!(ShapeError::InvalidDrv("x".into()).kind(), "invalid_drv");
assert_eq!(
ShapeError::UnsupportedShape("x".into()).kind(),
"unsupported_shape"
);
}
}