From 6e65562e3d70b66656598e6a567dc71e80dd0f56 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 14 Jan 2022 11:11:54 -0500 Subject: [PATCH 1/2] `impl Default` for `Os` and `Arch`, use in `ImageConfiguration::default()` Hardcoding Linux/x86_64 is a trap because it will silently do the right thing only for users on that platform. Closes: https://github.com/containers/oci-spec-rs/issues/88 --- src/image/config.rs | 4 ++-- src/image/descriptor.rs | 4 ++-- src/image/mod.rs | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/image/config.rs b/src/image/config.rs index 1d6156beaa..512793050c 100644 --- a/src/image/config.rs +++ b/src/image/config.rs @@ -186,8 +186,8 @@ impl Default for ImageConfiguration { Self { created: Default::default(), author: Default::default(), - architecture: Arch::Amd64, - os: Os::Linux, + architecture: Default::default(), + os: Default::default(), os_version: Default::default(), os_features: Default::default(), variant: Default::default(), diff --git a/src/image/descriptor.rs b/src/image/descriptor.rs index 7a56804c26..641bacb8bf 100644 --- a/src/image/descriptor.rs +++ b/src/image/descriptor.rs @@ -112,8 +112,8 @@ pub struct Platform { impl Default for Platform { fn default() -> Self { Self { - architecture: Arch::Amd64, - os: Os::Linux, + architecture: Default::default(), + os: Default::default(), os_version: Default::default(), os_features: Default::default(), variant: Default::default(), diff --git a/src/image/mod.rs b/src/image/mod.rs index d7e2fa88b2..73c2b3a322 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -227,6 +227,12 @@ impl<'de> Deserialize<'de> for Os { } } +impl Default for Os { + fn default() -> Self { + Os::from(std::env::consts::OS) + } +} + /// Name of the CPU target architecture. #[derive(Clone, Debug, PartialEq, Eq)] pub enum Arch { @@ -370,3 +376,16 @@ impl<'de> Deserialize<'de> for Arch { Ok(arch.as_str().into()) } } + +impl Default for Arch { + fn default() -> Self { + // Translate from the Rust architecture names to the Go versions. + // I think the Rust ones are the same as the Linux kernel ones. + let goarch = match std::env::consts::ARCH { + "x86_64" => "amd64", + "aarch64" => "arm64", + o => o, + }; + Arch::from(goarch) + } +} From 5c0198470ca12fa59d0eaaf91c22f1edc58c3477 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 14 Jan 2022 11:24:19 -0500 Subject: [PATCH 2/2] Switch to `#[derive(Default)]` Since we can, and this silences a clippy warning. --- src/image/config.rs | 29 +++++++++++------------------ src/image/descriptor.rs | 16 +++------------- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/image/config.rs b/src/image/config.rs index 512793050c..a58c46aa66 100644 --- a/src/image/config.rs +++ b/src/image/config.rs @@ -15,7 +15,17 @@ use std::{ }; #[derive( - Builder, Clone, Debug, Deserialize, Eq, Getters, MutGetters, Setters, PartialEq, Serialize, + Builder, + Clone, + Debug, + Default, + Deserialize, + Eq, + Getters, + MutGetters, + Setters, + PartialEq, + Serialize, )] #[builder( default, @@ -181,23 +191,6 @@ impl ImageConfiguration { } } -impl Default for ImageConfiguration { - fn default() -> Self { - Self { - created: Default::default(), - author: Default::default(), - architecture: Default::default(), - os: Default::default(), - os_version: Default::default(), - os_features: Default::default(), - variant: Default::default(), - config: Default::default(), - rootfs: Default::default(), - history: Default::default(), - } - } -} - #[derive( Builder, Clone, diff --git a/src/image/descriptor.rs b/src/image/descriptor.rs index 641bacb8bf..876b2ddfc3 100644 --- a/src/image/descriptor.rs +++ b/src/image/descriptor.rs @@ -62,7 +62,9 @@ pub struct Descriptor { platform: Option, } -#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, Setters, PartialEq, Serialize)] +#[derive( + Builder, Clone, Debug, Default, Deserialize, Eq, Getters, Setters, PartialEq, Serialize, +)] #[builder( pattern = "owned", setter(into, strip_option), @@ -109,18 +111,6 @@ pub struct Platform { variant: Option, } -impl Default for Platform { - fn default() -> Self { - Self { - architecture: Default::default(), - os: Default::default(), - os_version: Default::default(), - os_features: Default::default(), - variant: Default::default(), - } - } -} - impl Descriptor { /// Construct a new descriptor with the required fields. pub fn new(media_type: MediaType, size: i64, digest: impl Into) -> Self {