-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathop.rs
More file actions
148 lines (131 loc) · 4.12 KB
/
Copy pathop.rs
File metadata and controls
148 lines (131 loc) · 4.12 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::collections::{HashMap, HashSet};
use yazi_shared::{Id, Ids, Layer, event::Cmd, url::{Url, UrnBuf}};
use super::{Cha, File};
pub static FILES_TICKET: Ids = Ids::new();
#[derive(Clone, Debug)]
pub enum FilesOp {
Full(Url, Vec<File>, Cha),
Part(Url, Vec<File>, Id),
Done(Url, Cha, Id),
Size(Url, HashMap<UrnBuf, u64>),
IOErr(Url, std::io::ErrorKind),
Creating(Url, Vec<File>),
Deleting(Url, HashSet<UrnBuf>),
Updating(Url, HashMap<UrnBuf, File>),
Upserting(Url, HashMap<UrnBuf, File>),
}
impl FilesOp {
#[inline]
pub fn cwd(&self) -> &Url {
match self {
Self::Full(u, ..) => u,
Self::Part(u, ..) => u,
Self::Done(u, ..) => u,
Self::Size(u, _) => u,
Self::IOErr(u, _) => u,
Self::Creating(u, _) => u,
Self::Deleting(u, _) => u,
Self::Updating(u, _) => u,
Self::Upserting(u, _) => u,
}
}
#[inline]
pub fn emit(self) {
yazi_shared::event::Event::Call(
Cmd::new("update_files").with_any("op", self).into(),
Layer::Manager,
)
.emit();
}
pub fn prepare(cwd: &Url) -> Id {
let ticket = FILES_TICKET.next();
Self::Part(cwd.clone(), vec![], ticket).emit();
ticket
}
pub fn rename(map: HashMap<Url, File>) {
let mut parents: HashMap<_, (HashSet<_>, HashMap<_, _>)> = Default::default();
for (o, n) in map {
let Some(o_p) = o.parent_url() else { continue };
let Some(n_p) = n.url.parent_url() else { continue };
if o_p != n_p {
parents.entry(o_p).or_default().0.insert(o.urn_owned());
}
parents.entry(n_p).or_default().1.insert(n.urn_owned(), n);
}
for (p, (o, n)) in parents {
match (o.is_empty(), n.is_empty()) {
(true, true) => unreachable!(),
(true, false) => Self::Upserting(p, n).emit(),
(false, true) => Self::Deleting(p, o).emit(),
(false, false) => {
Self::Deleting(p.clone(), o).emit();
Self::Upserting(p, n).emit();
}
}
}
}
pub fn create(files: Vec<File>) {
let mut parents: HashMap<_, Vec<_>> = Default::default();
for file in files {
let Some(parent) = file.url.parent_url() else { continue };
parents.entry(parent).or_default().push(file);
}
for (parent, files) in parents {
Self::Creating(parent, files).emit();
}
}
pub fn mutate(ops: Vec<Self>) {
let mut parents: HashMap<_, (HashMap<_, _>, HashSet<_>)> = Default::default();
for op in ops {
match op {
Self::Upserting(p, map) => parents.entry(p).or_default().0.extend(map),
Self::Deleting(p, urns) => parents.entry(p).or_default().1.extend(urns),
_ => unreachable!(),
}
}
for (p, (u, d)) in parents {
match (u.is_empty(), d.is_empty()) {
(true, true) => unreachable!(),
(true, false) => Self::Deleting(p, d).emit(),
(false, true) => Self::Upserting(p, u).emit(),
(false, false) => {
Self::Deleting(p.clone(), d).emit();
Self::Upserting(p, u).emit();
}
}
}
}
pub fn rebase(&self, new: &Url) -> Self {
macro_rules! files {
($files:expr) => {{ $files.iter().map(|f| f.rebase(new)).collect() }};
}
macro_rules! map {
($map:expr) => {{ $map.iter().map(|(u, f)| (u.clone(), f.rebase(new))).collect() }};
}
let n = new.clone();
match self {
Self::Full(_, files, cha) => Self::Full(n, files!(files), *cha),
Self::Part(_, files, ticket) => Self::Part(n, files!(files), *ticket),
Self::Done(_, cha, ticket) => Self::Done(n, *cha, *ticket),
Self::Size(_, map) => Self::Size(n, map.iter().map(|(u, &s)| (u.clone(), s)).collect()),
Self::IOErr(_, err) => Self::IOErr(n, *err),
Self::Creating(_, files) => Self::Creating(n, files!(files)),
Self::Deleting(_, urns) => Self::Deleting(n, urns.clone()),
Self::Updating(_, map) => Self::Updating(n, map!(map)),
Self::Upserting(_, map) => Self::Upserting(n, map!(map)),
}
}
pub fn diff_recoverable(&self, contains: impl Fn(&Url) -> bool) -> (Vec<Url>, Vec<Url>) {
match self {
Self::Deleting(cwd, urns) => (urns.iter().map(|u| cwd.join(u)).collect(), vec![]),
Self::Updating(cwd, urns) | Self::Upserting(cwd, urns) => urns
.iter()
.filter(|&(u, f)| u != f.urn())
.map(|(u, f)| (cwd.join(u), f))
.filter(|(u, _)| contains(u))
.map(|(u, f)| (u, f.url_owned()))
.unzip(),
_ => (vec![], vec![]),
}
}
}