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;
10pub mod json_like;
11mod pipe_fun;
12pub mod quotation;
13pub mod registry;
14
15use base64::Base64Proc;
16use bom::BomClearProc;
17use hex::HexProc;
18use json_like::JsonLikeProc;
19use quotation::EscQuotaProc;
20
21#[derive(Serialize, Deserialize, Debug)]
22pub struct PipeLineResult {
23 pub name: String,
24 pub result: String,
25}
26
27pub fn raw_to_utf8_string(data: &RawData) -> String {
28 match data {
29 RawData::String(s) => s.clone(),
30 RawData::Bytes(b) => String::from_utf8_lossy(b).into_owned(),
31 RawData::ArcBytes(b) => String::from_utf8_lossy(b).into_owned(),
32 }
33}
34
35static BUILTIN_PIPE_INIT: Once = Once::new();
36
37fn decode_base64_stage() -> PipeHold {
38 Arc::new(Base64Proc)
39}
40
41fn decode_hex_stage() -> PipeHold {
42 Arc::new(HexProc)
43}
44
45fn unquote_unescape_stage() -> PipeHold {
46 Arc::new(EscQuotaProc)
47}
48
49fn bom_strip_stage() -> PipeHold {
50 Arc::new(BomClearProc)
51}
52
53fn json_like_stage() -> PipeHold {
54 Arc::new(JsonLikeProc)
55}
56
57pub fn ensure_builtin_pipe_units() {
59 BUILTIN_PIPE_INIT.call_once(|| {
60 registry::register_pipe_unit("decode/base64", decode_base64_stage);
61 registry::register_pipe_unit("decode/hex", decode_hex_stage);
62 registry::register_pipe_unit("unquote/unescape", unquote_unescape_stage);
63 registry::register_pipe_unit("strip/bom", bom_strip_stage);
64 registry::register_pipe_unit("json_like", json_like_stage);
65 });
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_builtin_pipe_units_registered() {
74 ensure_builtin_pipe_units();
75
76 let units = registry::list_pipe_units();
78
79 assert!(
80 units
81 .iter()
82 .any(|name| name.to_uppercase() == "DECODE/BASE64")
83 );
84 assert!(units.iter().any(|name| name.to_uppercase() == "DECODE/HEX"));
85 assert!(
86 units
87 .iter()
88 .any(|name| name.to_uppercase() == "UNQUOTE/UNESCAPE")
89 );
90 assert!(units.iter().any(|name| name.to_uppercase() == "STRIP/BOM"));
91 assert!(units.iter().any(|name| name.to_uppercase() == "JSON_LIKE"));
92 }
93
94 #[test]
95 fn test_strip_bom_can_be_created() {
96 ensure_builtin_pipe_units();
97
98 let processor = registry::create_pipe_unit("strip/bom");
100 assert!(processor.is_some());
101
102 if let Some(proc) = processor {
104 assert_eq!(proc.name(), "strip/bom");
105 }
106 }
107}