Skip to content

Commit d3ad1c0

Browse files
authored
refactor: refine PathLike and StrandLike traits (#3340)
1 parent 9cd7428 commit d3ad1c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1230
-1440
lines changed

Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ unicode-width = { version = "0.2.0", default-features = false }
5959
uzers = "0.12.1"
6060

6161
[workspace.lints.clippy]
62-
format_push_string = "warn"
63-
implicit_clone = "warn"
64-
module_inception = "allow"
65-
unit_arg = "allow"
66-
use_self = "warn"
62+
format_push_string = "warn"
63+
if_same_then_else = "allow"
64+
implicit_clone = "warn"
65+
len_without_is_empty = "allow"
66+
missing_safety_doc = "allow"
67+
module_inception = "allow"
68+
unit_arg = "allow"
69+
use_self = "warn"

yazi-actor/src/cmp/show.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{mem, ops::ControlFlow};
33
use anyhow::Result;
44
use yazi_macro::{render, succ};
55
use yazi_parser::cmp::{CmpItem, ShowOpt};
6-
use yazi_shared::{data::Data, path::{AsPathDyn, PathDyn, PathLike}, strand::{AsStrand, StrandLike}};
6+
use yazi_shared::{data::Data, path::{AsPath, PathDyn}, strand::StrandLike};
77

88
use crate::{Actor, Ctx};
99

@@ -30,7 +30,7 @@ impl Actor for Show {
3030
};
3131

3232
cmp.ticket = opt.ticket;
33-
cmp.cands = Self::match_candidates(opt.word.as_path_dyn(), cache);
33+
cmp.cands = Self::match_candidates(opt.word.as_path(), cache);
3434
if cmp.cands.is_empty() {
3535
succ!(render!(mem::replace(&mut cmp.visible, false)));
3636
}
@@ -47,16 +47,15 @@ impl Show {
4747
let smart = !word.encoded_bytes().iter().any(|&b| b.is_ascii_uppercase());
4848

4949
let flow = cache.iter().try_fold((Vec::new(), Vec::new()), |(mut exact, mut fuzzy), item| {
50-
let name = item.name.as_strand();
5150
let starts_with =
52-
if smart { name.eq_ignore_ascii_case(word) } else { name.starts_with(word) };
51+
if smart { item.name.eq_ignore_ascii_case(word) } else { item.name.starts_with(word) };
5352

5453
if starts_with {
5554
exact.push(item);
5655
if exact.len() >= LIMIT {
5756
return ControlFlow::Break((exact, fuzzy));
5857
}
59-
} else if fuzzy.len() < LIMIT - exact.len() && name.contains(word) {
58+
} else if fuzzy.len() < LIMIT - exact.len() && item.name.contains(word) {
6059
// Here we don't break the control flow, since we want more exact matching.
6160
fuzzy.push(item)
6261
}

yazi-actor/src/cmp/trigger.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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::{AnyAsciiChar, data::Data, natsort, path::{AsPath, PathBufDyn, PathLike}, scheme::{SchemeCow, SchemeLike}, strand::StrandBufLike, url::{UrlBuf, UrlCow, UrlLike}};
8+
use yazi_shared::{AnyAsciiChar, data::Data, natsort, path::{PathBufDyn, PathDyn, PathLike}, scheme::{SchemeCow, SchemeLike}, strand::StrandLike, url::{UrlBuf, UrlCow, UrlLike}};
99
use yazi_vfs::provider;
1010

1111
use crate::{Actor, Ctx};
@@ -74,28 +74,25 @@ impl Trigger {
7474
}
7575

7676
let sep = if cfg!(windows) {
77-
AnyAsciiChar::new(&[b'/', b'\\']).unwrap()
77+
AnyAsciiChar::new(b"/\\").unwrap()
7878
} else {
79-
AnyAsciiChar::new(&[b'/']).unwrap()
79+
AnyAsciiChar::new(b"/").unwrap()
8080
};
8181

82-
Some(match path.as_path().rsplit_pred(sep) {
82+
Some(match path.rsplit_pred(sep) {
8383
Some((p, c)) if p.is_empty() => {
84-
let root = PathBufDyn::with(scheme.kind(), MAIN_SEPARATOR_STR).expect("valid root");
84+
let root = PathDyn::with(scheme.kind(), MAIN_SEPARATOR_STR).expect("valid root");
8585
(UrlCow::try_from((scheme, root)).ok()?.into_owned(), c.into())
8686
}
87-
Some((p, c)) => {
88-
let parent = PathBufDyn::with(scheme.kind(), p.as_dyn()).expect("valid parent");
89-
(expand_url(UrlCow::try_from((scheme, parent)).ok()?), c.into())
90-
}
87+
Some((p, c)) => (expand_url(UrlCow::try_from((scheme, p)).ok()?), c.into()),
9188
None => (CWD.load().as_ref().clone(), path.into()),
9289
})
9390
}
9491
}
9592

9693
#[cfg(test)]
9794
mod tests {
98-
use yazi_shared::{path::PathBufLike, url::UrlLike};
95+
use yazi_shared::url::UrlLike;
9996

10097
use super::*;
10198

@@ -113,11 +110,19 @@ mod tests {
113110
yazi_fs::init();
114111
compare("", "", "");
115112
compare(" ", "", " ");
113+
116114
compare("/", "/", "");
117-
compare("//", "//", "");
115+
compare("//", "/", "");
116+
compare("///", "/", "");
117+
118118
compare("/foo", "/", "foo");
119+
compare("//foo", "/", "foo");
120+
compare("///foo", "/", "foo");
121+
119122
compare("/foo/", "/foo/", "");
123+
compare("//foo/", "/foo/", "");
120124
compare("/foo/bar", "/foo/", "bar");
125+
compare("///foo/bar", "/foo/", "bar");
121126
}
122127

123128
#[cfg(windows)]

yazi-actor/src/lives/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use mlua::{AnyUserData, IntoLua, UserData, UserDataFields, UserDataMethods, Valu
44
use yazi_binding::Style;
55
use yazi_config::THEME;
66
use yazi_plugin::bindings::Range;
7-
use yazi_shared::{path::PathLike, url::UrlLike};
7+
use yazi_shared::url::UrlLike;
88

99
use super::Lives;
1010
use crate::lives::PtrCell;

yazi-actor/src/lives/tab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::Deref;
22

33
use mlua::{AnyUserData, UserData, UserDataFields, UserDataMethods, Value};
44
use yazi_binding::{Id, UrlRef, cached_field};
5-
use yazi_shared::{path::PathLike, strand::StrandLike, url::UrlLike};
5+
use yazi_shared::url::UrlLike;
66

77
use super::{Finder, Folder, Lives, Mode, Preference, Preview, PtrCell, Selected};
88

yazi-actor/src/mgr/cd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use yazi_fs::{File, FilesOp, path::expand_url};
99
use yazi_macro::{act, err, render, succ};
1010
use yazi_parser::mgr::CdOpt;
1111
use yazi_proxy::{CmpProxy, InputProxy, MgrProxy};
12-
use yazi_shared::{Debounce, data::Data, errors::InputError, path::PathLike, url::{AsUrl, UrlBuf, UrlLike}};
12+
use yazi_shared::{Debounce, data::Data, errors::InputError, url::{AsUrl, UrlBuf, UrlLike}};
1313
use yazi_vfs::VfsFile;
1414

1515
use crate::{Actor, Ctx};
@@ -85,7 +85,7 @@ impl Cd {
8585
}
8686

8787
if let Some(p) = url.parent() {
88-
FilesOp::Upserting(p.into(), [(url.urn().owned(), file)].into()).emit();
88+
FilesOp::Upserting(p.into(), [(url.urn().into(), file)].into()).emit();
8989
}
9090
MgrProxy::reveal(&url);
9191
}

yazi-actor/src/mgr/create.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use yazi_fs::{File, FilesOp};
44
use yazi_macro::{ok_or_not_found, succ};
55
use yazi_parser::mgr::CreateOpt;
66
use yazi_proxy::{ConfirmProxy, InputProxy, MgrProxy};
7-
use yazi_shared::{data::Data, path::PathLike, url::{UrlBuf, UrlLike}};
7+
use yazi_shared::{data::Data, url::{UrlBuf, UrlLike}};
88
use yazi_vfs::{VfsFile, maybe_exists, provider};
99
use yazi_watcher::WATCHER;
1010

@@ -54,7 +54,7 @@ impl Create {
5454
&& let Some((parent, urn)) = real.pair()
5555
{
5656
ok_or_not_found!(provider::remove_file(&new).await);
57-
FilesOp::Deleting(parent.into(), [urn.owned()].into()).emit();
57+
FilesOp::Deleting(parent.into(), [urn.into()].into()).emit();
5858
provider::create(&new).await?;
5959
} else if let Some(parent) = new.parent() {
6060
provider::create_dir_all(parent).await.ok();
@@ -68,7 +68,7 @@ impl Create {
6868
&& let Some((parent, urn)) = real.pair()
6969
{
7070
let file = File::new(&real).await?;
71-
FilesOp::Upserting(parent.into(), [(urn.owned(), file)].into()).emit();
71+
FilesOp::Upserting(parent.into(), [(urn.into(), file)].into()).emit();
7272
MgrProxy::reveal(&real);
7373
}
7474

yazi-actor/src/mgr/filter_do.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Result;
22
use yazi_fs::Filter;
33
use yazi_macro::{act, render, succ};
44
use yazi_parser::mgr::FilterOpt;
5-
use yazi_shared::{data::Data, path::PathLike};
5+
use yazi_shared::data::Data;
66

77
use crate::{Actor, Ctx};
88

@@ -16,7 +16,7 @@ impl Actor for FilterDo {
1616
fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> {
1717
let filter = if opt.query.is_empty() { None } else { Some(Filter::new(&opt.query, opt.case)?) };
1818

19-
let hovered = cx.hovered().map(|f| f.urn().owned());
19+
let hovered = cx.hovered().map(|f| f.urn().into());
2020
cx.current_mut().files.set_filter(filter);
2121

2222
if cx.hovered().map(|f| f.urn()) != hovered.as_ref().map(Into::into) {

yazi-actor/src/mgr/hidden.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use yazi_core::tab::Folder;
33
use yazi_fs::FolderStage;
44
use yazi_macro::{act, render, render_and, succ};
55
use yazi_parser::mgr::HiddenOpt;
6-
use yazi_shared::{data::Data, path::PathLike};
6+
use yazi_shared::data::Data;
77

88
use crate::{Actor, Ctx};
99

@@ -18,7 +18,7 @@ impl Actor for Hidden {
1818
let state = opt.state.bool(cx.tab().pref.show_hidden);
1919
cx.tab_mut().pref.show_hidden = state;
2020

21-
let hovered = cx.hovered().map(|f| f.urn().owned());
21+
let hovered = cx.hovered().map(|f| f.urn().to_owned());
2222
let apply = |f: &mut Folder| {
2323
if f.stage == FolderStage::Loading {
2424
render!();

yazi-actor/src/mgr/rename.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use yazi_fs::{File, FilesOp};
55
use yazi_macro::{act, err, ok_or_not_found, succ};
66
use yazi_parser::mgr::RenameOpt;
77
use yazi_proxy::{ConfirmProxy, InputProxy, MgrProxy};
8-
use yazi_shared::{Id, data::Data, path::PathLike, strand::StrandLike, url::{UrlBuf, UrlLike}};
8+
use yazi_shared::{Id, data::Data, url::{UrlBuf, UrlLike}};
99
use yazi_vfs::{VfsFile, maybe_exists, provider};
1010
use yazi_watcher::WATCHER;
1111

@@ -73,21 +73,22 @@ impl Rename {
7373
provider::rename(&old, &new).await?;
7474

7575
if let Ok(u) = overwritten
76+
&& u != new
7677
&& let Some((parent, urn)) = u.pair()
7778
{
7879
ok_or_not_found!(provider::rename(&u, &new).await);
79-
FilesOp::Deleting(parent.to_owned(), [urn.owned()].into()).emit();
80+
FilesOp::Deleting(parent.to_owned(), [urn.into()].into()).emit();
8081
}
8182

8283
let new = provider::casefold(&new).await?;
8384
let Some((new_p, new_n)) = new.pair() else { return Ok(()) };
8485

8586
let file = File::new(&new).await?;
8687
if new_p == old_p {
87-
FilesOp::Upserting(old_p.into(), [(old_n.owned(), file)].into()).emit();
88+
FilesOp::Upserting(old_p.into(), [(old_n.into(), file)].into()).emit();
8889
} else {
89-
FilesOp::Deleting(old_p.into(), [old_n.owned()].into()).emit();
90-
FilesOp::Upserting(new_p.into(), [(new_n.owned(), file)].into()).emit();
90+
FilesOp::Deleting(old_p.into(), [old_n.into()].into()).emit();
91+
FilesOp::Upserting(new_p.into(), [(new_n.into(), file)].into()).emit();
9192
}
9293

9394
MgrProxy::reveal(&new);

0 commit comments

Comments
 (0)