From b6d21f0f0ef4bad86cac080b510af465a5a5b5c4 Mon Sep 17 00:00:00 2001 From: Nokome Bentley Date: Tue, 7 Jun 2022 19:58:47 +1200 Subject: [PATCH] Add `MediaType::to_docker_v2s2` for Docker Image Manifest V2 Schema 2 equivalents Signed-off-by: Nokome Bentley --- src/image/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/image/mod.rs b/src/image/mod.rs index 72acec17e9..2c63a067d7 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -109,6 +109,31 @@ impl From<&str> for MediaType { } } +/// Trait to get the Docker Image Manifest V2 Schema 2 media type for an OCI media type +/// +/// This may be necessary for compatibility with tools that do not recognize the OCI Media Types. +/// Where a [`MediaType`] is expected you can use `MediaType::ImageManifest.to_v2s2()?` instead and +/// `impl From<&str> for MediaType` will create a [`MediaType::Other`] for it. +/// +/// Not all OCI Media Types have an equivalent Docker V2S2 Media Type. In those cases, `to_v2s2` will error. +pub trait ToV2S2 { + /// Get the [Docker Image Manifest V2 Schema 2](https://docs.docker.com/registry/spec/manifest-v2-2/) + /// media type equivalent for an OCI media type + fn to_v2s2(&self) -> Result<&str, std::fmt::Error>; +} + +impl ToV2S2 for MediaType { + fn to_v2s2(&self) -> Result<&str, std::fmt::Error> { + Ok(match self { + Self::ImageIndex => "application/vnd.docker.distribution.manifest.list.v2+json", + Self::ImageManifest => "application/vnd.docker.distribution.manifest.v2+json", + Self::ImageConfig => "application/vnd.docker.container.image.v1+json", + Self::ImageLayerGzip => "application/vnd.docker.image.rootfs.diff.tar.gzip", + _ => return Err(std::fmt::Error), + }) + } +} + impl Serialize for MediaType { fn serialize(&self, serializer: S) -> Result where