Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #497 from cgwalters/image-reference-serializable
Browse files Browse the repository at this point in the history
container: Add an API to serialize `Transport`
  • Loading branch information
jmarrero authored Jul 6, 2023
2 parents e1de5a1 + 7e21bc6 commit e7e5d73
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions lib/src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,32 @@ impl TryFrom<&str> for Transport {

fn try_from(value: &str) -> Result<Self> {
Ok(match value {
"registry" | "docker" => Self::Registry,
"oci" => Self::OciDir,
"oci-archive" => Self::OciArchive,
"containers-storage" => Self::ContainerStorage,
Self::REGISTRY_STR | "docker" => Self::Registry,
Self::OCI_STR => Self::OciDir,
Self::OCI_ARCHIVE_STR => Self::OciArchive,
Self::CONTAINERS_STORAGE_STR => Self::ContainerStorage,
o => return Err(anyhow!("Unknown transport '{}'", o)),
})
}
}

impl Transport {
const OCI_STR: &str = "oci";
const OCI_ARCHIVE_STR: &str = "oci-archive";
const CONTAINERS_STORAGE_STR: &str = "containers-storage";
const REGISTRY_STR: &str = "registry";

/// Retrieve an identifier that can then be re-parsed from [`Transport::try_from::<&str>`].
pub fn serializable_name(&self) -> &'static str {
match self {
Transport::Registry => Self::REGISTRY_STR,
Transport::OciDir => Self::OCI_STR,
Transport::OciArchive => Self::OCI_ARCHIVE_STR,
Transport::ContainerStorage => Self::CONTAINERS_STORAGE_STR,
}
}
}

impl TryFrom<&str> for ImageReference {
type Error = anyhow::Error;

Expand Down Expand Up @@ -428,6 +445,18 @@ mod tests {

use super::*;

#[test]
fn test_serializable_transport() {
for v in [
Transport::Registry,
Transport::ContainerStorage,
Transport::OciArchive,
Transport::OciDir,
] {
assert_eq!(Transport::try_from(v.serializable_name()).unwrap(), v);
}
}

const INVALID_IRS: &[&str] = &["", "foo://", "docker:blah", "registry:", "foo:bar"];
const VALID_IRS: &[&str] = &[
"containers-storage:localhost/someimage",
Expand Down

0 comments on commit e7e5d73

Please sign in to comment.