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

container: Add an API to serialize Transport #497

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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