debian_packaging/
source_package_control.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/*! Source package control files. */
6
7use {
8    crate::{
9        control::{ControlFile, ControlParagraph},
10        error::{DebianError, Result},
11    },
12    std::io::BufRead,
13};
14
15/// Represents a `debian/control` file.
16///
17/// Specified at <https://www.debian.org/doc/debian-policy/ch-controlfields.html#source-package-control-files-debian-control>.
18///
19/// The 1st paragraph defines the source package. Subsequent paragraphs define binary packages
20/// that the source tree builds.
21#[derive(Default)]
22pub struct SourcePackageControlFile<'a> {
23    general: ControlParagraph<'a>,
24    binaries: Vec<ControlParagraph<'a>>,
25}
26
27impl<'a> SourcePackageControlFile<'a> {
28    /// Construct an instance from an iterable of [ControlParagraph].
29    pub fn from_paragraphs(
30        mut paragraphs: impl Iterator<Item = ControlParagraph<'a>>,
31    ) -> Result<Self> {
32        let general = paragraphs.next().ok_or_else(|| {
33            DebianError::ControlParseError(
34                "no general paragraph in source control file".to_string(),
35            )
36        })?;
37
38        let binaries = paragraphs.collect::<Vec<_>>();
39
40        Ok(Self { general, binaries })
41    }
42
43    /// Construct an instance by parsing a control file from a reader.
44    pub fn parse_reader<R: BufRead>(reader: &mut R) -> Result<Self> {
45        let control = ControlFile::parse_reader(reader)?;
46
47        Self::from_paragraphs(control.paragraphs().map(|x| x.to_owned()))
48    }
49
50    /// Construct an instance by parsing a string.
51    pub fn parse_str(s: &str) -> Result<Self> {
52        let mut reader = std::io::BufReader::new(s.as_bytes());
53        Self::parse_reader(&mut reader)
54    }
55
56    /// Obtain a handle on the general paragraph.
57    pub fn general_paragraph(&self) -> &ControlParagraph<'a> {
58        &self.general
59    }
60
61    /// Obtain an iterator over paragraphs defining binaries.
62    pub fn binary_paragraphs(&self) -> impl Iterator<Item = &ControlParagraph<'a>> {
63        self.binaries.iter()
64    }
65}