shared_mime_embedded/
lib.rs

1use data::EMBED_BYTES;
2use log::*;
3use postcard::from_bytes;
4
5use shared_mime::record::MimeTypeRecord;
6#[cfg(feature = "xdg-runtime")]
7use shared_mime::runtime::load_xdg_mime_info;
8use shared_mime::LoadError;
9pub use shared_mime::{Answer, FileQuery, FileQueryBuilder, MimeDB};
10
11mod data;
12#[cfg(test)]
13mod tests;
14
15/// Get the embedded MIME info database.
16pub fn embedded_mime_db() -> MimeDB {
17    let mut db = MimeDB::new();
18    let recs: Vec<MimeTypeRecord> = from_bytes(EMBED_BYTES).expect("embedded data decode failed");
19    db.add_records(recs);
20    debug!(
21        "loaded embedded MIME info with {} types and {} globs",
22        db.type_count(),
23        db.glob_count()
24    );
25    db
26}
27
28/// Load the MIME info database.
29///
30/// This starts by loading the embedded database. If the `xdg-runtime` feature
31/// is enabled, it then loads the XDG shared mime database installed on the
32/// system, treating the embedded database as a directory of mime information
33/// that preceeds any system information.
34pub fn load_mime_db() -> Result<MimeDB, LoadError> {
35    debug!("loading embedded MIME database");
36    let mut db = embedded_mime_db();
37
38    #[cfg(feature = "xdg-runtime")]
39    {
40        debug!("loading runtime MIME database");
41        let nt = db.type_count();
42        let ng = db.glob_count();
43        match load_xdg_mime_info() {
44            Ok(info) => db.add_shared_mime_info(info),
45            Err(e) => warn!("error loading MIME info: {:?}", e),
46        }
47        debug!(
48            "loaded shared MIME info with {} new types and {} new globs",
49            db.type_count() - nt,
50            db.glob_count() - ng
51        );
52    }
53
54    Ok(db)
55}