use {
crate::{
control::{ControlFile, ControlParagraph},
error::{DebianError, Result},
},
std::io::BufRead,
};
#[derive(Default)]
pub struct SourcePackageControlFile<'a> {
general: ControlParagraph<'a>,
binaries: Vec<ControlParagraph<'a>>,
}
impl<'a> SourcePackageControlFile<'a> {
pub fn from_paragraphs(
mut paragraphs: impl Iterator<Item = ControlParagraph<'a>>,
) -> Result<Self> {
let general = paragraphs.next().ok_or_else(|| {
DebianError::ControlParseError(
"no general paragraph in source control file".to_string(),
)
})?;
let binaries = paragraphs.collect::<Vec<_>>();
Ok(Self { general, binaries })
}
pub fn parse_reader<R: BufRead>(reader: &mut R) -> Result<Self> {
let control = ControlFile::parse_reader(reader)?;
Self::from_paragraphs(control.paragraphs().map(|x| x.to_owned()))
}
pub fn parse_str(s: &str) -> Result<Self> {
let mut reader = std::io::BufReader::new(s.as_bytes());
Self::parse_reader(&mut reader)
}
pub fn general_paragraph(&self) -> &ControlParagraph<'a> {
&self.general
}
pub fn binary_paragraphs(&self) -> impl Iterator<Item = &ControlParagraph<'a>> {
self.binaries.iter()
}
}