use alloc::collections::BTreeSet;
use alloc::string::ToString;
use core::str::FromStr;
use crate::rr::RecordType;
use crate::rr::rdata::CSYNC;
use crate::serialize::txt::errors::{ParseError, ParseErrorKind, ParseResult};
pub(crate) fn parse<'i, I: Iterator<Item = &'i str>>(mut tokens: I) -> ParseResult<CSYNC> {
let soa_serial: u32 = tokens
.next()
.ok_or_else(|| ParseError::from(ParseErrorKind::MissingToken("soa_serial".to_string())))
.and_then(|s| s.parse().map_err(Into::into))?;
let flags: u16 = tokens
.next()
.ok_or_else(|| ParseError::from(ParseErrorKind::MissingToken("flags".to_string())))
.and_then(|s| s.parse().map_err(Into::into))?;
let immediate: bool = flags & 0b0000_0001 == 0b0000_0001;
let soa_minimum: bool = flags & 0b0000_0010 == 0b0000_0010;
let mut record_types = BTreeSet::new();
for token in tokens {
record_types.insert(RecordType::from_str(token)?);
}
Ok(CSYNC::new(soa_serial, immediate, soa_minimum, record_types))
}
#[test]
fn test_parsing() {
assert_eq!(
parse(vec!["123", "3", "NS"].into_iter()).expect("failed to parse CSYNC"),
CSYNC::new(123, true, true, [RecordType::NS]),
);
}
#[test]
fn test_parsing_fails() {
assert!(parse(vec!["NS"].into_iter()).is_err());
assert!(parse(vec![].into_iter()).is_err());
}