yazi/yazi-fs/src/file/file.rs
三咲雅 misaki masa 9accf929f4
Some checks are pending
Cachix / Publish Flake (push) Waiting to run
Check / clippy (push) Waiting to run
Check / rustfmt (push) Waiting to run
Check / stylua (push) Waiting to run
Draft / build-unix (gcc-aarch64-linux-gnu, ubuntu-latest, aarch64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-i686-linux-gnu, ubuntu-latest, i686-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-riscv64-linux-gnu, ubuntu-latest, riscv64gc-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (gcc-sparc64-linux-gnu, ubuntu-latest, sparc64-unknown-linux-gnu) (push) Waiting to run
Draft / build-unix (macos-latest, aarch64-apple-darwin) (push) Waiting to run
Draft / build-unix (macos-latest, x86_64-apple-darwin) (push) Waiting to run
Draft / build-unix (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Waiting to run
Draft / build-windows (windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Draft / build-windows (windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Draft / build-musl (aarch64-unknown-linux-musl) (push) Waiting to run
Draft / build-musl (x86_64-unknown-linux-musl) (push) Waiting to run
Draft / build-snap (amd64, ubuntu-latest) (push) Waiting to run
Draft / build-snap (arm64, ubuntu-24.04-arm) (push) Waiting to run
Draft / snap (push) Blocked by required conditions
Draft / draft (push) Blocked by required conditions
Draft / nightly (push) Blocked by required conditions
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
feat: trash bin (#4144)
2026-07-22 03:13:58 +08:00

86 lines
2 KiB
Rust

use std::{borrow::Cow, ops::Deref, path::Path};
use serde::{Deserialize, Serialize};
use yazi_shared::{path::PathDyn, strand::Strand, url::{AsUrl, Url, UrlBuf, UrlLike}};
use crate::{FsUrl, cha::{Cha, ChaType}, file::FileExtra};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct File {
pub url: UrlBuf,
pub cha: Cha,
#[serde(flatten)]
pub extra: FileExtra,
}
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, extra: Default::default() }
}
#[inline]
pub fn chdir(&self, wd: &Path) -> Self {
Self { url: self.url.rebase(wd), cha: self.cha, extra: self.extra.clone() }
}
#[inline]
pub fn content_path(&self) -> Cow<'_, Path> {
if let Some(backing) = self.extra.backing() {
backing.into()
} else if let Some(local) = self.url.as_local() {
local.into()
} else {
self.url.cache().expect("non-local URL should have a cache path").into()
}
}
}
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() }
}