Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion between a Config and a ConfigFile #76

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ impl Config {
}
}

impl TryFrom<Config> for ConfigFile {
type Error = crate::errors::OciDistributionError;

fn try_from(config: Config) -> Result<Self> {
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)
}
}

/// 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
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub enum OciDistributionError {
/// Versioned object: JSON deserialization error
#[error("Failed to parse manifest: {0}")]
VersionedParsingError(String),
#[error("Failed to convert Config into ConfigFile: {0}")]
/// Transparent wrapper around `std::string::FromUtf8Error`
ConfigConversionError(String),
}

/// Helper type to declare `Result` objects that might return a `OciDistributionError`
Expand Down