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
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.
§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());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);Trait Implementations§
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)