Skip to content

Commit 604b866

Browse files
authored
refactor: simplify paths (#3297)
1 parent b766cff commit 604b866

File tree

30 files changed

+180
-317
lines changed

30 files changed

+180
-317
lines changed

yazi-actor/src/cmp/trigger.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::{ffi::OsString, mem, path::MAIN_SEPARATOR_STR};
1+
use std::{ffi::OsString, mem, path::{MAIN_SEPARATOR_STR, PathBuf}};
22

33
use anyhow::Result;
44
use yazi_fs::{CWD, path::expand_url, provider::{DirReader, FileHolder}};
55
use yazi_macro::{act, render, succ};
66
use yazi_parser::cmp::{CmpItem, ShowOpt, TriggerOpt};
77
use yazi_proxy::CmpProxy;
8-
use yazi_shared::{OsStrSplit, data::Data, natsort, scheme::SchemeLike, url::{UrlBuf, UrlCow, UrnBuf}};
8+
use yazi_shared::{OsStrSplit, data::Data, natsort, scheme::SchemeLike, url::{UrlBuf, UrlCow}};
99
use yazi_vfs::provider;
1010

1111
use crate::{Actor, Ctx};
@@ -67,7 +67,7 @@ impl Actor for Trigger {
6767
}
6868

6969
impl Trigger {
70-
fn split_url(s: &str) -> Option<(UrlBuf, UrnBuf)> {
70+
fn split_url(s: &str) -> Option<(UrlBuf, PathBuf)> {
7171
let (scheme, path, ..) = UrlCow::parse(s.as_bytes()).ok()?;
7272

7373
if scheme.is_local() && path.as_os_str() == "~" {
@@ -91,16 +91,16 @@ impl Trigger {
9191

9292
#[cfg(test)]
9393
mod tests {
94-
use yazi_shared::url::{UrlLike, Urn};
94+
use yazi_shared::url::UrlLike;
9595

9696
use super::*;
9797

9898
fn compare(s: &str, parent: &str, child: &str) {
9999
let (mut p, c) = Trigger::split_url(s).unwrap();
100100
if let Some(u) = p.strip_prefix(yazi_fs::CWD.load().as_ref()) {
101-
p = UrlBuf::from(&**u);
101+
p = UrlBuf::from(u);
102102
}
103-
assert_eq!((p, c.as_urn()), (parent.parse().unwrap(), Urn::new(child)));
103+
assert_eq!((p, c.to_str().unwrap()), (parent.parse().unwrap(), child));
104104
}
105105

106106
#[cfg(unix)]

yazi-binding/src/url.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use mlua::{AnyUserData, ExternalError, FromLua, Lua, MetaMethod, UserData, UserD
44
use yazi_fs::{FsHash64, FsHash128};
55
use yazi_shared::{IntoOsStr, scheme::SchemeLike, url::{AsUrl, UrlCow, UrlLike}};
66

7-
use crate::{Scheme, Urn, cached_field, deprecate};
7+
use crate::{Scheme, cached_field, deprecate};
88

99
pub type UrlRef = UserDataRef<Url>;
1010

@@ -108,7 +108,10 @@ impl UserData for Url {
108108
me.ext().map(|s| lua.create_string(s.as_encoded_bytes())).transpose()
109109
});
110110
cached_field!(fields, parent, |_, me| Ok(me.parent().map(Self::new)));
111-
cached_field!(fields, urn, |_, me| Ok(Urn::new(me.urn())));
111+
cached_field!(fields, urn, |_, me| {
112+
// FIXME: remove type inference
113+
Ok(super::Path::<std::path::PathBuf>::new(me.urn()))
114+
});
112115
cached_field!(fields, base, |_, me| Ok(me.base().map(Self::new)));
113116

114117
cached_field!(fields, scheme, |_, me| Ok(Scheme::new(&me.scheme)));

yazi-binding/src/urn.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,34 @@ use std::{ops::Deref, path::PathBuf};
33
use mlua::{ExternalError, FromLua, Lua, UserData, Value};
44
use yazi_shared::path::PathBufLike;
55

6-
pub struct Urn<P: PathBufLike = PathBuf> {
7-
inner: yazi_shared::url::UrnBuf<P>,
8-
}
6+
pub struct Path<P: PathBufLike = PathBuf>(pub P);
97

10-
impl<P> Deref for Urn<P>
8+
impl<P> Deref for Path<P>
119
where
1210
P: PathBufLike,
1311
{
14-
type Target = yazi_shared::url::UrnBuf<P>;
15-
16-
fn deref(&self) -> &Self::Target { &self.inner }
17-
}
12+
type Target = P;
1813

19-
impl<P> From<Urn<P>> for yazi_shared::url::UrnBuf<P>
20-
where
21-
P: PathBufLike,
22-
{
23-
fn from(value: Urn<P>) -> Self { value.inner }
14+
fn deref(&self) -> &Self::Target { &self.0 }
2415
}
2516

26-
impl<P> Urn<P>
17+
impl<P> Path<P>
2718
where
2819
P: PathBufLike,
2920
{
30-
pub fn new(urn: impl Into<yazi_shared::url::UrnBuf<P>>) -> Self { Self { inner: urn.into() } }
21+
pub fn new(urn: impl Into<P>) -> Self { Self(urn.into()) }
3122
}
3223

33-
impl<P> FromLua for Urn<P>
24+
impl<P> FromLua for Path<P>
3425
where
3526
P: PathBufLike,
3627
{
3728
fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
3829
Ok(match value {
3930
Value::UserData(ud) => ud.take()?,
40-
_ => Err("Expected a Urn".into_lua_err())?,
31+
_ => Err("Expected a Path".into_lua_err())?,
4132
})
4233
}
4334
}
4435

45-
impl<P> UserData for Urn<P> where P: PathBufLike {}
36+
impl<P> UserData for Path<P> where P: PathBufLike {}

yazi-boot/src/boot.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use futures::executor::block_on;
44
use hashbrown::HashSet;
55
use serde::Serialize;
66
use yazi_fs::{CWD, Xdg, path::expand_url};
7-
use yazi_shared::url::{UrlBuf, UrlCow, UrlLike, UrnBuf};
7+
use yazi_shared::url::{UrlBuf, UrlCow, UrlLike};
88
use yazi_vfs::provider;
99

1010
#[derive(Debug, Default, Serialize)]
1111
pub struct Boot {
1212
pub cwds: Vec<UrlBuf>,
13-
pub files: Vec<UrnBuf>,
13+
pub files: Vec<PathBuf>,
1414

1515
pub local_events: HashSet<String>,
1616
pub remote_events: HashSet<String>,
@@ -22,25 +22,25 @@ pub struct Boot {
2222
}
2323

2424
impl Boot {
25-
async fn parse_entries(entries: &[UrlBuf]) -> (Vec<UrlBuf>, Vec<UrnBuf>) {
25+
async fn parse_entries(entries: &[UrlBuf]) -> (Vec<UrlBuf>, Vec<PathBuf>) {
2626
if entries.is_empty() {
27-
return (vec![CWD.load().as_ref().clone()], vec![UrnBuf::default()]);
27+
return (vec![CWD.load().as_ref().clone()], vec![PathBuf::default()]);
2828
}
2929

30-
async fn go(entry: &UrlBuf) -> (UrlBuf, UrnBuf) {
30+
async fn go(entry: &UrlBuf) -> (UrlBuf, PathBuf) {
3131
let mut entry = expand_url(entry);
3232
if let Ok(u @ UrlCow::Owned { .. }) = provider::absolute(&entry).await {
3333
entry = u.into_owned();
3434
}
3535

3636
let Some((parent, child)) = entry.pair() else {
37-
return (entry, UrnBuf::default());
37+
return (entry, PathBuf::default());
3838
};
3939

4040
if provider::metadata(&entry).await.is_ok_and(|m| m.is_file()) {
4141
(parent.into(), child.to_owned())
4242
} else {
43-
(entry, UrnBuf::default())
43+
(entry, PathBuf::default())
4444
}
4545
}
4646

yazi-core/src/tab/finder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use std::path::{Path, PathBuf};
2+
13
use anyhow::Result;
24
use hashbrown::HashMap;
35
use yazi_fs::{Files, Filter, FilterCase};
4-
use yazi_shared::url::{UrlBuf, Urn, UrnBuf};
6+
use yazi_shared::url::UrlBuf;
57

68
use crate::tab::Folder;
79

810
pub struct Finder {
911
pub filter: Filter,
10-
pub matched: HashMap<UrnBuf, u8>,
12+
pub matched: HashMap<PathBuf, u8>,
1113
lock: FinderLock,
1214
}
1315

@@ -76,7 +78,7 @@ impl Finder {
7678
}
7779

7880
impl Finder {
79-
pub fn matched_idx(&self, folder: &Folder, urn: &Urn) -> Option<u8> {
81+
pub fn matched_idx(&self, folder: &Folder, urn: &Path) -> Option<u8> {
8082
if self.lock == *folder { self.matched.get(urn).copied() } else { None }
8183
}
8284
}

yazi-core/src/tab/folder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::mem;
1+
use std::{mem, path::{Path, PathBuf}};
22

33
use yazi_config::{LAYOUT, YAZI};
44
use yazi_dds::Pubsub;
55
use yazi_fs::{File, Files, FilesOp, FolderStage, cha::Cha};
66
use yazi_macro::err;
77
use yazi_parser::Step;
88
use yazi_proxy::MgrProxy;
9-
use yazi_shared::{Id, url::{UrlBuf, Urn, UrnBuf}};
9+
use yazi_shared::{Id, url::UrlBuf};
1010
use yazi_widgets::Scrollable;
1111

1212
pub struct Folder {
@@ -19,7 +19,7 @@ pub struct Folder {
1919
pub cursor: usize,
2020

2121
pub page: usize,
22-
pub trace: Option<UrnBuf>,
22+
pub trace: Option<PathBuf>,
2323
}
2424

2525
impl Default for Folder {
@@ -108,7 +108,7 @@ impl Folder {
108108
b
109109
}
110110

111-
pub fn hover(&mut self, urn: &Urn) -> bool {
111+
pub fn hover(&mut self, urn: &Path) -> bool {
112112
if self.hovered().map(|h| h.urn()) == Some(urn) {
113113
return self.arrow(0);
114114
}
@@ -117,7 +117,7 @@ impl Folder {
117117
self.arrow(new - self.cursor as isize)
118118
}
119119

120-
pub fn repos(&mut self, urn: Option<&Urn>) -> bool {
120+
pub fn repos(&mut self, urn: Option<&Path>) -> bool {
121121
if let Some(u) = urn {
122122
self.hover(u)
123123
} else if let Some(u) = &self.trace {

yazi-dds/src/sendable.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Sendable {
4646
Some(t) if t == TypeId::of::<yazi_binding::Url>() => {
4747
Data::Url(ud.take::<yazi_binding::Url>()?.into())
4848
}
49-
Some(t) if t == TypeId::of::<yazi_binding::Urn>() => {
50-
Data::Urn(ud.take::<yazi_binding::Urn>()?.into())
49+
Some(t) if t == TypeId::of::<yazi_binding::Path<std::path::PathBuf>>() => {
50+
Data::Path(ud.take::<yazi_binding::Path<std::path::PathBuf>>()?.0)
5151
}
5252
Some(t) if t == TypeId::of::<yazi_binding::Id>() => {
5353
Data::Id(**ud.borrow::<yazi_binding::Id>()?)
@@ -83,7 +83,7 @@ impl Sendable {
8383
Value::Table(tbl)
8484
}
8585
Data::Url(u) => yazi_binding::Url::new(u).into_lua(lua)?,
86-
Data::Urn(u) => yazi_binding::Urn::new(u).into_lua(lua)?,
86+
Data::Path(u) => yazi_binding::Path::<std::path::PathBuf>::new(u).into_lua(lua)?,
8787
Data::Any(a) => {
8888
if a.is::<yazi_fs::FilesOp>() {
8989
lua.create_any_userdata(*a.downcast::<yazi_fs::FilesOp>().unwrap())?.into_lua(lua)?
@@ -121,7 +121,7 @@ impl Sendable {
121121
}
122122
Data::Id(i) => yazi_binding::Id(*i).into_lua(lua)?,
123123
Data::Url(u) => yazi_binding::Url::new(u.clone()).into_lua(lua)?,
124-
Data::Urn(u) => yazi_binding::Urn::new(u.clone()).into_lua(lua)?,
124+
Data::Path(u) => yazi_binding::Path::<std::path::PathBuf>::new(u).into_lua(lua)?,
125125
Data::Bytes(b) => Value::String(lua.create_string(b)?),
126126
Data::Any(a) => {
127127
if let Some(t) = a.downcast_ref::<yazi_fs::FilesOp>() {
@@ -214,8 +214,8 @@ impl Sendable {
214214
Some(t) if t == TypeId::of::<yazi_binding::Url>() => {
215215
DataKey::Url(ud.take::<yazi_binding::Url>()?.into())
216216
}
217-
Some(t) if t == TypeId::of::<yazi_binding::Urn>() => {
218-
DataKey::Urn(ud.take::<yazi_binding::Urn>()?.into())
217+
Some(t) if t == TypeId::of::<yazi_binding::Path>() => {
218+
DataKey::Path(ud.take::<yazi_binding::Path>()?.0)
219219
}
220220
Some(t) if t == TypeId::of::<yazi_binding::Id>() => {
221221
DataKey::Id(**ud.borrow::<yazi_binding::Id>()?)
@@ -230,7 +230,7 @@ impl Sendable {
230230
fn key_to_value(lua: &Lua, key: DataKey) -> mlua::Result<Value> {
231231
match key {
232232
DataKey::Url(u) => yazi_binding::Url::new(u).into_lua(lua),
233-
DataKey::Urn(u) => yazi_binding::Urn::new(u).into_lua(lua),
233+
DataKey::Path(u) => yazi_binding::Path::<std::path::PathBuf>::new(u).into_lua(lua),
234234
_ => Self::key_to_value_ref(lua, &key),
235235
}
236236
}
@@ -244,7 +244,7 @@ impl Sendable {
244244
DataKey::String(s) => Value::String(lua.create_string(s.as_ref())?),
245245
DataKey::Id(i) => yazi_binding::Id(*i).into_lua(lua)?,
246246
DataKey::Url(u) => yazi_binding::Url::new(u.clone()).into_lua(lua)?,
247-
DataKey::Urn(u) => yazi_binding::Urn::new(u.clone()).into_lua(lua)?,
247+
DataKey::Path(u) => yazi_binding::Path::<std::path::PathBuf>::new(u).into_lua(lua)?,
248248
DataKey::Bytes(b) => Value::String(lua.create_string(b)?),
249249
})
250250
}

yazi-fs/src/cha/cha.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ impl Cha {
5959
let mut kind = ChaKind::DUMMY;
6060
let mode = r#type.map(ChaMode::from_bare).unwrap_or_default();
6161

62+
#[cfg(unix)]
63+
use yazi_shared::path::PathLike;
6264
#[cfg(unix)]
6365
if _url.as_url().urn().is_hidden() {
6466
kind |= ChaKind::HIDDEN;

yazi-fs/src/file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{ffi::OsStr, hash::{Hash, Hasher}, ops::Deref, path::{Path, PathBuf}};
22

3-
use yazi_shared::url::{Uri, UrlBuf, UrlLike, Urn};
3+
use yazi_shared::url::{UrlBuf, UrlLike};
44

55
use crate::cha::{Cha, ChaType};
66

@@ -37,10 +37,10 @@ impl File {
3737
pub fn url_owned(&self) -> UrlBuf { self.url.to_owned() }
3838

3939
#[inline]
40-
pub fn uri(&self) -> &Uri { self.url.uri() }
40+
pub fn uri(&self) -> &Path { self.url.uri() }
4141

4242
#[inline]
43-
pub fn urn(&self) -> &Urn { self.url.urn() }
43+
pub fn urn(&self) -> &Path { self.url.urn() }
4444

4545
#[inline]
4646
pub fn name(&self) -> Option<&OsStr> { self.url.name() }

0 commit comments

Comments
 (0)