From b0c1e78d5de3779f99f2d00421a9a23b77e0d74d Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Sat, 22 Apr 2023 09:45:34 +0800 Subject: [PATCH 1/3] implement TryFrom for ConfigFile Signed-off-by: Luke Swithenbank --- src/client.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/client.rs b/src/client.rs index 560de302..518b7a9d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -165,6 +165,15 @@ impl Config { } } +impl TryFrom for ConfigFile { + type Error = crate::errors::OciDistributionError; + + fn try_from(config: Config) -> Result { + let config_file: ConfigFile = serde_json::from_slice(&config.data)?; + Ok(config_file) + } +} + /// The OCI client connects to an OCI registry and fetches OCI images. /// /// An OCI registry is a container registry that adheres to the OCI Distribution From 345cd050286bf237111ac6f12a13a8621b53206e Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Sat, 22 Apr 2023 11:57:51 +0800 Subject: [PATCH 2/3] parse the config data into a string before deserialising it Signed-off-by: Luke Swithenbank --- src/client.rs | 3 ++- src/errors.rs | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 518b7a9d..31e18b56 100644 --- a/src/client.rs +++ b/src/client.rs @@ -169,7 +169,8 @@ impl TryFrom for ConfigFile { type Error = crate::errors::OciDistributionError; fn try_from(config: Config) -> Result { - let config_file: ConfigFile = serde_json::from_slice(&config.data)?; + let config = String::from_utf8(config.data)?; + let config_file: ConfigFile = serde_json::from_str(&config)?; Ok(config_file) } } diff --git a/src/errors.rs b/src/errors.rs index 3404fef8..bcb9729a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -90,6 +90,9 @@ pub enum OciDistributionError { /// Versioned object: JSON deserialization error #[error("Failed to parse manifest: {0}")] VersionedParsingError(String), + #[error(transparent)] + /// Transparent wrapper around `std::string::FromUtf8Error` + FromUtf8(#[from] std::string::FromUtf8Error), } /// Helper type to declare `Result` objects that might return a `OciDistributionError` From 6aaf1cbe1d93315b3ea883b0fac6b18ad706a510 Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Tue, 26 Sep 2023 10:43:04 +0200 Subject: [PATCH 3/3] refactor: provide dedicated error type Provide a dedicated error type to address Config conversion errors. Signed-off-by: Flavio Castelli --- src/client.rs | 6 ++++-- src/errors.rs | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index 31e18b56..ba3ff601 100644 --- a/src/client.rs +++ b/src/client.rs @@ -169,8 +169,10 @@ impl TryFrom for ConfigFile { type Error = crate::errors::OciDistributionError; fn try_from(config: Config) -> Result { - let config = String::from_utf8(config.data)?; - let config_file: ConfigFile = serde_json::from_str(&config)?; + let config = String::from_utf8(config.data) + .map_err(|e| OciDistributionError::ConfigConversionError(e.to_string()))?; + let config_file: ConfigFile = serde_json::from_str(&config) + .map_err(|e| OciDistributionError::ConfigConversionError(e.to_string()))?; Ok(config_file) } } diff --git a/src/errors.rs b/src/errors.rs index bcb9729a..d7859e45 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -90,9 +90,9 @@ pub enum OciDistributionError { /// Versioned object: JSON deserialization error #[error("Failed to parse manifest: {0}")] VersionedParsingError(String), - #[error(transparent)] + #[error("Failed to convert Config into ConfigFile: {0}")] /// Transparent wrapper around `std::string::FromUtf8Error` - FromUtf8(#[from] std::string::FromUtf8Error), + ConfigConversionError(String), } /// Helper type to declare `Result` objects that might return a `OciDistributionError`