Skip to content

Commit 5468718

Browse files
authored
feat: promote Id to a first-class type (#2692)
1 parent 2c99075 commit 5468718

File tree

17 files changed

+61
-42
lines changed

17 files changed

+61
-42
lines changed
File renamed without changes.

yazi-binding/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
mod macros;
22

3-
yazi_macro::mod_flat!(error stage url);
3+
yazi_macro::mod_flat!(error id stage url);

yazi-core/src/cmp/cmp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::{collections::HashMap, path::PathBuf};
22

3+
use yazi_shared::Id;
4+
35
#[derive(Default)]
46
pub struct Cmp {
57
pub(super) caches: HashMap<PathBuf, Vec<String>>,
68
pub(super) cands: Vec<String>,
79
pub(super) offset: usize,
810
pub cursor: usize,
911

10-
pub(super) ticket: usize,
12+
pub(super) ticket: Id,
1113
pub visible: bool,
1214
}
1315

yazi-core/src/cmp/commands/show.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, mem, ops::ControlFlow, path::PathBuf};
22

33
use yazi_macro::render;
4-
use yazi_shared::event::{Cmd, CmdCow, Data};
4+
use yazi_shared::{Id, event::{Cmd, CmdCow, Data}};
55

66
use crate::cmp::Cmp;
77

@@ -11,7 +11,7 @@ struct Opt {
1111
cache: Vec<String>,
1212
cache_name: PathBuf,
1313
word: Cow<'static, str>,
14-
ticket: usize,
14+
ticket: Id,
1515
}
1616

1717
impl From<CmdCow> for Opt {
@@ -20,7 +20,7 @@ impl From<CmdCow> for Opt {
2020
cache: c.take_any("cache").unwrap_or_default(),
2121
cache_name: c.take_any("cache-name").unwrap_or_default(),
2222
word: c.take_str("word").unwrap_or_default(),
23-
ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0),
23+
ticket: c.get("ticket").and_then(Data::as_id).unwrap_or_default(),
2424
}
2525
}
2626
}

yazi-core/src/cmp/commands/trigger.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ use std::{borrow::Cow, mem, path::{MAIN_SEPARATOR_STR, PathBuf}};
33
use tokio::fs;
44
use yazi_fs::{CWD, expand_path};
55
use yazi_macro::{emit, render};
6-
use yazi_shared::event::{Cmd, CmdCow, Data};
6+
use yazi_shared::{Id, event::{Cmd, CmdCow, Data}};
77

88
use crate::cmp::Cmp;
99

1010
struct Opt {
1111
word: Cow<'static, str>,
12-
ticket: usize,
12+
ticket: Id,
1313
}
1414

1515
impl From<CmdCow> for Opt {
1616
fn from(mut c: CmdCow) -> Self {
1717
Self {
1818
word: c.take_first_str().unwrap_or_default(),
19-
ticket: c.get("ticket").and_then(Data::as_usize).unwrap_or(0),
19+
ticket: c.get("ticket").and_then(Data::as_id).unwrap_or_default(),
2020
}
2121
}
2222
}

yazi-dds/src/sendable.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ impl Sendable {
4343
Value::UserData(ud) => {
4444
if let Ok(t) = ud.take::<yazi_binding::Url>() {
4545
Data::Url(t.into())
46+
} else if let Ok(t) = ud.borrow::<yazi_binding::Id>() {
47+
Data::Id(**t)
4648
} else if let Ok(t) = ud.take::<yazi_fs::FilesOp>() {
4749
Data::Any(Box::new(t))
4850
} else if let Ok(t) = ud.take::<super::body::BodyYankIter>() {
@@ -107,6 +109,7 @@ impl Sendable {
107109
}
108110
Value::Table(tbl)
109111
}
112+
Data::Id(i) => yazi_binding::Id(*i).into_lua(lua)?,
110113
Data::Url(u) => yazi_binding::Url::new(u.clone()).into_lua(lua)?,
111114
Data::Bytes(b) => Value::String(lua.create_string(b)?),
112115
Data::Any(a) => Value::UserData(if let Some(t) = a.downcast_ref::<yazi_fs::FilesOp>() {
@@ -188,23 +191,15 @@ impl Sendable {
188191
Value::Table(_) => Err("table is not supported".into_lua_err())?,
189192
Value::Function(_) => Err("function is not supported".into_lua_err())?,
190193
Value::Thread(_) => Err("thread is not supported".into_lua_err())?,
191-
Value::UserData(ud) => {
192-
if let Ok(t) = ud.take::<yazi_binding::Url>() {
193-
DataKey::Url(t.into())
194-
} else {
195-
Err("unsupported userdata included".into_lua_err())?
196-
}
197-
}
194+
Value::UserData(_) => Err("userdata is not supported".into_lua_err())?,
198195
Value::Error(_) => Err("error is not supported".into_lua_err())?,
199196
Value::Other(..) => Err("unknown data is not supported".into_lua_err())?,
200197
})
201198
}
202199

200+
#[inline]
203201
fn key_to_value(lua: &Lua, key: DataKey) -> mlua::Result<Value> {
204-
Ok(match key {
205-
DataKey::Url(u) => yazi_binding::Url::new(u).into_lua(lua)?,
206-
key => Self::key_to_value_ref(lua, &key)?,
207-
})
202+
Self::key_to_value_ref(lua, &key)
208203
}
209204

210205
fn key_to_value_ref(lua: &Lua, key: &DataKey) -> mlua::Result<Value> {
@@ -214,7 +209,6 @@ impl Sendable {
214209
DataKey::Integer(i) => Value::Integer(*i),
215210
DataKey::Number(n) => Value::Number(n.get()),
216211
DataKey::String(s) => Value::String(lua.create_string(s.as_ref())?),
217-
DataKey::Url(u) => yazi_binding::Url::new(u.clone()).into_lua(lua)?,
218212
})
219213
}
220214
}

yazi-fm/src/lives/tab.rs

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

33
use mlua::{AnyUserData, UserData, UserDataFields, UserDataMethods, Value};
4-
use yazi_binding::{UrlRef, cached_field};
5-
use yazi_plugin::Id;
4+
use yazi_binding::{Id, UrlRef, cached_field};
65

76
use super::{Finder, Folder, Lives, Mode, Preference, Preview, PtrCell, Selected};
87

yazi-plugin/src/fs/op.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use mlua::{IntoLua, Lua, Table};
2-
use yazi_binding::Url;
2+
use yazi_binding::{Id, Url};
33

4-
use crate::{Id, bindings::Cha, file::File};
4+
use crate::{bindings::Cha, file::File};
55

66
pub(super) struct FilesOp(yazi_fs::FilesOp);
77

yazi-plugin/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod macros;
44

55
yazi_macro::mod_pub!(bindings config elements external file fs isolate loader process pubsub utils);
66

7-
yazi_macro::mod_flat!(clipboard composer id lua runtime);
7+
yazi_macro::mod_flat!(clipboard composer lua runtime);
88

99
pub fn init() -> anyhow::Result<()> {
1010
CLIPBOARD.with(<_>::default);

yazi-plugin/src/pubsub/pubsub.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use mlua::{ExternalResult, Function, Lua, Value};
2+
use yazi_binding::Id;
23
use yazi_dds::body::Body;
34

4-
use crate::{Id, runtime::RtRef};
5+
use crate::runtime::RtRef;
56

67
pub struct Pubsub;
78

0 commit comments

Comments
 (0)