parse-book-source 0.5.0

Terminal reader for novel
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! 确定性 transform 算子(`clean` 流水线):编解码 / 哈希(含 HMAC)/ 对称加解密 / 繁简。
//!
//! 全部纯函数、纯 Rust、零 C 依赖(crypto 钉在 RustCrypto cipher 0.4 / digest 0.10 代)。
//! 这些 fn 既供 `eval::apply_clean` 调用,也作为后续 JS 引擎 `crypto` 对象的单一后端。
//!
//! 失败语义:编解码/解密是「配置或数据错误」,显式返回 `EvalError::Codec`/`Crypto`,
//! 而非静默空——便于书源作者定位(对比「选择器无匹配返回空」)。

use crate::error::EvalError;
use crate::source::{
    ByteEnc, CipherAlgo, CipherMode, CipherOp, CipherStep, CnConvert, Codec, HashAlgo, HashOut,
    HashStep, Padding,
};
use base64::Engine;
use base64::alphabet;
use base64::engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig};
use std::collections::{BTreeMap, HashMap};
use std::sync::LazyLock;

// 解码对填充宽容(真实书源 base64 常缺 `=`);编码按规范带填充。
static B64_STD: LazyLock<GeneralPurpose> = LazyLock::new(|| {
    GeneralPurpose::new(
        &alphabet::STANDARD,
        GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
    )
});
static B64_URL: LazyLock<GeneralPurpose> = LazyLock::new(|| {
    GeneralPurpose::new(
        &alphabet::URL_SAFE,
        GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
    )
});

// ───────────────────────── 编 / 解码 ─────────────────────────

/// 解码当前串(base64/base64url/hex/url)→ 文本(字节按 UTF-8 有损解释)。
pub fn decode(s: &str, codec: Codec) -> Result<String, EvalError> {
    Ok(String::from_utf8_lossy(&decode_bytes(s, codec)?).into_owned())
}

/// 编码当前串的 UTF-8 字节(base64/base64url/hex/url)。
///
/// 实际不会失败;返回 `Result` 仅为与 [`decode`] 在 clean 流水线 / JS `crypto` 绑定中保持统一签名。
pub fn encode(s: &str, codec: Codec) -> Result<String, EvalError> {
    Ok(match codec {
        Codec::Base64 => B64_STD.encode(s.as_bytes()),
        Codec::Base64url => B64_URL.encode(s.as_bytes()),
        Codec::Hex => hex::encode(s.as_bytes()),
        Codec::Url => {
            percent_encoding::utf8_percent_encode(s, percent_encoding::NON_ALPHANUMERIC).to_string()
        }
    })
}

fn decode_bytes(s: &str, codec: Codec) -> Result<Vec<u8>, EvalError> {
    match codec {
        Codec::Base64 => B64_STD
            .decode(s.trim())
            .map_err(|e| EvalError::Codec(format!("base64: {e}"))),
        Codec::Base64url => B64_URL
            .decode(s.trim())
            .map_err(|e| EvalError::Codec(format!("base64url: {e}"))),
        Codec::Hex => hex::decode(s.trim()).map_err(|e| EvalError::Codec(format!("hex: {e}"))),
        Codec::Url => Ok(percent_encoding::percent_decode_str(s).collect()),
    }
}

/// 按字节编码把串解释为字节(crypto 的 key/iv/输入)。
fn to_bytes(s: &str, enc: ByteEnc) -> Result<Vec<u8>, EvalError> {
    match enc {
        ByteEnc::Utf8 | ByteEnc::Raw => Ok(s.as_bytes().to_vec()),
        ByteEnc::Base64 => B64_STD
            .decode(s.trim())
            .map_err(|e| EvalError::Codec(format!("base64: {e}"))),
        ByteEnc::Hex => hex::decode(s.trim()).map_err(|e| EvalError::Codec(format!("hex: {e}"))),
    }
}

/// 把字节按编码渲染为串(crypto 的输出)。
fn from_bytes(b: &[u8], enc: ByteEnc) -> String {
    match enc {
        ByteEnc::Utf8 | ByteEnc::Raw => String::from_utf8_lossy(b).into_owned(),
        ByteEnc::Base64 => B64_STD.encode(b),
        ByteEnc::Hex => hex::encode(b),
    }
}

// ───────────────────────── 哈希 / HMAC ─────────────────────────

/// 计算哈希或 HMAC(提供 `hmacKey` 时),按 `output` 编码输出。
pub fn hash(s: &str, step: &HashStep) -> Result<String, EvalError> {
    let data = s.as_bytes();
    let digest = match &step.hmac_key {
        Some(k) => hmac_digest(step.algo, &to_bytes(k, step.hmac_key_enc)?, data),
        None => plain_digest(step.algo, data),
    };
    Ok(match step.output {
        HashOut::Hex => hex::encode(&digest),
        HashOut::Base64 => B64_STD.encode(&digest),
    })
}

fn plain_digest(algo: HashAlgo, data: &[u8]) -> Vec<u8> {
    use md5::Md5;
    use sha1::Sha1;
    use sha2::{Digest, Sha256, Sha512};
    match algo {
        HashAlgo::Md5 => Md5::digest(data).to_vec(),
        HashAlgo::Sha1 => Sha1::digest(data).to_vec(),
        HashAlgo::Sha256 => Sha256::digest(data).to_vec(),
        HashAlgo::Sha512 => Sha512::digest(data).to_vec(),
    }
}

fn hmac_digest(algo: HashAlgo, key: &[u8], data: &[u8]) -> Vec<u8> {
    use hmac::{Hmac, Mac};
    // HMAC 接受任意长度密钥,new_from_slice 不会失败。
    macro_rules! mac {
        ($h:ty) => {{
            let mut m = <Hmac<$h>>::new_from_slice(key).expect("HMAC accepts any key length");
            m.update(data);
            m.finalize().into_bytes().to_vec()
        }};
    }
    match algo {
        HashAlgo::Md5 => mac!(md5::Md5),
        HashAlgo::Sha1 => mac!(sha1::Sha1),
        HashAlgo::Sha256 => mac!(sha2::Sha256),
        HashAlgo::Sha512 => mac!(sha2::Sha512),
    }
}

// ───────────────────────── 对称加解密 ─────────────────────────

/// 解/加密当前串。默认值贴合解密主场景(见 [`CipherStep`])。
pub fn cipher(s: &str, st: &CipherStep) -> Result<String, EvalError> {
    let input_enc = st.input_enc.unwrap_or(match st.op {
        CipherOp::Decrypt => ByteEnc::Base64,
        CipherOp::Encrypt => ByteEnc::Utf8,
    });
    let output_enc = st.output_enc.unwrap_or(match st.op {
        CipherOp::Decrypt => ByteEnc::Utf8,
        CipherOp::Encrypt => ByteEnc::Base64,
    });
    let data = to_bytes(s, input_enc)?;
    let key = to_bytes(&st.key, st.key_enc)?;
    let iv = match &st.iv {
        Some(iv) => to_bytes(iv, st.iv_enc)?,
        None => Vec::new(),
    };
    let out = match st.mode {
        CipherMode::Gcm => gcm(st, &key, &iv, &data)?,
        CipherMode::Cfb => cfb(st, &key, &iv, &data)?,
        CipherMode::Cbc | CipherMode::Ecb => block(st, &key, &iv, &data)?,
    };
    Ok(from_bytes(&out, output_enc))
}

fn keyiv(e: impl std::fmt::Display) -> EvalError {
    EvalError::Crypto(format!("invalid key/iv length: {e}"))
}
fn unpad(e: impl std::fmt::Display) -> EvalError {
    EvalError::Crypto(format!("decrypt/unpad failed: {e}"))
}

/// CBC/ECB(带 padding)。展开 mode×op×padding 全分支(由调用方按 key 长度选具体分组密码)。
macro_rules! run_block {
    ($bc:ty, $st:expr, $key:expr, $iv:expr, $data:expr) => {{
        use cipher::block_padding::{NoPadding, Pkcs7, ZeroPadding};
        use cipher::{BlockDecryptMut, BlockEncryptMut, KeyInit, KeyIvInit};
        match ($st.mode, $st.op) {
            (CipherMode::Cbc, CipherOp::Decrypt) => {
                let c = cbc::Decryptor::<$bc>::new_from_slices($key, $iv).map_err(keyiv)?;
                match $st.padding {
                    Padding::Pkcs7 => c.decrypt_padded_vec_mut::<Pkcs7>($data).map_err(unpad),
                    Padding::Zero => c
                        .decrypt_padded_vec_mut::<ZeroPadding>($data)
                        .map_err(unpad),
                    Padding::None => c.decrypt_padded_vec_mut::<NoPadding>($data).map_err(unpad),
                }
            }
            (CipherMode::Cbc, CipherOp::Encrypt) => {
                let c = cbc::Encryptor::<$bc>::new_from_slices($key, $iv).map_err(keyiv)?;
                Ok(match $st.padding {
                    Padding::Pkcs7 => c.encrypt_padded_vec_mut::<Pkcs7>($data),
                    Padding::Zero => c.encrypt_padded_vec_mut::<ZeroPadding>($data),
                    Padding::None => c.encrypt_padded_vec_mut::<NoPadding>($data),
                })
            }
            (CipherMode::Ecb, CipherOp::Decrypt) => {
                let c = ecb::Decryptor::<$bc>::new_from_slice($key).map_err(keyiv)?;
                match $st.padding {
                    Padding::Pkcs7 => c.decrypt_padded_vec_mut::<Pkcs7>($data).map_err(unpad),
                    Padding::Zero => c
                        .decrypt_padded_vec_mut::<ZeroPadding>($data)
                        .map_err(unpad),
                    Padding::None => c.decrypt_padded_vec_mut::<NoPadding>($data).map_err(unpad),
                }
            }
            (CipherMode::Ecb, CipherOp::Encrypt) => {
                let c = ecb::Encryptor::<$bc>::new_from_slice($key).map_err(keyiv)?;
                Ok(match $st.padding {
                    Padding::Pkcs7 => c.encrypt_padded_vec_mut::<Pkcs7>($data),
                    Padding::Zero => c.encrypt_padded_vec_mut::<ZeroPadding>($data),
                    Padding::None => c.encrypt_padded_vec_mut::<NoPadding>($data),
                })
            }
            // cipher() 只把 cbc/ecb 路由到 block();此分支防御未来重构误接其它模式,
            // 显式报错而非 panic(库不应 panic,与本模块「显式错误」哲学一致)。
            _ => Err(EvalError::Crypto(format!(
                "block() 不支持的加密模式: {:?}",
                $st.mode
            ))),
        }
    }};
}

fn block(st: &CipherStep, key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, EvalError> {
    match st.algo {
        CipherAlgo::Aes => match key.len() {
            16 => run_block!(aes::Aes128, st, key, iv, data),
            24 => run_block!(aes::Aes192, st, key, iv, data),
            32 => run_block!(aes::Aes256, st, key, iv, data),
            n => Err(EvalError::Crypto(format!(
                "AES key must be 16/24/32 bytes, got {n}"
            ))),
        },
        CipherAlgo::Des => match key.len() {
            8 => run_block!(des::Des, st, key, iv, data),
            n => Err(EvalError::Crypto(format!(
                "DES key must be 8 bytes, got {n}"
            ))),
        },
        CipherAlgo::TripleDes => match key.len() {
            24 => run_block!(des::TdesEde3, st, key, iv, data),
            16 => run_block!(des::TdesEde2, st, key, iv, data),
            n => Err(EvalError::Crypto(format!(
                "3DES key must be 16/24 bytes, got {n}"
            ))),
        },
    }
}

/// CFB(128 位反馈,对齐 CryptoJS 默认;无 padding,流式)。
fn cfb(st: &CipherStep, key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, EvalError> {
    use cfb_mode::cipher::{AsyncStreamCipher, KeyIvInit};
    let mut buf = data.to_vec();
    macro_rules! go {
        ($bc:ty) => {{
            match st.op {
                CipherOp::Decrypt => cfb_mode::Decryptor::<$bc>::new_from_slices(key, iv)
                    .map_err(keyiv)?
                    .decrypt(&mut buf),
                CipherOp::Encrypt => cfb_mode::Encryptor::<$bc>::new_from_slices(key, iv)
                    .map_err(keyiv)?
                    .encrypt(&mut buf),
            }
        }};
    }
    match st.algo {
        CipherAlgo::Aes => match key.len() {
            16 => go!(aes::Aes128),
            24 => go!(aes::Aes192),
            32 => go!(aes::Aes256),
            n => {
                return Err(EvalError::Crypto(format!(
                    "AES key must be 16/24/32 bytes, got {n}"
                )));
            }
        },
        CipherAlgo::Des => match key.len() {
            8 => go!(des::Des),
            n => {
                return Err(EvalError::Crypto(format!(
                    "DES key must be 8 bytes, got {n}"
                )));
            }
        },
        CipherAlgo::TripleDes => match key.len() {
            24 => go!(des::TdesEde3),
            16 => go!(des::TdesEde2),
            n => {
                return Err(EvalError::Crypto(format!(
                    "3DES key must be 16/24 bytes, got {n}"
                )));
            }
        },
    }
    Ok(buf)
}

/// AES-GCM(AEAD;nonce=IV 须 12 字节;密文 = 密文‖tag,与 Java/CryptoJS 一致)。
fn gcm(st: &CipherStep, key: &[u8], iv: &[u8], data: &[u8]) -> Result<Vec<u8>, EvalError> {
    use aes_gcm::aead::generic_array::GenericArray;
    use aes_gcm::aead::{Aead, KeyInit};
    use aes_gcm::{Aes128Gcm, Aes256Gcm};
    if st.algo != CipherAlgo::Aes {
        return Err(EvalError::Crypto("GCM 仅支持 AES".into()));
    }
    if iv.len() != 12 {
        return Err(EvalError::Crypto(format!(
            "AES-GCM nonce 须 12 字节,实际 {}",
            iv.len()
        )));
    }
    let nonce = GenericArray::from_slice(iv);
    macro_rules! go {
        ($t:ty) => {{
            let c = <$t>::new_from_slice(key).map_err(keyiv)?;
            match st.op {
                CipherOp::Decrypt => c
                    .decrypt(nonce, data)
                    .map_err(|e| EvalError::Crypto(format!("gcm decrypt: {e}"))),
                CipherOp::Encrypt => c
                    .encrypt(nonce, data)
                    .map_err(|e| EvalError::Crypto(format!("gcm encrypt: {e}"))),
            }
        }};
    }
    match key.len() {
        16 => go!(Aes128Gcm),
        32 => go!(Aes256Gcm),
        n => Err(EvalError::Crypto(format!(
            "AES-GCM key must be 16/32 bytes, got {n}"
        ))),
    }
}

// ───────────────────────── 字体反爬还原 ─────────────────────────

/// 字体反爬还原:把文本中的私有区(PUA)字符按映射表换回真字(表外字符原样保留)。
/// `table` 键为码点十六进制(容忍 `U+` 前缀),值取首字符;键非法时报错。
/// 表是数据,由书源内联提供(引擎不内置任何站点的表),可用 `trn gen-fontmap` 生成。
pub fn font_map(s: &str, table: &BTreeMap<String, String>) -> Result<String, EvalError> {
    let map = build_font_map(table)?;
    Ok(remap(s, &map))
}

/// 把「码点十六进制 → 目标字符串」表转为 `char → char` 表(值取首字符;空值跳过)。
fn build_font_map(raw: &BTreeMap<String, String>) -> Result<HashMap<char, char>, EvalError> {
    let mut map = HashMap::with_capacity(raw.len());
    for (k, v) in raw {
        if let Some(to) = v.chars().next() {
            map.insert(parse_code_point(k)?, to);
        }
    }
    Ok(map)
}

/// 解析码点十六进制串(容忍 `U+`/`u+` 前缀)为字符。
fn parse_code_point(k: &str) -> Result<char, EvalError> {
    let hex = k
        .strip_prefix("U+")
        .or_else(|| k.strip_prefix("u+"))
        .unwrap_or(k);
    let cp = u32::from_str_radix(hex, 16)
        .map_err(|_| EvalError::Font(format!("invalid code point: {k}")))?;
    char::from_u32(cp).ok_or_else(|| EvalError::Font(format!("invalid code point: {k}")))
}

/// 按映射表逐字符替换(表外字符保留)。
fn remap(s: &str, map: &HashMap<char, char>) -> String {
    s.chars()
        .map(|c| map.get(&c).copied().unwrap_or(c))
        .collect()
}

// ───────────────────────── 繁简转换 ─────────────────────────

/// 繁简转换(t2s/s2t)。词典加载失败(理论不会,内置词典)时退化为原样返回。
pub fn cn_convert(s: &str, dir: CnConvert) -> String {
    use ferrous_opencc::OpenCC;
    use ferrous_opencc::config::BuiltinConfig;
    static T2S: LazyLock<Option<OpenCC>> =
        LazyLock::new(|| OpenCC::from_config(BuiltinConfig::T2s).ok());
    static S2T: LazyLock<Option<OpenCC>> =
        LazyLock::new(|| OpenCC::from_config(BuiltinConfig::S2t).ok());
    let cc = match dir {
        CnConvert::T2s => &*T2S,
        CnConvert::S2t => &*S2T,
    };
    cc.as_ref()
        .map(|c| c.convert(s))
        .unwrap_or_else(|| s.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::source::{ByteEnc, CipherAlgo, CipherMode, CipherOp, CipherStep, Padding};

    #[test]
    fn base64_round_trip() {
        assert_eq!(encode("hello", Codec::Base64).unwrap(), "aGVsbG8=");
        assert_eq!(decode("aGVsbG8=", Codec::Base64).unwrap(), "hello");
        // 缺填充也能解。
        assert_eq!(decode("aGVsbG8", Codec::Base64).unwrap(), "hello");
    }

    #[test]
    fn hex_and_url() {
        assert_eq!(decode("68656c6c6f", Codec::Hex).unwrap(), "hello");
        assert_eq!(encode("a b&c", Codec::Url).unwrap(), "a%20b%26c");
        assert_eq!(decode("a%20b%26c", Codec::Url).unwrap(), "a b&c");
    }

    #[test]
    fn invalid_base64_errors() {
        assert!(matches!(
            decode("!!!notbase64!!!", Codec::Base64),
            Err(EvalError::Codec(_))
        ));
    }

    fn hashstep(algo: HashAlgo, out: HashOut, key: Option<&str>) -> HashStep {
        HashStep {
            algo,
            output: out,
            hmac_key: key.map(|s| s.to_string()),
            hmac_key_enc: ByteEnc::Utf8,
        }
    }

    #[test]
    fn md5_sha_known_vectors() {
        assert_eq!(
            hash("hello", &hashstep(HashAlgo::Md5, HashOut::Hex, None)).unwrap(),
            "5d41402abc4b2a76b9719d911017c592"
        );
        assert_eq!(
            hash("hello", &hashstep(HashAlgo::Sha256, HashOut::Hex, None)).unwrap(),
            "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
        );
    }

    #[test]
    fn hmac_sha256_known_vector() {
        // RFC-ish 已知向量。
        let v = hash(
            "The quick brown fox jumps over the lazy dog",
            &hashstep(HashAlgo::Sha256, HashOut::Hex, Some("key")),
        )
        .unwrap();
        assert_eq!(
            v,
            "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
        );
    }

    fn aes_cbc(op: CipherOp, in_enc: ByteEnc, out_enc: ByteEnc) -> CipherStep {
        CipherStep {
            algo: CipherAlgo::Aes,
            mode: CipherMode::Cbc,
            padding: Padding::Pkcs7,
            op,
            key: "0123456789abcdef".into(),
            key_enc: ByteEnc::Utf8,
            iv: Some("abcdef9876543210".into()),
            iv_enc: ByteEnc::Utf8,
            input_enc: Some(in_enc),
            output_enc: Some(out_enc),
        }
    }

    #[test]
    fn aes_cbc_round_trip() {
        let plain = "蛊真人正文:第一章";
        let ct = cipher(
            plain,
            &aes_cbc(CipherOp::Encrypt, ByteEnc::Utf8, ByteEnc::Base64),
        )
        .unwrap();
        let back = cipher(
            &ct,
            &aes_cbc(CipherOp::Decrypt, ByteEnc::Base64, ByteEnc::Utf8),
        )
        .unwrap();
        assert_eq!(back, plain);
    }

    #[test]
    fn aes_cbc_known_ciphertext_decrypts() {
        // 由上面的加密路径产出的固定密文,验证 decrypt 独立正确。
        let plain = "hello world";
        let ct = cipher(
            plain,
            &aes_cbc(CipherOp::Encrypt, ByteEnc::Utf8, ByteEnc::Base64),
        )
        .unwrap();
        let back = cipher(
            &ct,
            &aes_cbc(CipherOp::Decrypt, ByteEnc::Base64, ByteEnc::Utf8),
        )
        .unwrap();
        assert_eq!(back, plain);
    }

    #[test]
    fn aes_gcm_round_trip() {
        let mk = |op| CipherStep {
            algo: CipherAlgo::Aes,
            mode: CipherMode::Gcm,
            padding: Padding::None,
            op,
            key: "0123456789abcdef".into(),
            key_enc: ByteEnc::Utf8,
            iv: Some("0123456789ab".into()), // 12 字节
            iv_enc: ByteEnc::Utf8,
            input_enc: None,
            output_enc: None,
        };
        let ct = cipher("secret内容", &mk(CipherOp::Encrypt)).unwrap();
        let back = cipher(&ct, &mk(CipherOp::Decrypt)).unwrap();
        assert_eq!(back, "secret内容");
    }

    #[test]
    fn cn_t2s_s2t() {
        assert_eq!(cn_convert("漢字測試", CnConvert::T2s), "汉字测试");
        assert_eq!(cn_convert("汉字测试", CnConvert::S2t), "漢字測試");
    }

    #[test]
    fn font_map_restores_pua_via_table() {
        let mut t = BTreeMap::new();
        t.insert("E4DE".into(), "".into());
        t.insert("E4F3".into(), "".into());
        t.insert("U+E001".into(), "".into()); // 容忍 U+ 前缀
        // 表内字符替换,表外字符(普通中文/标点)原样保留。
        assert_eq!(
            font_map("\u{E4DE}\u{E4F3}\u{E001}x,y", &t).unwrap(),
            "一的甲x,y"
        );
    }

    #[test]
    fn font_map_invalid_code_point_errors() {
        let mut t = BTreeMap::new();
        t.insert("ZZZZ".into(), "".into()); // 非法十六进制 → 报错
        assert!(matches!(font_map("x", &t), Err(EvalError::Font(_))));
    }
}