pkgsrc

Struct Pattern

source
pub struct Pattern { /* private fields */ }
Expand description

Package pattern matching.

Pattern matching is used to specify package requirements for various dependency types. This module supports all of the pattern match types that are used across pkgsrc.

§Examples

Standard UNIX glob matches are probably the most common style of dependency pattern, matching any version of a specific package. This module uses the glob crate to perform the match.

use pkgsrc::Pattern;

let m = Pattern::new("mutt-[0-9]*").unwrap();
assert_eq!(m.matches("mutt-2.2.13"), true);
assert_eq!(m.matches("mutt-vid-1.1"), false);
assert_eq!(m.matches("pine-1.0"), false);

Next most popular are so-called “dewey” matches. These are used to test for a specific range of versions.

use pkgsrc::Pattern;

let m = Pattern::new("librsvg>=2.12<2.41").unwrap();
assert_eq!(m.matches("librsvg-2.11"), false);
assert_eq!(m.matches("librsvg-2.12alpha"), false);
assert_eq!(m.matches("librsvg-2.13"), true);
assert_eq!(m.matches("librsvg-2.41"), false);

Alternate matches are csh-style {foo,bar} either/or matches, matching any of the expanded strings.

use pkgsrc::Pattern;

let m = Pattern::new("{mysql,mariadb,percona}-[0-9]*").unwrap();
assert_eq!(m.matches("mysql-8.0.36"), true);
assert_eq!(m.matches("mariadb-11.4.3"), true);
assert_eq!(m.matches("postgresql-16.4"), false);

Finally plain, exact string matches can be used, though these are very rare and never recommended.

use pkgsrc::Pattern;

let m = Pattern::new("foobar-1.0").unwrap();
assert_eq!(m.matches("foobar-1.0"), true);
assert_eq!(m.matches("foobar-1.1"), false);

If the pattern is invalid, Pattern::new will return a PatternError.

use pkgsrc::{PatternError::*, Pattern};

// Missing closing bracket or too many *'s.
assert!(matches!(Pattern::new("foo-[0-9"), Err(Glob(_))));
assert!(matches!(Pattern::new("foo-[0-9]***"), Err(Glob(_))));

// Too many or incorrectly-ordered comparisons.
assert!(matches!(Pattern::new("foo>1.0<2<3"), Err(Dewey(_))));
assert!(matches!(Pattern::new("foo<1>0"), Err(Dewey(_))));

// Unbalanced or incorrectly-ordered braces.
assert!(matches!(Pattern::new("{foo,bar}}>1.0"), Err(Alternate)));
assert!(matches!(Pattern::new("foo}b{ar>1.0"), Err(Alternate)));

Implementations§

source§

impl Pattern

source

pub fn new(pattern: &str) -> Result<Self, PatternError>

Compile a pattern. If the pattern is invalid in any way a PatternError is returned.

§Example
use pkgsrc::Pattern;

let pkgmatch = Pattern::new("librsvg>=2.12<2.41");
assert!(pkgmatch.is_ok());

// Missing closing brace
let pkgmatch = Pattern::new("{mariadb,mysql*-[0-9]");
assert!(pkgmatch.is_err());
source

pub fn matches(&self, pkg: &str) -> bool

Return whether a given str matches the compiled pattern. pkg must be a fully-specified PKGNAME.

§Example
use pkgsrc::Pattern;

let pkgmatch = Pattern::new("librsvg>=2.12<2.41").unwrap();
assert_eq!(pkgmatch.matches("librsvg"), false);
assert_eq!(pkgmatch.matches("librsvg-2.11"), false);
assert_eq!(pkgmatch.matches("librsvg-2.13"), true);
assert_eq!(pkgmatch.matches("librsvg-2.41"), false);
source

pub fn pattern(&self) -> &str

Return the original pattern string.

Trait Implementations§

source§

impl Clone for Pattern

source§

fn clone(&self) -> Pattern

Returns a copy 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 Pattern

source§

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

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

impl Default for Pattern

source§

fn default() -> Pattern

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

impl Hash for Pattern

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Pattern

source§

fn eq(&self, other: &Pattern) -> 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 StructuralPartialEq for Pattern

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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.