Skip to content

Commit

Permalink
Merge pull request #14 from k9withabone/is_empty
Browse files Browse the repository at this point in the history
Add `is_empty()` methods
  • Loading branch information
k9withabone authored Apr 8, 2024
2 parents 3154b13 + 2be9127 commit d268ea6
Show file tree
Hide file tree
Showing 11 changed files with 605 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ serde_yaml = "0.9"

[package]
name = "compose_spec"
version = "0.1.0"
version = "0.1.1-beta.1"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
107 changes: 107 additions & 0 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,54 @@ pub struct Network {
pub extensions: Extensions,
}

impl Network {
/// Returns `true` if all fields are [`None`], `false`, or empty.
///
/// The `ipam` field counts as empty if it is [`None`] or [empty](Ipam::is_empty()).
///
/// # Examples
///
/// ```
/// use compose_spec::{Network, network::Ipam};
///
/// let mut network = Network::default();
/// assert!(network.is_empty());
///
/// network.ipam = Some(Ipam::default());
/// assert!(network.is_empty());
///
/// network.ipam = Some(Ipam {
/// driver: Some("driver".to_owned()),
/// ..Ipam::default()
/// });
/// assert!(!network.is_empty());
/// ```
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
driver,
driver_opts,
attachable,
enable_ipv6,
ipam,
internal,
labels,
name,
extensions,
} = self;

driver.is_none()
&& driver_opts.is_empty()
&& !attachable
&& !enable_ipv6
&& !ipam.as_ref().is_some_and(|ipam| !ipam.is_empty())
&& !internal
&& labels.is_empty()
&& name.is_none()
&& extensions.is_empty()
}
}

/// [`Network`] driver.
///
/// Default and available values are platform specific.
Expand Down Expand Up @@ -195,6 +243,45 @@ pub struct Ipam {
pub extensions: Extensions,
}

impl Ipam {
/// Returns `true` if all fields are [`None`] or empty.
///
/// The `config` field counts as empty if all [`IpamConfig`]s are
/// [empty](IpamConfig::is_empty()) or if the [`Vec`] is empty.
///
/// # Examples
///
/// ```
/// use compose_spec::network::{Ipam, IpamConfig};
///
/// let mut ipam = Ipam::default();
/// assert!(ipam.is_empty());
///
/// ipam.config.push(IpamConfig::default());
/// assert!(ipam.is_empty());
///
/// ipam.config.push(IpamConfig {
/// subnet: Some("10.0.0.0/24".parse().unwrap()),
/// ..IpamConfig::default()
/// });
/// assert!(!ipam.is_empty());
/// ```
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
driver,
config,
options,
extensions,
} = self;

driver.is_none()
&& config.iter().all(IpamConfig::is_empty)
&& options.is_empty()
&& extensions.is_empty()
}
}

/// [`Ipam`] configuration.
///
/// [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/06-networks.md#ipam)
Expand Down Expand Up @@ -223,3 +310,23 @@ pub struct IpamConfig {
#[serde(flatten)]
pub extensions: Extensions,
}

impl IpamConfig {
/// Returns `true` if all fields are [`None`] or empty.
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
subnet,
ip_range,
gateway,
aux_addresses,
extensions,
} = self;

subnet.is_none()
&& ip_range.is_none()
&& gateway.is_none()
&& aux_addresses.is_empty()
&& extensions.is_empty()
}
}
14 changes: 14 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,20 @@ pub struct Logging {
pub extensions: Extensions,
}

impl Logging {
/// Returns `true` if all fields are [`None`] or empty.
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
driver,
options,
extensions,
} = self;

driver.is_none() && options.is_empty() && extensions.is_empty()
}
}

/// Preference for a [`Service`] container to be killed by the platform in the case of memory
/// starvation.
///
Expand Down
22 changes: 22 additions & 0 deletions src/service/blkio_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ pub struct BlkioConfig {
pub weight_device: Vec<WeightDevice>,
}

impl BlkioConfig {
/// Returns `true` if all fields are empty or [`None`].
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
device_read_bps,
device_read_iops,
device_write_bps,
device_write_iops,
weight,
weight_device,
} = self;

device_read_bps.is_empty()
&& device_read_iops.is_empty()
&& device_write_bps.is_empty()
&& device_write_iops.is_empty()
&& weight.is_none()
&& weight_device.is_empty()
}
}

/// Limit in bytes per second for read/write operations on a given device.
///
/// [compose-spec](https://github.com/compose-spec/compose-spec/blob/master/05-services.md#device_read_bps-device_write_bps)
Expand Down
52 changes: 52 additions & 0 deletions src/service/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,58 @@ pub struct Build {
pub extensions: Extensions,
}

impl Build {
/// Returns `true` if all fields are [`None`] or empty.
#[must_use]
pub fn is_empty(&self) -> bool {
let Self {
context,
dockerfile,
args,
ssh,
cache_from,
cache_to,
additional_contexts,
extra_hosts,
isolation,
privileged,
labels,
no_cache,
pull,
network,
shm_size,
target,
secrets,
tags,
ulimits,
platforms,
extensions,
} = self;

context.is_none()
&& dockerfile.is_none()
&& args.is_empty()
&& ssh.is_empty()
&& cache_from.is_empty()
&& cache_to.is_empty()
&& additional_contexts.is_empty()
&& extra_hosts.is_empty()
&& isolation.is_none()
&& !privileged
&& labels.is_empty()
&& !no_cache
&& !pull
&& network.is_none()
&& shm_size.is_none()
&& target.is_none()
&& secrets.is_empty()
&& tags.is_empty()
&& ulimits.is_empty()
&& platforms.is_empty()
&& extensions.is_empty()
}
}

/// Deserialize `additional_contexts` field of [`Build`].
///
/// Converts from [`ListOrMap`].
Expand Down
Loading

0 comments on commit d268ea6

Please sign in to comment.