mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
84 lines
1.9 KiB
Rust
84 lines
1.9 KiB
Rust
use std::{borrow::Cow, hash::{Hash, Hasher}, ops::Deref, path::Path};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use yazi_shared::{path::{PathBufDyn, PathDyn}, strand::Strand, url::{AsUrl, Url, UrlBuf, UrlLike}};
|
|
|
|
use crate::cha::{Cha, ChaType};
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
pub struct File {
|
|
pub url: UrlBuf,
|
|
pub cha: Cha,
|
|
pub link_to: Option<PathBufDyn>,
|
|
}
|
|
|
|
impl Deref for File {
|
|
type Target = Cha;
|
|
|
|
fn deref(&self) -> &Self::Target { &self.cha }
|
|
}
|
|
|
|
impl From<&Self> for File {
|
|
fn from(value: &Self) -> Self { value.clone() }
|
|
}
|
|
|
|
impl From<File> for Cow<'_, File> {
|
|
fn from(value: File) -> Self { Cow::Owned(value) }
|
|
}
|
|
|
|
impl<'a> From<&'a File> for Cow<'a, File> {
|
|
fn from(value: &'a File) -> Self { Cow::Borrowed(value) }
|
|
}
|
|
|
|
impl AsUrl for File {
|
|
fn as_url(&self) -> Url<'_> { self.url.as_url() }
|
|
}
|
|
|
|
impl AsUrl for &File {
|
|
fn as_url(&self) -> Url<'_> { self.url.as_url() }
|
|
}
|
|
|
|
impl File {
|
|
#[inline]
|
|
pub fn from_dummy(url: impl Into<UrlBuf>, r#type: Option<ChaType>) -> Self {
|
|
let url = url.into();
|
|
let cha = Cha::from_dummy(&url, r#type);
|
|
Self { url, cha, link_to: None }
|
|
}
|
|
|
|
#[inline]
|
|
pub fn chdir(&self, wd: &Path) -> Self {
|
|
Self { url: self.url.rebase(wd), cha: self.cha, link_to: self.link_to.clone() }
|
|
}
|
|
}
|
|
|
|
impl File {
|
|
// --- Url
|
|
#[inline]
|
|
pub fn url_owned(&self) -> UrlBuf { self.url.clone() }
|
|
|
|
#[inline]
|
|
pub fn uri(&self) -> PathDyn<'_> { self.url.uri() }
|
|
|
|
#[inline]
|
|
pub fn urn(&self) -> PathDyn<'_> { self.url.urn() }
|
|
|
|
#[inline]
|
|
pub fn entry_key(&self) -> PathDyn<'_> { self.url.entry_key() }
|
|
|
|
#[inline]
|
|
pub fn name(&self) -> Option<Strand<'_>> { self.url.name() }
|
|
|
|
#[inline]
|
|
pub fn stem(&self) -> Option<Strand<'_>> { self.url.stem() }
|
|
}
|
|
|
|
impl Hash for File {
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
self.url.hash(state);
|
|
self.cha.len.hash(state);
|
|
self.cha.btime.hash(state);
|
|
self.cha.ctime.hash(state);
|
|
self.cha.mtime.hash(state);
|
|
}
|
|
}
|