Skip to main content

Globs

Struct Globs 

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

The filename rules, loaded from every globs2 on the system.

Implementations§

Source§

impl Globs

Source

pub fn parse(text: &str) -> Globs

Parses one globs2 file’s contents.

Lines that cannot be read are skipped rather than failing the file: this is generated data, and one unfamiliar line in it must not cost every rule after it. __NOGLOBS__ — the database’s way of saying a type has had its inherited patterns removed — carries no pattern and is skipped with the rest.

Source

pub fn parse_legacy(text: &str) -> Globs

The older globs format: type:pattern, with no weight and no flags. Still shipped beside globs2, and the only one some hand-made databases have.

Everything in it is weight 50, which is what the format means and what update-mime-database writes into globs2 for a rule that did not ask for anything else.

Source

pub fn load_from(dirs: &[PathBuf]) -> Globs

Every globs2 under dirs (each a XDG_DATA_DIRS entry), merged. Earlier directories are more specific and are consulted first when two rules are otherwise equal.

A directory with no globs2 falls back to its globs — the older file, which is what a hand-made database is most likely to have. Never both from the same directory: they say the same thing, and reading both would double every rule in it.

Source

pub fn type_of(&self, path: &Path) -> Option<&str>

The type of a file with this name, or None when no rule matches.

None rather than application/octet-stream: “no rule knows this name” and “this is a stream of bytes” are different answers, and a caller offering to open the file needs to tell them apart — the first deserves “nothing here knows what this is”, the second would be a claim the database never made. This is the same distinction as a missing config file versus an unparseable one.

When several rules match, the database’s own precedence applies: the highest weight wins, then the longest pattern — so archive.tar.gz is a compressed tar rather than a gzip file, because *.tar.gz is longer than *.gz. A case-sensitive rule only matches the spelling it gives.

Case is tried exactly first, then folded. a.c and a.C are different files: the database has *.c for C and *.C for C++, and a lookup that lowercases everything up front makes one of them unreachable. So the name is matched as written, and only if nothing matches is it tried again in lower case — which is what makes script.PL still a perl script.

Beyond that the specification says the result is undefined, and implementations do differ. Two tie-breaks, in this order, each chosen against what the rest of this machine answers:

  1. A registered type beats an x- one. *.obj is claimed by model/obj, application/x-coff and application/x-tgif at the same weight; x- means unregistered, and a Wavefront model is the better reading of a .obj than a COFF object file. gio answers model/obj here too.
  2. Then the first rule wins, which is what the reference implementation does (it skips a second rule for an extension it has already seen). *.json is claimed by application/json and application/schema+json; the first is the answer everything else on the machine gives.
Source

pub fn all_matches(&self, path: &Path) -> Vec<&str>

Every type whose pattern matches this name, best first.

For mimetype --all, and for a caller that would rather see an ambiguity than have it resolved: photo.jpg matches one rule, but archive.tar.gz matches both *.tar.gz and *.gz and a person may want to know that.

Source

pub fn mimes(&self) -> impl Iterator<Item = &str>

Every type some rule can name a file after, in load order and with repeats — one type usually has several patterns.

This is as close to “the types this machine knows about” as the database gets. It is deliberately the glob file and not the union of everything mentioned anywhere: a type no filename can produce is not one somebody will go looking for by name, and subclasses alone names hundreds of them.

Source

pub fn is_empty(&self) -> bool

Whether anything at all was loaded. An empty database is not an error — a machine may genuinely have no shared-mime-info — but a caller that shows a chooser wants to say so rather than reporting that every file is of unknown type.

Trait Implementations§

Source§

impl Clone for Globs

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Globs

Source§

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

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

impl Default for Globs

Source§

fn default() -> Self

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

impl Eq for Globs

Source§

impl PartialEq for Globs

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Globs

Auto Trait Implementations§

§

impl Freeze for Globs

§

impl RefUnwindSafe for Globs

§

impl Send for Globs

§

impl Sync for Globs

§

impl Unpin for Globs

§

impl UnsafeUnpin for Globs

§

impl UnwindSafe for Globs

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<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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.