fresh_core/
file_explorer.rs1use crate::api::OverlayColorSpec;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4use ts_rs::TS;
5
6#[derive(Debug, Clone, Serialize, Deserialize, TS)]
8#[serde(deny_unknown_fields)]
9#[ts(export)]
10pub struct FileExplorerDecoration {
11 #[ts(type = "string")]
13 pub path: PathBuf,
14 pub symbol: String,
16 pub color: OverlayColorSpec,
18 #[serde(default)]
20 pub priority: i32,
21}
22
23fn default_leading_slot_min_width() -> usize {
24 2
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, TS)]
29#[serde(rename_all = "camelCase", deny_unknown_fields)]
30#[ts(export)]
31pub struct FileExplorerTooltip {
32 pub title: String,
34 pub lines: Vec<String>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize, TS)]
40#[serde(rename_all = "camelCase", deny_unknown_fields)]
41#[ts(export)]
42pub struct FileExplorerLeadingSlot {
43 pub text: String,
45 pub color: OverlayColorSpec,
47 #[serde(default = "default_leading_slot_min_width")]
49 pub min_width: usize,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, TS)]
54#[serde(rename_all = "camelCase", deny_unknown_fields)]
55#[ts(export)]
56pub struct FileExplorerTrailingSlot {
57 pub text: String,
59 pub color: OverlayColorSpec,
61 #[serde(default)]
63 pub tooltip: Option<FileExplorerTooltip>,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize, TS)]
71#[serde(rename_all = "camelCase", deny_unknown_fields)]
72#[ts(export)]
73pub struct FileExplorerSlotEntry {
74 #[ts(type = "string")]
76 pub path: PathBuf,
77 #[serde(default)]
79 pub leading: Option<FileExplorerLeadingSlot>,
80 #[serde(default)]
82 pub suppress_leading: bool,
83 #[serde(default)]
85 pub trailing: Option<FileExplorerTrailingSlot>,
86 #[serde(default)]
88 pub suppress_trailing: bool,
89 #[serde(default)]
91 pub name_color: Option<OverlayColorSpec>,
92 #[serde(default)]
94 pub suppress_name_color: bool,
95 #[serde(default)]
97 pub priority: i32,
98}
99
100#[cfg(feature = "plugins")]
101impl<'js> rquickjs::FromJs<'js> for FileExplorerDecoration {
102 fn from_js(_ctx: &rquickjs::Ctx<'js>, value: rquickjs::Value<'js>) -> rquickjs::Result<Self> {
103 rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
104 from: "object",
105 to: "FileExplorerDecoration",
106 message: Some(e.to_string()),
107 })
108 }
109}
110
111#[cfg(feature = "plugins")]
112impl<'js> rquickjs::FromJs<'js> for FileExplorerSlotEntry {
113 fn from_js(_ctx: &rquickjs::Ctx<'js>, value: rquickjs::Value<'js>) -> rquickjs::Result<Self> {
114 rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
115 from: "object",
116 to: "FileExplorerSlotEntry",
117 message: Some(e.to_string()),
118 })
119 }
120}
121
122#[cfg(all(test, feature = "plugins"))]
123mod tests {
124 use super::*;
125 use rquickjs::{Context, FromJs, Runtime, Value};
126
127 #[test]
131 fn from_js_decodes_all_visible_fields() {
132 let rt = Runtime::new().unwrap();
133 let ctx = Context::full(&rt).unwrap();
134 ctx.with(|ctx| {
135 let v: Value = ctx
136 .eval::<Value, _>(
137 b"({path: '/tmp/a.rs', symbol: 'M', \
138 color: 'ui.file_status_added_fg', priority: 7})"
139 .as_slice(),
140 )
141 .unwrap();
142 let got = FileExplorerDecoration::from_js(&ctx, v).unwrap();
143 assert_eq!(got.path, PathBuf::from("/tmp/a.rs"));
144 assert_eq!(got.symbol, "M");
145 assert_eq!(got.priority, 7);
146 assert_eq!(got.color.as_theme_key(), Some("ui.file_status_added_fg"));
147 });
148 }
149
150 #[test]
151 fn slot_entry_from_js_decodes_suppression_flags() {
152 let rt = Runtime::new().unwrap();
153 let ctx = Context::full(&rt).unwrap();
154 ctx.with(|ctx| {
155 let v: Value = ctx
156 .eval::<Value, _>(
157 br#"({
158 path: '/tmp/a.rs',
159 suppressLeading: true,
160 suppressTrailing: true,
161 suppressNameColor: true,
162 priority: 5
163 })"#,
164 )
165 .unwrap();
166 let got = FileExplorerSlotEntry::from_js(&ctx, v).unwrap();
167 assert_eq!(got.path, PathBuf::from("/tmp/a.rs"));
168 assert!(got.suppress_leading);
169 assert!(got.suppress_trailing);
170 assert!(got.suppress_name_color);
171 assert_eq!(got.priority, 5);
172 });
173 }
174}