knife_util/lib.rs
1//! # knife-util
2//!
3//! 一个高性能的 Rust 工具库,为 knife 项目提供核心功能支持。
4//!
5//! ## 核心功能
6//!
7//! - **🔄 JSON 深度合并**: 支持类型转换和 `_override_` 机制的高级 JSON 合并
8//! - **🛠️ 统一错误处理**: 结构化的错误类型,支持错误链和上下文信息
9//! - **📁 智能路径管理**: 自动检测工作目录,支持开发和生产环境
10//! - **⚙️ TOML 双向转换**: TOML 与 JSON 之间的无缝转换
11//!
12//! ## 快速开始
13//!
14//! ### JSON 深度合并
15//! ```rust
16//! use knife_util::{merge_json, AppError};
17//! use serde_json::json;
18//!
19//! fn main() -> Result<(), AppError> {
20//! let config = json!({
21//! "database": {"host": "localhost", "port": 5432},
22//! "cache": {"enabled": true}
23//! });
24//!
25//! let override_config = json!({
26//! "database.port._override_": 3306, // 使用 _override_ 强制覆盖
27//! "cache": {"ttl": 3600} // 正常合并
28//! });
29//!
30//! let merged = merge_json(&config, &override_config)?;
31//! println!("合并结果: {}", merged);
32//! Ok(())
33//! }
34//! ```
35//!
36//! ### 智能路径管理
37//! ```rust
38//! use knife_util::{get_work_dir, build_work_path, is_dev_mode, AppError};
39//!
40//! fn main() -> Result<(), AppError> {
41//! // 自动检测工作目录
42//! let work_dir = get_work_dir()?;
43//! println!("工作目录: {:?}", work_dir);
44//!
45//! // 构建项目路径
46//! let config_path = build_work_path("config/app.toml")?;
47//! println!("配置文件路径: {:?}", config_path);
48//!
49//! // 环境检测
50//! if is_dev_mode() {
51//! println!("运行在开发模式");
52//! }
53//! Ok(())
54//! }
55//! ```
56//!
57//! ### TOML 转换工具
58//! ```rust
59//! use knife_util::{toml_to_json, json_to_toml, AppError};
60//! use serde_json::json;
61//!
62//! fn main() -> Result<(), AppError> {
63//! let toml_content = r#"
64//! title = "My Application"
65//! version = "1.0.0"
66//! [database]
67//! host = "localhost"
68//! port = 5432
69//! "#;
70//!
71//! // TOML -> JSON
72//! let json_value = toml_to_json(toml_content)?;
73//! println!("JSON: {}", json_value);
74//!
75//! // JSON -> TOML
76//! let modified_json = json!({
77//! "title": "Updated App",
78//! "version": "2.0.0",
79//! "database": {"host": "prod-server", "port": 3306}
80//! });
81//! let toml_result = json_to_toml(&modified_json)?;
82//! println!("TOML: {}", toml_result);
83//! Ok(())
84//! }
85//! ```
86
87/// 错误处理模块
88pub mod error;
89
90/// JSON合并功能模块
91pub mod merge;
92
93/// 枚举工具模块
94pub mod enums;
95
96/// 路径工具模块
97pub mod path;
98
99/// TOML工具模块
100pub mod toml;
101
102/// 错误类型
103pub use error::AppError;
104
105/// JSON合并功能
106pub use merge::{merge_json, MergePolicy, MergeWithTrait};
107
108/// 枚举工具导出
109pub use enums::EnumTypeTrait;
110
111/// 路径工具
112pub use path::{build_work_path, get_work_dir, is_dev_mode};
113
114/// TOML工具
115pub use toml::{json_to_toml, toml_file_to_json, toml_to_json};