From 898bff0024da0a81cc1afaf486b988e8eb7b429c Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 18 Dec 2024 15:02:12 -0500 Subject: [PATCH] rhsm: Factor out a helper Signed-off-by: Colin Walters --- lib/src/rhsm.rs | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/lib/src/rhsm.rs b/lib/src/rhsm.rs index cb5fd4a9..2ba477b1 100644 --- a/lib/src/rhsm.rs +++ b/lib/src/rhsm.rs @@ -36,62 +36,39 @@ struct RhsmFacts { available_digest: String, } +/// Return the image reference, version and digest as owned strings. +/// A missing version is serialized as the empty string. +fn status_to_strings(imagestatus: &crate::spec::ImageStatus) -> (String, String, String) { + let image = imagestatus.image.image.clone(); + let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); + let digest = imagestatus.image_digest.clone(); + (image, version, digest) +} + impl From for RhsmFacts { fn from(hoststatus: crate::spec::HostStatus) -> Self { let (booted_image, booted_version, booted_digest) = hoststatus .booted .as_ref() - .and_then(|boot_entry| { - boot_entry.image.as_ref().map(|imagestatus| { - let image = imagestatus.image.image.clone(); - let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); - let digest = imagestatus.image_digest.clone(); - - (image, version, digest) - }) - }) + .and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) .unwrap_or_default(); let (staged_image, staged_version, staged_digest) = hoststatus .staged .as_ref() - .and_then(|boot_entry| { - boot_entry.image.as_ref().map(|imagestatus| { - let image = imagestatus.image.image.clone(); - let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); - let digest = imagestatus.image_digest.clone(); - - (image, version, digest) - }) - }) + .and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) .unwrap_or_default(); let (rollback_image, rollback_version, rollback_digest) = hoststatus .rollback .as_ref() - .and_then(|boot_entry| { - boot_entry.image.as_ref().map(|imagestatus| { - let image = imagestatus.image.image.clone(); - let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); - let digest = imagestatus.image_digest.clone(); - - (image, version, digest) - }) - }) + .and_then(|boot_entry| boot_entry.image.as_ref().map(status_to_strings)) .unwrap_or_default(); let (available_image, available_version, available_digest) = hoststatus .booted .as_ref() - .and_then(|boot_entry| { - boot_entry.cached_update.as_ref().map(|imagestatus| { - let image = imagestatus.image.image.clone(); - let version = imagestatus.version.as_ref().cloned().unwrap_or_default(); - let digest = imagestatus.image_digest.clone(); - - (image, version, digest) - }) - }) + .and_then(|boot_entry| boot_entry.cached_update.as_ref().map(status_to_strings)) .unwrap_or_default(); Self {