Pattern

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(_))));

// Version component overflow (exceeds i64::MAX).
assert!(matches!(Pattern::new("foo>=20251208143052123456"), 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.

§Errors

Returns PatternError::Alternate if braces are unbalanced.

Returns PatternError::Dewey if a dewey pattern is malformed.

Returns PatternError::Glob if a glob pattern is invalid.

§Examples
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 best_match<'a>( &self, pkg1: &'a str, pkg2: &'a str, ) -> Result<Option<&'a str>, PatternError>

Given two package names, return the “best” match - that is, the one that is a match with the higher version. If neither match return None.

When versions compare equal, the lexicographically smaller string is returned, to match pkg_install’s pkg_order().

§Errors

Returns PatternError::Dewey if parsing a package version fails.

Source

pub fn best_match_pbulk<'a>( &self, pkg1: &'a str, pkg2: &'a str, ) -> Result<Option<&'a str>, PatternError>

Identical to Pattern::best_match except when versions compare equal, the lexicographically greater string is returned to match pbulk’s pkg_order().

§Errors

Returns PatternError::Dewey if parsing a package version fails.

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 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 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 Display for Pattern

Source§

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

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

impl FromStr for Pattern

Source§

type Err = PatternError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this 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 Eq for Pattern

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, 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.