Skip to content

Commit

Permalink
Merge pull request #90 from cgwalters/default-platform
Browse files Browse the repository at this point in the history
`impl Default` for `Os` and `Arch`, use in `ImageConfiguration::default()`
  • Loading branch information
saschagrunert authored Jan 17, 2022
2 parents ced72f4 + 5c01984 commit b10f910
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
29 changes: 11 additions & 18 deletions src/image/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -181,23 +191,6 @@ impl ImageConfiguration {
}
}

impl Default for ImageConfiguration {
fn default() -> Self {
Self {
created: Default::default(),
author: Default::default(),
architecture: Arch::Amd64,
os: Os::Linux,
os_version: Default::default(),
os_features: Default::default(),
variant: Default::default(),
config: Default::default(),
rootfs: Default::default(),
history: Default::default(),
}
}
}

#[derive(
Builder,
Clone,
Expand Down
16 changes: 3 additions & 13 deletions src/image/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ pub struct Descriptor {
platform: Option<Platform>,
}

#[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),
Expand Down Expand Up @@ -109,18 +111,6 @@ pub struct Platform {
variant: Option<String>,
}

impl Default for Platform {
fn default() -> Self {
Self {
architecture: Arch::Amd64,
os: Os::Linux,
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<String>) -> Self {
Expand Down
19 changes: 19 additions & 0 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit b10f910

Please sign in to comment.