From e59112145067cbbec7997af531bcfbf67c982c72 Mon Sep 17 00:00:00 2001 From: Ximon Eighteen <3304436+ximon18@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:36:29 +0100 Subject: [PATCH 1/2] Proof of concept, expected to be replaced by a better impl (a) as a separate `FormatWriter` and (b) that only uses tabs between record fields and not between rdata values. --- src/base/dig_printer.rs | 4 ++-- src/base/zonefile_fmt.rs | 39 +++++++++++++++++++++++---------------- src/rdata/nsec3.rs | 4 ++-- src/sign/records.rs | 6 ++++++ src/validate.rs | 2 +- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/base/dig_printer.rs b/src/base/dig_printer.rs index 426b8dc37..f67ad257c 100644 --- a/src/base/dig_printer.rs +++ b/src/base/dig_printer.rs @@ -24,7 +24,7 @@ impl<'a, Octs: AsRef<[u8]>> fmt::Display for DigPrinter<'a, Octs> { writeln!( f, ";; ->>HEADER<<- opcode: {}, rcode: {}, id: {}", - header.opcode().display_zonefile(false), + header.opcode().display_zonefile(false, false), header.rcode(), header.id() )?; @@ -161,7 +161,7 @@ fn write_record_item( let parsed = item.to_any_record::>(); match parsed { - Ok(item) => writeln!(f, "{}", item.display_zonefile(false)), + Ok(item) => writeln!(f, "{}", item.display_zonefile(false, false)), Err(_) => writeln!( f, "; {} {} {} {} ", diff --git a/src/base/zonefile_fmt.rs b/src/base/zonefile_fmt.rs index 8a9e22e75..72b21a0af 100644 --- a/src/base/zonefile_fmt.rs +++ b/src/base/zonefile_fmt.rs @@ -13,18 +13,19 @@ pub type Result = core::result::Result<(), Error>; pub struct ZoneFileDisplay<'a, T: ?Sized> { inner: &'a T, - pretty: bool, + multiline: bool, + tabbed: bool, } impl fmt::Display for ZoneFileDisplay<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.pretty { + if self.multiline { self.inner .fmt(&mut MultiLineWriter::new(f)) .map_err(|_| fmt::Error) } else { self.inner - .fmt(&mut SimpleWriter::new(f)) + .fmt(&mut SimpleWriter::new(f, self.tabbed)) .map_err(|_| fmt::Error) } } @@ -41,10 +42,11 @@ pub trait ZonefileFmt { /// /// The returned object will be displayed as zonefile when printed or /// written using `fmt::Display`. - fn display_zonefile(&self, pretty: bool) -> ZoneFileDisplay<'_, Self> { + fn display_zonefile(&self, multiline: bool, tabbed: bool) -> ZoneFileDisplay<'_, Self> { ZoneFileDisplay { inner: self, - pretty, + multiline, + tabbed, } } } @@ -88,13 +90,15 @@ pub trait FormatWriter: Sized { struct SimpleWriter { first: bool, writer: W, + tabbed: bool, } impl SimpleWriter { - fn new(writer: W) -> Self { + fn new(writer: W, tabbed: bool) -> Self { Self { first: true, writer, + tabbed, } } } @@ -102,7 +106,10 @@ impl SimpleWriter { impl FormatWriter for SimpleWriter { fn fmt_token(&mut self, args: fmt::Arguments<'_>) -> Result { if !self.first { - self.writer.write_char(' ')?; + match self.tabbed { + true => self.writer.write_char('\t')?, + false => self.writer.write_char(' ')?, + } } self.first = false; self.writer.write_fmt(args)?; @@ -251,7 +258,7 @@ mod test { let record = create_record(A::new("128.140.76.106".parse().unwrap())); assert_eq!( "example.com. 3600 IN A 128.140.76.106", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -262,7 +269,7 @@ mod test { )); assert_eq!( "example.com. 3600 IN CNAME example.com.", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -279,7 +286,7 @@ mod test { ); assert_eq!( "example.com. 3600 IN DS 5414 15 2 DEADBEEF", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); assert_eq!( [ @@ -289,7 +296,7 @@ mod test { " DEADBEEF )", ] .join("\n"), - record.display_zonefile(true).to_string() + record.display_zonefile(true, false).to_string() ); } @@ -306,7 +313,7 @@ mod test { ); assert_eq!( "example.com. 3600 IN CDS 5414 15 2 DEADBEEF", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -318,7 +325,7 @@ mod test { )); assert_eq!( "example.com. 3600 IN MX 20 example.com.", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -338,7 +345,7 @@ mod test { more like a silly monkey with a typewriter accidentally writing \ some shakespeare along the way but it feels like I have to type \ e\" \"ven longer to hit that limit!\"", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -351,7 +358,7 @@ mod test { )); assert_eq!( "example.com. 3600 IN HINFO \"Windows\" \"Windows Server\"", - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } @@ -368,7 +375,7 @@ mod test { )); assert_eq!( r#"example.com. 3600 IN NAPTR 100 50 "a" "z3950+N2L+N2C" "!^urn:cid:.+@([^\\.]+\\.)(.*)$!\\2!i" cidserver.example.com."#, - record.display_zonefile(false).to_string() + record.display_zonefile(false, false).to_string() ); } } diff --git a/src/rdata/nsec3.rs b/src/rdata/nsec3.rs index d80f12b9b..f57f48166 100644 --- a/src/rdata/nsec3.rs +++ b/src/rdata/nsec3.rs @@ -1608,7 +1608,7 @@ mod test { Nsec3::scan, &rdata, ); - assert_eq!(&format!("{}", rdata.display_zonefile(false)), "1 10 11 626172 CPNMU A SRV"); + assert_eq!(&format!("{}", rdata.display_zonefile(false, false)), "1 10 11 626172 CPNMU A SRV"); } #[test] @@ -1632,7 +1632,7 @@ mod test { Nsec3::scan, &rdata, ); - assert_eq!(&format!("{}", rdata.display_zonefile(false)), "1 10 11 - CPNMU A SRV"); + assert_eq!(&format!("{}", rdata.display_zonefile(false, false)), "1 10 11 - CPNMU A SRV"); } #[test] diff --git a/src/sign/records.rs b/src/sign/records.rs index f14891b6f..8993842e8 100644 --- a/src/sign/records.rs +++ b/src/sign/records.rs @@ -31,6 +31,7 @@ use crate::zonetree::types::StoredRecordData; use crate::zonetree::StoredName; use super::{SignRaw, SigningKey}; +use core::slice::Iter; //------------ SortedRecords ------------------------------------------------- @@ -79,6 +80,11 @@ impl SortedRecords { { self.rrsets().find(|rrset| rrset.rtype() == Rtype::SOA) } + + + pub fn iter(&self) -> Iter<'_, Record> { + self.records.iter() + } } impl SortedRecords { diff --git a/src/validate.rs b/src/validate.rs index cdcc18312..ea1ef8b82 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -322,7 +322,7 @@ impl> Key { w, "{} IN DNSKEY {}", self.owner().fmt_with_dot(), - self.to_dnskey().display_zonefile(false), + self.to_dnskey().display_zonefile(false, false), ) } From b2a2169013c94e0646127b7b51cc5d300e9ee72d Mon Sep 17 00:00:00 2001 From: Ximon Eighteen <3304436+ximon18@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:39:08 +0100 Subject: [PATCH 2/2] Cargo fmt. --- src/base/zonefile_fmt.rs | 6 +++++- src/sign/records.rs | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/base/zonefile_fmt.rs b/src/base/zonefile_fmt.rs index 72b21a0af..dd7860586 100644 --- a/src/base/zonefile_fmt.rs +++ b/src/base/zonefile_fmt.rs @@ -42,7 +42,11 @@ pub trait ZonefileFmt { /// /// The returned object will be displayed as zonefile when printed or /// written using `fmt::Display`. - fn display_zonefile(&self, multiline: bool, tabbed: bool) -> ZoneFileDisplay<'_, Self> { + fn display_zonefile( + &self, + multiline: bool, + tabbed: bool, + ) -> ZoneFileDisplay<'_, Self> { ZoneFileDisplay { inner: self, multiline, diff --git a/src/sign/records.rs b/src/sign/records.rs index 8993842e8..75a1f252c 100644 --- a/src/sign/records.rs +++ b/src/sign/records.rs @@ -81,7 +81,6 @@ impl SortedRecords { self.rrsets().find(|rrset| rrset.rtype() == Rtype::SOA) } - pub fn iter(&self) -> Iter<'_, Record> { self.records.iter() }