SummaryBuilder

Struct SummaryBuilder 

Source
pub struct SummaryBuilder { /* private fields */ }
Expand description

Builder for constructing a Summary from VARIABLE=VALUE lines.

§Example

use pkgsrc::summary::SummaryBuilder;

let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/summary/mktool.txt");
let input = std::fs::read_to_string(path).expect("failed to read mktool.txt");

assert_eq!(
    SummaryBuilder::new()
        .vars(input.lines())
        .build()
        .expect("build failed")
        .pkgname(),
    "mktool-1.4.2"
);

Implementations§

Source§

impl SummaryBuilder

Source

pub fn new() -> Self

Create a new empty SummaryBuilder.

Source

pub fn var(self, line: impl AsRef<str>) -> Self

Add a single VARIABLE=VALUE line.

This method is infallible; validation occurs when build is called.

Prefer vars when adding multiple variables.

Source

pub fn vars<I, S>(self, lines: I) -> Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Add VARIABLE=VALUE lines.

This method is infallible; validation occurs when build is called.

§Example
use pkgsrc::summary::SummaryBuilder;

let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/summary/mktool.txt");
let input = std::fs::read_to_string(path).expect("failed to read mktool.txt");

assert_eq!(
    SummaryBuilder::new()
        .vars(input.lines())
        .build()
        .expect("build failed")
        .pkgname(),
    "mktool-1.4.2"
);
Source

pub fn allow_unknown(self, yes: bool) -> Self

Allow unknown variables instead of returning an error.

Source

pub fn allow_incomplete(self, yes: bool) -> Self

Allow incomplete entries missing required fields.

Source

pub fn build(self) -> Result<Summary>

Validate and finalize the Summary.

Parses all added variables, validates that all required fields are present, and returns the completed Summary.

§Errors

Returns Error if the input is invalid. Applications may want to ignore Error::UnknownVariable if they wish to be future-proof against potential new additions to the pkg_summary format.

§Examples
use pkgsrc::summary::{Error, SummaryBuilder};

let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/summary/mktool.txt");
let input = std::fs::read_to_string(path).expect("failed to read mktool.txt");

// Valid pkg_summary data.
assert_eq!(
    SummaryBuilder::new()
        .vars(input.lines())
        .build()
        .expect("build failed")
        .pkgname(),
    "mktool-1.4.2"
);

// Missing required fields.
assert!(matches!(
    SummaryBuilder::new()
        .vars(["PKGNAME=testpkg-1.0", "COMMENT=Test"])
        .build(),
    Err(Error::Incomplete { .. })
));

// Contains a line not in VARIABLE=VALUE format.
assert!(matches!(
    SummaryBuilder::new()
        .vars(["not a valid line"])
        .build(),
    Err(Error::ParseLine { .. })
));

// Unknown variable name.
assert!(matches!(
    SummaryBuilder::new()
        .vars(["UNKNOWN=value"])
        .build(),
    Err(Error::UnknownVariable { .. })
));

// Invalid integer value (with all other required fields present).
assert!(matches!(
    SummaryBuilder::new()
        .vars([
            "BUILD_DATE=2019-08-12",
            "CATEGORIES=devel",
            "COMMENT=test",
            "DESCRIPTION=test",
            "MACHINE_ARCH=x86_64",
            "OPSYS=NetBSD",
            "OS_VERSION=9.0",
            "PKGNAME=test-1.0",
            "PKGPATH=devel/test",
            "PKGTOOLS_VERSION=20091115",
            "SIZE_PKG=not_a_number",
        ])
        .build(),
    Err(Error::ParseInt { .. })
));

Trait Implementations§

Source§

impl Clone for SummaryBuilder

Source§

fn clone(&self) -> SummaryBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SummaryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SummaryBuilder

Source§

fn default() -> SummaryBuilder

Returns the “default value” for a type. Read more
Source§

impl PartialEq for SummaryBuilder

Source§

fn eq(&self, other: &SummaryBuilder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SummaryBuilder

Source§

impl StructuralPartialEq for SummaryBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.