blivemsg 0.2.0

Bilibili Live Message Library
Documentation
# Cargo Features 使用指南

## 📋 概述

`blivemsg` 使用 Cargo Features 实现**库与 CLI 工具的依赖分离**,让库用户可以按需选择功能,最小化依赖体积。

---

## 🎯 核心设计理念

### 1. **库模式(默认)**
```toml
[dependencies]
blivemsg = "0.2.0"
```
- ✅ 最小依赖(约 80 个 crate)
- ✅ 编译速度快
- ✅ 适合集成到其他项目
- ❌ 不包含 CLI 工具
- ❌ 不包含 Protobuf 支持

### 2. **CLI 模式**
```bash
cargo install blivemsg --features cli
```
- ✅ 完整功能(约 120 个 crate)
- ✅ 包含命令行工具
- ✅ 支持 TOML 配置文件
- ❌ 依赖较多

### 3. **Protobuf 模式**
```toml
[dependencies]
blivemsg = { version = "0.2.0", features = ["protobuf-support"] }
```
- ✅ 支持 `INTERACT_WORD_V2` 消息
- ✅ 需要 `prost``bytes``base64`
- ⚠️ 仅当需要此消息类型时启用

---

## 🔧 Feature 详细说明

### `cli` - CLI 工具支持

**额外依赖**:
- `clap` - 命令行参数解析
- `toml` - 配置文件解析

**使用场景**:
```bash
# 安装 CLI 工具
cargo install blivemsg --features cli

# 或直接运行
cargo run --bin blivemsg-cli --features cli -- -r 7734200 -c cookies.json
```

### `protobuf-support` - Protobuf 消息解析

**额外依赖**:
- `prost` - Protobuf 序列化
- `bytes` - 字节缓冲区
- `base64` - Base64 编码

**使用场景**:
```rust
use blivemsg::types::Message;

match message {
    #[cfg(feature = "protobuf-support")]
    Message::InteractWordV2(i) => {
        println!("{} 进入了直播间", i.username);
    }
    _ => {}
}
```

### `emulation` - 浏览器指纹模拟

**额外依赖**:
- `wreq-util` - HTTP 客户端增强

**使用场景**:
高级反爬虫场景,模拟真实浏览器行为。

---

## 📊 依赖对比

| 模式 | 依赖数量 | 编译时间 | 二进制大小 | 适用场景 |
|------|---------|---------|-----------|----------|
| 默认(库) | ~80 ||| 集成到其他项目 |
| + cli | ~120 ||| 独立命令行工具 |
| + protobuf-support | ~95 ||| 需要完整消息支持 |
| 全部启用 | ~135 ||| 开发/测试环境 |

---

## 💡 最佳实践

### 作为库使用者

```toml
# Cargo.toml
[dependencies]
# 基础用法(推荐)
blivemsg = "0.2.0"

# 如果需要 Protobuf 消息
blivemsg = { version = "0.2.0", features = ["protobuf-support"] }
```

### 作为 CLI 开发者

```bash
# 开发时启用所有功能
cargo run --features "cli,protobuf-support" -- -r 7734200 -c cookies.json

# 发布时只启用 CLI
cargo build --release --features cli
```

### 检查工作区

```bash
# 检查库(无 features)
cargo check --lib

# 检查 CLI(带 features)
cargo check --bin blivemsg-cli --features cli

# 检查所有 targets
cargo check --all-targets --features cli
```

---

## 🔍 常见问题

### Q1: 为什么默认不启用 `cli` feature?

**A**: 库用户通常不需要 CLI 工具,启用会增加不必要的依赖(clap、toml 等)。保持默认最小化可以让库更轻量。

### Q2: 如何同时使用库和 CLI?

**A**: 
```toml
[dependencies]
blivemsg = "0.2.0"

[dev-dependencies]
# 测试时可以启用 cli
blivemsg = { version = "0.2.0", features = ["cli"] }
```

### Q3: `protobuf-support` 会影响性能吗?

**A**: 不会。Protobuf 解析仅在收到 `INTERACT_WORD_V2` 消息时触发,其他消息不受影响。

### Q4: 如何查看当前启用的 features?

**A**:
```bash
cargo tree --edges features
```

---

## 📝 发布清单

发布到 crates.io 前确认:

- [ ] 库模式编译通过:`cargo build --lib`
- [ ] CLI 模式编译通过:`cargo build --features cli`
- [ ] 所有 features 组合测试通过
- [ ] Clippy 无警告:`cargo clippy --all-targets --features cli`
- [ ] 文档生成正常:`cargo doc --no-deps`
- [ ] README 中的示例代码可运行

---

## 🔗 相关资源

- [Cargo Features 官方文档]https://doc.rust-lang.org/cargo/reference/features.html
- [crates.io 发布指南]https://doc.rust-lang.org/cargo/reference/publishing.html