Skip to main content

wpl/eval/builtins/
base64.rs

1use base64::{Engine as _, engine::general_purpose};
2use bytes::Bytes;
3use orion_error::{ErrorOwe, ErrorWith};
4use std::sync::Arc;
5
6use wp_model_core::raw::RawData;
7use wp_parse_api::{PipeProcessor, WparseResult};
8
9#[derive(Debug)]
10pub struct Base64Proc;
11
12impl PipeProcessor for Base64Proc {
13    /// Decodes Base64-encoded data while preserving the input container type.
14    /// For string inputs, attempts UTF-8 conversion but falls back to bytes on invalid UTF-8.
15    fn process(&self, data: RawData) -> WparseResult<RawData> {
16        match data {
17            RawData::String(s) => {
18                let decoded = general_purpose::STANDARD
19                    .decode(s.as_bytes())
20                    .owe_data()
21                    .want("base64 decode")?;
22                let vstring = String::from_utf8(decoded).owe_data().want("to-json")?;
23                Ok(RawData::from_string(vstring))
24            }
25            RawData::Bytes(b) => {
26                let decoded = general_purpose::STANDARD
27                    .decode(b.as_ref())
28                    .owe_data()
29                    .want("base64 decode")?;
30                Ok(RawData::Bytes(Bytes::from(decoded)))
31            }
32            RawData::ArcBytes(b) => {
33                let decoded = general_purpose::STANDARD
34                    .decode(b.as_ref())
35                    .owe_data()
36                    .want("base64 decode")?;
37                // 注意:RawData::ArcBytes 现在使用 Arc<Vec<u8>>
38                Ok(RawData::ArcBytes(Arc::new(decoded)))
39            }
40        }
41    }
42
43    fn name(&self) -> &'static str {
44        "decode/base64"
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::types::AnyResult;
51
52    use super::*;
53
54    #[test]
55    fn test_base64() -> AnyResult<()> {
56        let data = RawData::from_string("aGVsbG8=".to_string());
57        let y = Base64Proc.process(data)?;
58        assert_eq!(crate::eval::builtins::raw_to_utf8_string(&y), "hello");
59
60        // Test with a longer complex string that should decode to readable text
61        let data = RawData::from_string("VGhpcyBpcyBhIHRlc3Qgb2YgYSBiYXNlNjQgZW5jb2RlZCBzdHJpbmcgd2l0aCBzcGVjaWFsIGNoYXJhY3RlcnMhIEBfXyUgJiYqKys=".to_string());
62        let what = Base64Proc.process(data)?;
63        let decoded = crate::eval::builtins::raw_to_utf8_string(&what);
64        assert!(decoded.starts_with(
65            "This is a test of a base64 encoded string with special characters! @__% &&*++"
66        ));
67
68        let bytes_data = RawData::Bytes(Bytes::from_static(b"aGVsbG8="));
69        let result = Base64Proc.process(bytes_data)?;
70        assert!(matches!(result, RawData::Bytes(_)));
71        assert_eq!(crate::eval::builtins::raw_to_utf8_string(&result), "hello");
72
73        let arc_data = RawData::ArcBytes(Arc::new(b"Zm9vYmFy".to_vec()));
74        let result = Base64Proc.process(arc_data)?;
75        assert!(matches!(result, RawData::ArcBytes(_)));
76        assert_eq!(crate::eval::builtins::raw_to_utf8_string(&result), "foobar");
77        Ok(())
78    }
79}