Skip to content

Commit

Permalink
Proof of concept, expected to be replaced by a better impl (a) as a s…
Browse files Browse the repository at this point in the history
…eparate `FormatWriter` and (b) that only uses tabs between record fields and not between rdata values.
  • Loading branch information
ximon18 committed Nov 22, 2024
1 parent d390d15 commit e591121
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/base/dig_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)?;
Expand Down Expand Up @@ -161,7 +161,7 @@ fn write_record_item(
let parsed = item.to_any_record::<AllRecordData<_, _>>();

match parsed {
Ok(item) => writeln!(f, "{}", item.display_zonefile(false)),
Ok(item) => writeln!(f, "{}", item.display_zonefile(false, false)),
Err(_) => writeln!(
f,
"; {} {} {} {} <invalid data>",
Expand Down
39 changes: 23 additions & 16 deletions src/base/zonefile_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: ZonefileFmt + ?Sized> 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)
}
}
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -88,21 +90,26 @@ pub trait FormatWriter: Sized {
struct SimpleWriter<W> {
first: bool,
writer: W,
tabbed: bool,
}

impl<W: fmt::Write> SimpleWriter<W> {
fn new(writer: W) -> Self {
fn new(writer: W, tabbed: bool) -> Self {
Self {
first: true,
writer,
tabbed,
}
}
}

impl<W: fmt::Write> FormatWriter for SimpleWriter<W> {
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)?;
Expand Down Expand Up @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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!(
[
Expand All @@ -289,7 +296,7 @@ mod test {
" DEADBEEF )",
]
.join("\n"),
record.display_zonefile(true).to_string()
record.display_zonefile(true, false).to_string()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}
}
4 changes: 2 additions & 2 deletions src/rdata/nsec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions src/sign/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::zonetree::types::StoredRecordData;
use crate::zonetree::StoredName;

use super::{SignRaw, SigningKey};
use core::slice::Iter;

//------------ SortedRecords -------------------------------------------------

Expand Down Expand Up @@ -79,6 +80,11 @@ impl<N, D> SortedRecords<N, D> {
{
self.rrsets().find(|rrset| rrset.rtype() == Rtype::SOA)
}


pub fn iter(&self) -> Iter<'_, Record<N, D>> {
self.records.iter()
}
}

impl<N> SortedRecords<N, StoredRecordData> {
Expand Down
2 changes: 1 addition & 1 deletion src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<Octs: AsRef<[u8]>> Key<Octs> {
w,
"{} IN DNSKEY {}",
self.owner().fmt_with_dot(),
self.to_dnskey().display_zonefile(false),
self.to_dnskey().display_zonefile(false, false),
)
}

Expand Down

0 comments on commit e591121

Please sign in to comment.