Browse Source

use Lazy instead of OnceCell for MIMEDB

master
Lynne Megido 2 months ago
parent
commit
c2e6738f6b
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
  1. 3
      src/files.rs
  2. 27
      src/lib.rs
  3. 4
      src/main.rs
  4. 3
      src/tests/mod.rs

3
src/files.rs

@ -2,6 +2,7 @@ use std::collections::{BTreeSet, HashMap};
use std::fs::File;
use std::io;
use std::io::{Read, Seek, SeekFrom};
use std::ops::Deref;
use std::path::Path;
use std::str::FromStr;
use std::sync::RwLock;
@ -96,7 +97,7 @@ pub fn wanted_file(
pub fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result<Findings, ScanError> {
let path = entry.path();
// try to determine mimetype for this entry
let result = match mime_type(MIMEDB.get().unwrap(), path) {
let result = match mime_type(MIMEDB.deref(), path) {
// an error occurred while trying to read the file
Err(_) => return Err(ScanError::File(path)),
// the file was read successfully, but we were unable to determine its mimetype

27
src/lib.rs

@ -9,7 +9,7 @@ pub mod parameters;
pub mod utils;
use cfg_if::cfg_if;
use once_cell::sync::OnceCell;
use once_cell::sync::Lazy;
use crate::findings::Findings;
use crate::mime_db::MimeDb;
@ -26,27 +26,10 @@ cfg_if! {
cfg_if! {
if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] {
/// A [OnceCell] holding an instance of [mime_db::MimeDb].
pub static MIMEDB: OnceCell<mime_db::InferDb> = OnceCell::new();
/// A [Lazy] holding an instance of [mime_db::MimeDb].
pub static MIMEDB: Lazy<mime_db::InferDb> = Lazy::new(|| crate::mime_db::InferDb::init());
} else {
/// A [OnceCell] holding an instance of [mime_db::MimeDb].
pub static MIMEDB: OnceCell<mime_db::XdgDb> = OnceCell::new();
}
}
/// Initialises [`MIMEDB`] with a value dependent on the current backend.
pub fn init_db() {
cfg_if! {
if #[cfg(any(all(unix, feature = "infer-backend"), all(not(unix), not(feature = "xdg-mime-backend"))))] {
MIMEDB
.set(crate::mime_db::InferDb::init())
.or(Err("Failed to initialise Infer backend!"))
.unwrap();
} else {
MIMEDB
.set(crate::mime_db::XdgDb::init())
.or(Err("Failed to initialise XDG Mime backend!"))
.unwrap();
}
/// A [Lazy] holding an instance of [mime_db::MimeDb].
pub static MIMEDB: Lazy<mime_db::XdgDb> = Lazy::new(|| crate::mime_db::XdgDb::init());
}
}

4
src/main.rs

@ -25,7 +25,7 @@ use fif::files::{scan_directory, scan_from_walkdir};
use fif::formats::Format;
use fif::parameters::OutputFormat;
use fif::utils::{clap_long_version, os_name};
use fif::{formats, init_db, parameters};
use fif::{formats, parameters};
use log::{debug, error, info, trace, warn, Level};
#[cfg(test)]
@ -58,8 +58,6 @@ fn main() {
std::env::consts::ARCH,
os_name()
);
trace!("Initialising mimetype database");
init_db();
debug!("Iterating directory: {:?}", args.dir);

3
src/tests/mod.rs

@ -128,9 +128,6 @@ fn simple_directory() {
// there should be one file missing: "ignore.fake_ext"
assert_eq!(entries.len(), files.len() - 1);
// initialise global mime DB - this is needed because `scan_from_walkdir` expects it to be present.
crate::init_db();
let results = scan_from_walkdir(&entries, false);
let canonical_results = scan_from_walkdir(&entries, true);
assert_eq!(results.len(), canonical_results.len());

Loading…
Cancel
Save