use std::collections::HashMap;
use super::header::*;
use super::question::*;
use super::resource::*;
use super::*;
use shared::error::*;
#[derive(Default)]
pub(crate) struct Builder {
pub(crate) msg: Option<Vec<u8>>,
pub(crate) section: Section,
pub(crate) header: HeaderInternal,
pub(crate) start: usize,
pub(crate) compression: Option<HashMap<String, usize>>,
}
impl Builder {
pub(crate) fn new(h: &Header) -> Self {
let (id, bits) = h.pack();
Builder {
msg: Some(vec![0; HEADER_LEN]),
start: 0,
section: Section::Header,
header: HeaderInternal {
id,
bits,
..Default::default()
},
compression: None,
}
}
pub(crate) fn enable_compression(&mut self) {
self.compression = Some(HashMap::new());
}
fn start_check(&self, section: Section) -> Result<()> {
if self.section <= Section::NotStarted {
return Err(Error::ErrNotStarted);
}
if self.section > section {
return Err(Error::ErrSectionDone);
}
Ok(())
}
pub(crate) fn start_questions(&mut self) -> Result<()> {
self.start_check(Section::Questions)?;
self.section = Section::Questions;
Ok(())
}
pub(crate) fn start_answers(&mut self) -> Result<()> {
self.start_check(Section::Answers)?;
self.section = Section::Answers;
Ok(())
}
pub(crate) fn start_authorities(&mut self) -> Result<()> {
self.start_check(Section::Authorities)?;
self.section = Section::Authorities;
Ok(())
}
pub(crate) fn start_additionals(&mut self) -> Result<()> {
self.start_check(Section::Additionals)?;
self.section = Section::Additionals;
Ok(())
}
fn increment_section_count(&mut self) -> Result<()> {
let section = self.section;
let (count, err) = match section {
Section::Questions => (&mut self.header.questions, Error::ErrTooManyQuestions),
Section::Answers => (&mut self.header.answers, Error::ErrTooManyAnswers),
Section::Authorities => (&mut self.header.authorities, Error::ErrTooManyAuthorities),
Section::Additionals => (&mut self.header.additionals, Error::ErrTooManyAdditionals),
Section::NotStarted => return Err(Error::ErrNotStarted),
Section::Done => return Err(Error::ErrSectionDone),
Section::Header => return Err(Error::ErrSectionHeader),
};
if *count == u16::MAX {
Err(err)
} else {
*count += 1;
Ok(())
}
}
pub(crate) fn add_question(&mut self, q: &Question) -> Result<()> {
if self.section < Section::Questions {
return Err(Error::ErrNotStarted);
}
if self.section > Section::Questions {
return Err(Error::ErrSectionDone);
}
let msg = self.msg.take();
if let Some(mut msg) = msg {
msg = q.pack(msg, &mut self.compression, self.start)?;
self.increment_section_count()?;
self.msg = Some(msg);
}
Ok(())
}
fn check_resource_section(&self) -> Result<()> {
if self.section < Section::Answers {
return Err(Error::ErrNotStarted);
}
if self.section > Section::Additionals {
return Err(Error::ErrSectionDone);
}
Ok(())
}
pub(crate) fn add_resource(&mut self, r: &mut Resource) -> Result<()> {
self.check_resource_section()?;
if let Some(body) = &r.body {
r.header.typ = body.real_type();
} else {
return Err(Error::ErrNilResourceBody);
}
if let Some(msg) = self.msg.take() {
let (mut msg, len_off) = r.header.pack(msg, &mut self.compression, self.start)?;
let pre_len = msg.len();
if let Some(body) = &r.body {
msg = body.pack(msg, &mut self.compression, self.start)?;
r.header.fix_len(&mut msg, len_off, pre_len)?;
self.increment_section_count()?;
}
self.msg = Some(msg);
}
Ok(())
}
pub(crate) fn finish(&mut self) -> Result<Vec<u8>> {
if self.section < Section::Header {
return Err(Error::ErrNotStarted);
}
self.section = Section::Done;
let buf = self.header.pack(vec![]);
assert_eq!(buf.len(), HEADER_LEN);
if let Some(mut msg) = self.msg.take() {
msg[..HEADER_LEN].copy_from_slice(&buf[..HEADER_LEN]);
Ok(msg)
} else {
Err(Error::ErrEmptyBuilderMsg)
}
}
}