dnsparse/
question.rs

1use core::fmt;
2
3use crate::{Error, Name, QueryKind, QueryClass};
4
5/// A DNS question.
6#[repr(C)]
7pub struct Question<'a> {
8  pub(crate) name: Name<'a>,
9  pub(crate) kind: QueryKind,
10  pub(crate) class: QueryClass,
11}
12
13impl fmt::Debug for Question<'_> {
14  fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
15    fmt.debug_struct("Question")
16      .field("name", &self.name())
17      .field("kind", &self.kind())
18      .field("class", &self.class())
19      .finish()
20  }
21}
22
23impl<'a> Question<'a> {
24  pub(crate) fn read(buf: &'a [u8], i: &'_ mut usize) -> Result<Self, Error> {
25    let mut j = *i;
26    let question = Self {
27      name:  Name::read(buf, &mut j)?,
28      kind:  QueryKind::read(buf, &mut j)?,
29      class: QueryClass::read(buf, &mut j)?,
30    };
31    *i = j;
32
33    Ok(question)
34  }
35
36  #[inline]
37  pub fn name(&self) -> &Name<'a> {
38    &self.name
39  }
40
41  #[inline]
42  pub fn kind(&self) -> &QueryKind {
43    &self.kind
44  }
45
46  #[inline]
47  pub fn class(&self) -> &QueryClass {
48    &self.class
49  }
50}
51
52/// Iterator over [`Question`](struct.Question.html)s contained in a [`Message`](struct.Message.html).
53#[derive(Debug)]
54pub struct Questions<'a> {
55  pub(crate) question_count: usize,
56  pub(crate) current_question: usize,
57  pub(crate) buf: &'a [u8],
58  pub(crate) buf_i: usize,
59}
60
61impl<'a> Iterator for Questions<'a> {
62  type Item = Question<'a>;
63
64  fn next(&mut self) -> Option<Self::Item> {
65    if self.current_question >= self.question_count {
66      return None
67    }
68
69    let mut i = self.buf_i;
70
71    let question = Question::read(self.buf, &mut i).ok()?;
72
73    self.current_question += 1;
74    self.buf_i = i;
75
76    Some(question)
77  }
78}