Skip to content

Commit

Permalink
Add MediaType::to_docker_v2s2 for Docker Image Manifest V2 Schema 2…
Browse files Browse the repository at this point in the history
… equivalents

Signed-off-by: Nokome Bentley <nokome@stenci.la>
  • Loading branch information
nokome committed Jun 8, 2022
1 parent 6df620e commit b6d21f0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit b6d21f0

Please sign in to comment.