vrl/stdlib/
decode_zstd.rs

1use crate::compiler::prelude::*;
2use nom::AsBytes;
3
4fn decode_zstd(value: Value) -> Resolved {
5    let value = value.try_bytes()?;
6    let result = zstd::decode_all(value.as_bytes());
7
8    match result {
9        Ok(decoded_bytes) => Ok(Value::Bytes(decoded_bytes.into())),
10        Err(_) => Err("unable to decode value with Zstd decoder".into()),
11    }
12}
13
14#[derive(Clone, Copy, Debug)]
15pub struct DecodeZstd;
16
17impl Function for DecodeZstd {
18    fn identifier(&self) -> &'static str {
19        "decode_zstd"
20    }
21
22    fn usage(&self) -> &'static str {
23        "Decodes the `value` (a [Zstandard](https://facebook.github.io/zstd) string) into its original string."
24    }
25
26    fn category(&self) -> &'static str {
27        Category::Codec.as_ref()
28    }
29
30    fn internal_failure_reasons(&self) -> &'static [&'static str] {
31        &["`value` isn't a valid encoded Zstd string."]
32    }
33
34    fn return_kind(&self) -> u16 {
35        kind::BYTES
36    }
37
38    fn examples(&self) -> &'static [Example] {
39        &[example! {
40            title: "Decode Zstd data",
41            source: r#"decode_zstd!(decode_base64!("KLUv/QBY/QEAYsQOFKClbQBedqXsb96EWDax/f/F/z+gNU4ZTInaUeAj82KqPFjUzKqhcfDqAIsLvAsnY1bI/N2mHzDixRQA"))"#,
42            result: Ok("you_have_successfully_decoded_me.congratulations.you_are_breathtaking."),
43        }]
44    }
45
46    fn compile(
47        &self,
48        _state: &state::TypeState,
49        _ctx: &mut FunctionCompileContext,
50        arguments: ArgumentList,
51    ) -> Compiled {
52        let value = arguments.required("value");
53
54        Ok(DecodeZstdFn { value }.as_expr())
55    }
56
57    fn parameters(&self) -> &'static [Parameter] {
58        const PARAMETERS: &[Parameter] = &[Parameter::required(
59            "value",
60            kind::BYTES,
61            "The [Zstandard](https://facebook.github.io/zstd) data to decode.",
62        )];
63        PARAMETERS
64    }
65}
66
67#[derive(Clone, Debug)]
68struct DecodeZstdFn {
69    value: Box<dyn Expression>,
70}
71
72impl FunctionExpression for DecodeZstdFn {
73    fn resolve(&self, ctx: &mut Context) -> Resolved {
74        let value = self.value.resolve(ctx)?;
75
76        decode_zstd(value)
77    }
78
79    fn type_def(&self, _: &state::TypeState) -> TypeDef {
80        // Always fallible due to the possibility of decoding errors that VRL can't detect
81        TypeDef::bytes().fallible()
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::value;
89    use nom::AsBytes;
90
91    fn get_encoded_bytes(text: &str) -> Vec<u8> {
92        zstd::encode_all(text.as_bytes(), 0).expect("Cannot encode bytes with Zstd encoder")
93    }
94
95    test_function![
96        decode_zstd => DecodeZstd;
97
98        right_zstd {
99            args: func_args![value: value!(get_encoded_bytes("sample").as_bytes())],
100            want: Ok(value!(b"sample")),
101            tdef: TypeDef::bytes().fallible(),
102        }
103
104        wrong_zstd {
105            args: func_args![value: value!("some_bytes")],
106            want: Err("unable to decode value with Zstd decoder"),
107            tdef: TypeDef::bytes().fallible(),
108        }
109    ];
110}