clash_prism_extension/lib.rs
1//! # clash-prism-extension — UI Extension 通用接口
2//!
3//! 三层架构中的 Layer 2(Host Bridge),提供 GUI 客户端接入 Prism Engine 的统一接口。
4//!
5//! ## 概述
6//!
7//! `clash-prism-extension` 是 Prism Engine 的 GUI 集成层。任何 Mihomo GUI 客户端
8//! 只需实现 [`PrismHost`] trait(约 80-120 行 Rust 代码),即可获得完整的
9//! Prism 配置编译、规则管理、文件监听等能力。
10//!
11//! ## 架构
12//!
13//! ```text
14//! ┌───────────────────────────────────────────────────────────────┐
15//! │ Layer 3: Frontend JSON API (Tauri IPC / Electron IPC / HTTP) │
16//! │ ← React/Vue/Svelte 前端调用 │
17//! ├───────────────────────────────────────────────────────────────┤
18//! │ Layer 2: Host Bridge (Rust trait PrismHost) ← 本 crate │
19//! │ ← GUI 实现适配层,桥接 Prism 和 GUI 内部 API │
20//! ├───────────────────────────────────────────────────────────────┤
21//! │ Layer 1: Prism Core Engine (clash-prism-core / clash-prism-dsl / ...) │
22//! │ ← 纯 Rust 库,无 GUI 依赖 │
23//! └───────────────────────────────────────────────────────────────┘
24//! ```
25//!
26//! ## 核心类型
27//!
28//! | 类型 | 说明 |
29//! |------|------|
30//! | [`PrismHost`] | GUI 宿主接口 trait(GUI 必须实现) |
31//! | [`PrismExtension`] | Extension 主入口(提供高层 API) |
32//! | [`ApplyOptions`] | 编译选项 |
33//! | [`ApplyResult`] | 编译结果 |
34//! | [`PrismStatus`] | Extension 运行状态 |
35//! | [`RuleGroup`] | 规则分组(供 GUI 展示和管理) |
36//! | [`RuleAnnotation`] | 规则注解(标记规则归属) |
37//!
38//! ## 模块结构
39//!
40//! | 模块 | 说明 |
41//! |------|------|
42//! | [`host`] | [`PrismHost`] trait 和宿主相关类型 |
43//! | [`extension`] | [`PrismExtension`] 主入口结构 |
44//! | [`types`] | API 数据结构定义 |
45//! | [`annotation`] | 规则注解提取和分组 |
46//!
47//! ## 快速开始
48//!
49//! ```rust,ignore
50//! use clash_prism_extension::{PrismHost, PrismExtension, ApplyOptions};
51//!
52//! struct MyGuiHost { /* ... */ }
53//!
54//! impl PrismHost for MyGuiHost {
55//! fn read_running_config(&self) -> Result<String, String> { /* ... */ }
56//! fn apply_config(&self, config: &str) -> Result<ApplyStatus, String> { /* ... */ }
57//! fn get_prism_workspace(&self) -> Result<std::path::PathBuf, String> { /* ... */ }
58//! fn notify(&self, event: PrismEvent) { /* ... */ }
59//! }
60//!
61//! let ext = PrismExtension::new(host);
62//! let result = ext.apply(ApplyOptions::default())?;
63//! ```
64//!
65//! ## 主要 API
66//!
67//! ```rust,ignore
68//! // 执行编译
69//! let result = ext.apply(ApplyOptions::default())?;
70//!
71//! // 查看运行状态
72//! let status = ext.status();
73//!
74//! // 列出规则组
75//! let groups = ext.list_rules()?;
76//!
77//! // 启用/禁用规则组
78//! ext.toggle_group("ad-filter.prism.yaml", false)?;
79//!
80//! // 判断规则归属
81//! let info = ext.is_prism_rule(5)?;
82//! ```
83//!
84//! ## Feature Flags
85//!
86//! | Feature | 说明 |
87//! |---------|------|
88//! | `watcher` | 启用文件监听功能(依赖 `notify` crate) |
89
90mod annotation;
91mod extension;
92mod host;
93mod types;
94mod variables;
95
96pub use extension::{IsPrismRule, PrismExtension};
97pub use host::{ApplyStatus, CoreInfo, PatchStats, PrismEvent, PrismHost, ProfileInfo};
98pub use types::{
99 ApplyOptions, ApplyResult, CompileStats, PrismStatus, RuleAnnotation, RuleDiff, RuleGroup,
100 RuleInsertPosition, TraceView,
101};