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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! # Host Bridge — PrismHost trait 定义
//!
//! 任何 Mihomo GUI 只需实现 [`PrismHost`] trait,即可接入 Prism Engine。
//! GUI 侧实现量约 80-120 行 Rust 代码。
//!
//! ## 概述
//!
//! 本模块定义了 GUI 客户端与 Prism Engine 之间的桥接接口。[`PrismHost`] trait
//! 是整个 Extension 层的核心抽象,GUI 只需实现 4 个必须方法即可完成接入。
//!
//! ## 必须实现的方法
//!
//! | 方法 | 说明 |
//! |------|------|
//! | [`read_running_config`](PrismHost::read_running_config) | 读取当前运行中的配置(YAML 字符串) |
//! | [`apply_config`](PrismHost::apply_config) | 将处理后的配置写回并触发热重载 |
//! | [`get_prism_workspace`](PrismHost::get_prism_workspace) | 获取 Prism 工作目录路径 |
//! | [`notify`](PrismHost::notify) | 向前端发送事件通知 |
//!
//! ## 可选方法
//!
//! | 方法 | 默认行为 |
//! |------|---------|
//! | [`read_raw_profile`](PrismHost::read_raw_profile) | 返回 `Err("not implemented")` |
//! | [`list_profiles`](PrismHost::list_profiles) | 返回 `Err("not implemented")` |
//! | [`get_core_info`](PrismHost::get_core_info) | 返回 `Err("not implemented")` |
//! | [`validate_config`](PrismHost::validate_config) | 返回 `Err("not implemented")` |
//! | [`script_count`](PrismHost::script_count) | 返回 `Ok(0)` |
//! | [`plugin_count`](PrismHost::plugin_count) | 返回 `Ok(0)` |
//! | [`get_current_profile`](PrismHost::get_current_profile) | 返回 `None`(`__when__.profile` 条件匹配依赖此方法) |
//! | [`get_variables`](PrismHost::get_variables) | 返回空映射(`{{var}}` 模板替换依赖此方法) |
use PathBuf;
/// 配置应用结果
///
/// 表示一次配置写回操作的结果,包含文件保存状态、热重载状态和状态消息。
/// 由 [`PrismHost::apply_config`] 返回。
/// 前端事件通知
///
/// Prism Engine 通过此枚举向 GUI 前端发送各类事件通知,
/// 包括 Patch 执行结果、配置重载状态、规则变更统计和文件监听事件。
///
/// 使用 `#[serde(tag = "type")]` 进行带标签的序列化,
/// 前端可根据 `type` 字段分发处理不同事件。
/// Patch 执行统计
///
/// 记录单个 Patch 执行过程中的变更数量和耗时。
/// 作为 [`PrismEvent::PatchApplied`] 事件的 payload 使用。
/// Profile 信息
///
/// 描述一个 mihomo 配置档案(Profile)的元数据。
/// 通过 [`PrismHost::list_profiles`] 获取所有 Profile 列表。
/// 核心信息
///
/// 描述 mihomo 核心的运行状态和连接参数。
/// 通过 [`PrismHost::get_core_info`] 获取。
///
/// 注意:`Debug` 实现中 `api_secret` 会被脱敏显示为 `"[REDACTED]"`,
/// 防止密钥通过日志意外泄露。
/// 通用 Extension 宿主接口
///
/// 任何 Mihomo GUI 只需实现这个 trait,即可接入 Prism Engine。
/// Tauri 2 + Rust 的 GUI(Zephyr、Clash Verge Rev、Clash Nyanpasu)直接实现;
/// Electron + TypeScript 的 GUI(mihomo-party)可通过 napi-rs 或 CLI 方式桥接。
///
/// # 必须实现的方法
///
/// - [`read_running_config`](PrismHost::read_running_config) — 读取当前运行配置
/// - [`apply_config`](PrismHost::apply_config) — 写回配置并触发热重载
/// - [`get_prism_workspace`](PrismHost::get_prism_workspace) — 获取工作目录
/// - [`notify`](PrismHost::notify) — 发送前端事件通知
///
/// # 线程安全
///
/// 实现 `Send + Sync`,确保可以在多线程环境中安全使用。
///
/// # 示例
///
/// ```rust,ignore
/// use clash_prism_extension::host::{PrismHost, ApplyStatus};
/// use std::path::PathBuf;
///
/// struct MyGuiHost;
///
/// impl PrismHost for MyGuiHost {
/// fn read_running_config(&self) -> Result<String, String> {
/// // 从 GUI 内部状态读取当前配置
/// Ok("rules:\n - MATCH,DIRECT".to_string())
/// }
///
/// fn apply_config(&self, config: &str) -> Result<ApplyStatus, String> {
/// // 将配置写回 GUI 并触发热重载
/// Ok(ApplyStatus {
/// files_saved: true,
/// hot_reload_success: true,
/// message: "配置已更新".to_string(),
/// restarted: false,
/// })
/// }
///
/// fn get_prism_workspace(&self) -> Result<PathBuf, String> {
/// Ok(PathBuf::from("/path/to/prism-workspace"))
/// }
///
/// fn notify(&self, event: clash_prism_extension::PrismEvent) {
/// // 通过 IPC 发送事件到前端
/// println!("Event: {:?}", event);
/// }
/// }
/// ```