|
1 | | -use std::{collections::HashMap, path::PathBuf}; |
| 1 | +use std::{borrow::Cow, collections::HashMap, path::PathBuf}; |
2 | 2 |
|
3 | | -use yazi_shared::url::{Url, UrlScheme}; |
| 3 | +use yazi_fs::File; |
| 4 | +use yazi_shared::{MIME_DIR, url::{Url, UrlScheme}}; |
4 | 5 |
|
5 | 6 | #[derive(Default)] |
6 | 7 | pub struct Mimetype(HashMap<PathBuf, String>); |
7 | 8 |
|
8 | 9 | impl Mimetype { |
9 | 10 | #[inline] |
10 | | - pub fn get(&self, url: &Url) -> Option<&str> { |
11 | | - let s = match url.scheme() { |
| 11 | + pub fn by_url(&self, url: &Url) -> Option<&str> { |
| 12 | + match url.scheme() { |
12 | 13 | UrlScheme::Regular => self.0.get(url.as_path()), |
13 | 14 | UrlScheme::Search => None, |
14 | 15 | UrlScheme::SearchItem => self.0.get(url.as_path()), |
15 | 16 | UrlScheme::Archive => None, |
16 | | - }; |
17 | | - s.map(|s| s.as_str()) |
| 17 | + } |
| 18 | + .map(|s| s.as_str()) |
| 19 | + } |
| 20 | + |
| 21 | + #[inline] |
| 22 | + pub fn by_url_owned(&self, url: &Url) -> Option<Cow<'static, str>> { |
| 23 | + self.by_url(url).map(|s| Cow::Owned(s.to_owned())) |
18 | 24 | } |
19 | 25 |
|
20 | 26 | #[inline] |
21 | | - pub fn get_owned(&self, url: &Url) -> Option<String> { self.get(url).map(|s| s.to_owned()) } |
| 27 | + pub fn by_file(&self, file: &File) -> Option<&str> { |
| 28 | + if file.is_dir() { Some(MIME_DIR) } else { self.by_url(&file.url) } |
| 29 | + } |
| 30 | + |
| 31 | + #[inline] |
| 32 | + pub fn by_file_owned(&self, file: &File) -> Option<Cow<'static, str>> { |
| 33 | + if file.is_dir() { Some(Cow::Borrowed(MIME_DIR)) } else { self.by_url_owned(&file.url) } |
| 34 | + } |
22 | 35 |
|
23 | 36 | #[inline] |
24 | 37 | pub fn contains(&self, url: &Url) -> bool { |
|
0 commit comments