From d5ca9bd5df4bc82d5584775eb166d3e902557c7e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 23 Apr 2020 07:54:12 +0200 Subject: [PATCH] src/config/network.rs: cleanup (new helper combine_entry) --- src/config/network.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/config/network.rs b/src/config/network.rs index af674bb1..29b88dfb 100644 --- a/src/config/network.rs +++ b/src/config/network.rs @@ -142,6 +142,33 @@ impl Interface { Ok(()) } + /// Return whether we can write a single entry for inet and inet6 + fn combine_entry(&self) -> bool { + // Note: use match to make sure we considered all values at compile time + match self { + Interface { + method_v4, + method_v6, + options_v4, + options_v6, + // the rest does not matter + name: _name, + auto: _auto, + exists: _exists, + active: _active, + cidr_v4: _cidr_v4, + cidr_v6: _cidr_v6, + gateway_v4: _gateway_v4, + gateway_v6: _gateway_v6, + mtu: _mtu, + } => { + method_v4 == method_v6 + && options_v4.is_empty() + && options_v6.is_empty() + } + } + } + fn write_iface(&self, w: &mut dyn Write) -> Result<(), Error> { fn method_to_str(method: NetworkConfigMethod) -> &'static str { @@ -159,9 +186,7 @@ impl Interface { writeln!(w, "auto {}", self.name)?; } - if self.method_v4 == self.method_v6 - && self.options_v4.is_empty() == self.options_v6.is_empty() - { + if self.combine_entry() { if let Some(method) = self.method_v4 { writeln!(w, "iface {} {}", self.name, method_to_str(method))?; self.write_iface_attributes_v4(w, method)?;