-
Notifications
You must be signed in to change notification settings - Fork 471
/
Copy pathfile.rs
78 lines (62 loc) · 1.6 KB
/
file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::{ffi::OsStr, fs::{FileType, Metadata}, ops::Deref};
use anyhow::Result;
use tokio::fs;
use yazi_shared::{SyncCell, theme::IconCache, url::{Url, Urn, UrnBuf}};
use crate::Cha;
#[derive(Clone, Debug, Default)]
pub struct File {
pub url: Url,
pub cha: Cha,
pub link_to: Option<Url>,
pub icon: SyncCell<IconCache>,
}
impl Deref for File {
type Target = Cha;
#[inline]
fn deref(&self) -> &Self::Target { &self.cha }
}
impl File {
#[inline]
pub async fn from(url: Url) -> Result<Self> {
let meta = fs::symlink_metadata(&url).await?;
Ok(Self::from_meta(url, meta).await)
}
#[inline]
pub async fn from_meta(url: Url, meta: Metadata) -> Self {
let link_to =
if meta.is_symlink() { fs::read_link(&url).await.map(Url::from).ok() } else { None };
let cha = Cha::new(&url, meta).await;
Self { url, cha, link_to, icon: Default::default() }
}
#[inline]
pub fn from_dummy(url: Url, ft: Option<FileType>) -> Self {
Self {
url,
cha: ft.map_or_else(Cha::dummy, Cha::from),
link_to: None,
icon: Default::default(),
}
}
#[inline]
pub fn rebase(&self, parent: &Url) -> Self {
Self {
url: self.url.rebase(parent),
cha: self.cha,
link_to: self.link_to.clone(),
icon: Default::default(),
}
}
}
impl File {
// --- Url
#[inline]
pub fn url_owned(&self) -> Url { self.url.to_owned() }
#[inline]
pub fn urn(&self) -> &Urn { self.url.urn() }
#[inline]
pub fn urn_owned(&self) -> UrnBuf { self.url.urn_owned() }
#[inline]
pub fn name(&self) -> &OsStr { self.url.name() }
#[inline]
pub fn stem(&self) -> Option<&OsStr> { self.url.file_stem() }
}