forked from neri/datatrash
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
|
use std::{collections::HashMap, str::FromStr};
|
||
|
|
||
|
use lazy_static::lazy_static;
|
||
|
use mime::Mime;
|
||
|
|
||
|
lazy_static! {
|
||
|
static ref ALIASES: HashMap<Mime, Mime> = get_mime_aliases();
|
||
|
static ref PARENTS: Vec<(Mime, Mime)> = get_mime_parent_relations();
|
||
|
}
|
||
|
|
||
|
fn get_mime_aliases() -> HashMap<Mime, Mime> {
|
||
|
tree_magic_db::aliases()
|
||
|
.lines()
|
||
|
.flat_map(|line| line.split_once(' '))
|
||
|
.flat_map(|(a, b)| Some((Mime::from_str(a).ok()?, Mime::from_str(b).ok()?)))
|
||
|
.collect()
|
||
|
}
|
||
|
|
||
|
pub(crate) fn get_alias(mimetype: &Mime) -> &Mime {
|
||
|
match ALIASES.get(mimetype) {
|
||
|
Some(x) => x,
|
||
|
None => mimetype,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn get_mime_parent_relations() -> Vec<(Mime, Mime)> {
|
||
|
tree_magic_db::subclasses()
|
||
|
.lines()
|
||
|
.flat_map(|line| line.split_once(' '))
|
||
|
.flat_map(|(child, parent)| {
|
||
|
Some((Mime::from_str(child).ok()?, Mime::from_str(parent).ok()?))
|
||
|
})
|
||
|
.collect()
|
||
|
}
|
||
|
|
||
|
fn get_mime_parents(mimetype: &Mime) -> Vec<&Mime> {
|
||
|
PARENTS
|
||
|
.iter()
|
||
|
.filter_map(|(child, parent)| (child == mimetype).then_some(parent))
|
||
|
.collect()
|
||
|
}
|
||
|
|
||
|
pub(crate) fn matches_text(mime: &Mime) -> bool {
|
||
|
if mime.type_() == mime::TEXT {
|
||
|
return true;
|
||
|
}
|
||
|
return get_mime_parents(mime).into_iter().any(matches_text);
|
||
|
}
|