Skip to main content

wpl/eval/builtins/
mod.rs

1use std::fmt::Debug;
2use std::sync::{Arc, Once};
3
4use wp_model_core::raw::RawData;
5use wp_parse_api::PipeHold;
6
7pub mod base64;
8pub mod bom;
9pub mod hex;
10mod pipe_fun;
11pub mod quotation;
12pub mod registry;
13
14use base64::Base64Proc;
15use bom::BomClearProc;
16use hex::HexProc;
17use quotation::EscQuotaProc;
18
19#[derive(Serialize, Deserialize, Debug)]
20pub struct PipeLineResult {
21    pub name: String,
22    pub result: String,
23}
24
25pub fn raw_to_utf8_string(data: &RawData) -> String {
26    match data {
27        RawData::String(s) => s.clone(),
28        RawData::Bytes(b) => String::from_utf8_lossy(b).into_owned(),
29        RawData::ArcBytes(b) => String::from_utf8_lossy(b).into_owned(),
30    }
31}
32
33static BUILTIN_PIPE_INIT: Once = Once::new();
34
35fn decode_base64_stage() -> PipeHold {
36    Arc::new(Base64Proc)
37}
38
39fn decode_hex_stage() -> PipeHold {
40    Arc::new(HexProc)
41}
42
43fn unquote_unescape_stage() -> PipeHold {
44    Arc::new(EscQuotaProc)
45}
46
47fn bom_strip_stage() -> PipeHold {
48    Arc::new(BomClearProc)
49}
50
51/// Ensure core decode/unquote pipe units are registered in the plg_pipe registry.
52pub fn ensure_builtin_pipe_units() {
53    BUILTIN_PIPE_INIT.call_once(|| {
54        registry::register_pipe_unit("decode/base64", decode_base64_stage);
55        registry::register_pipe_unit("decode/hex", decode_hex_stage);
56        registry::register_pipe_unit("unquote/unescape", unquote_unescape_stage);
57        registry::register_pipe_unit("strip/bom", bom_strip_stage);
58    });
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_builtin_pipe_units_registered() {
67        ensure_builtin_pipe_units();
68
69        // 验证所有内置处理器都已注册
70        let units = registry::list_pipe_units();
71
72        assert!(
73            units
74                .iter()
75                .any(|name| name.to_uppercase() == "DECODE/BASE64")
76        );
77        assert!(units.iter().any(|name| name.to_uppercase() == "DECODE/HEX"));
78        assert!(
79            units
80                .iter()
81                .any(|name| name.to_uppercase() == "UNQUOTE/UNESCAPE")
82        );
83        assert!(units.iter().any(|name| name.to_uppercase() == "STRIP/BOM"));
84    }
85
86    #[test]
87    fn test_strip_bom_can_be_created() {
88        ensure_builtin_pipe_units();
89
90        // 验证可以创建 strip/bom 处理器
91        let processor = registry::create_pipe_unit("strip/bom");
92        assert!(processor.is_some());
93
94        // 验证处理器名称
95        if let Some(proc) = processor {
96            assert_eq!(proc.name(), "strip/bom");
97        }
98    }
99}