Skip to content

Commit

Permalink
Refactor display format for DnsMessage and Question sections
Browse files Browse the repository at this point in the history
  • Loading branch information
joalopez1206 committed Jul 18, 2024
1 parent 4999c98 commit 25c7575
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,17 @@ impl DnsMessage {
impl fmt::Display for DnsMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut result = String::new();
let question = self.get_question();
let answers = self.get_answer().into_iter();
let authority = self.get_authority().into_iter();
let additional = self.get_additional().into_iter();
result.push_str(&format!("Answer\n"));
result.push_str(&format!("Question section\n"));
result.push_str(&format!("{}\n", question));
result.push_str(&format!("Answer section\n"));
answers.for_each(|answer| result.push_str(&format!("{}\n", answer)));
result.push_str(&format!("Authority\n"));
result.push_str(&format!("Authority section\n"));
authority.for_each(|authority| result.push_str(&format!("{}\n", authority)));
result.push_str(&format!("Additional\n"));
result.push_str(&format!("Additional section\n"));
additional.for_each(|additional| result.push_str(&format!("{}\n", additional)));
write!(f, "{}", result)
}
Expand Down
16 changes: 16 additions & 0 deletions src/message/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::message::rclass::Rclass;

use super::rrtype::Rrtype;

use std::fmt;

#[derive(Default, Clone)]
/// An struct that represents the question section from a dns message
/// ```text
Expand All @@ -29,6 +31,20 @@ pub struct Question {
rclass: Rclass,
}


impl fmt::Display for Question {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let name = self.get_qname().get_name();
let rtype = self.get_rrtype();
let rclass = self.get_rclass();

formatter.write_fmt(format_args!(
"{} {} {} ",
name, rclass, rtype
))
}
}

// Methods
impl Question {
/// Creates a new Question with default values
Expand Down

0 comments on commit 25c7575

Please sign in to comment.