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
impl Pattern
Sourcepub fn new(pattern: &str) -> Result<Self, PatternError>
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());Sourcepub fn matches(&self, pkg: &str) -> bool
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);Sourcepub fn best_match<'a>(
&self,
pkg1: &'a str,
pkg2: &'a str,
) -> Result<Option<&'a str>, PatternError>
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.
Sourcepub fn best_match_pbulk<'a>(
&self,
pkg1: &'a str,
pkg2: &'a str,
) -> Result<Option<&'a str>, PatternError>
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.
Trait Implementations§
impl Eq for Pattern
impl StructuralPartialEq for Pattern
Auto Trait Implementations§
impl Freeze for Pattern
impl RefUnwindSafe for Pattern
impl Send for Pattern
impl Sync for Pattern
impl Unpin for Pattern
impl UnwindSafe for Pattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.