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
use data_encoding::Encoding;

use nu_engine::command_prelude::*;

const EXTRA_USAGE: &str = r"The default alphabet is taken from RFC 4648, section 6.

Note this command will collect stream input.";

#[derive(Clone)]
pub struct DecodeBase32;

impl Command for DecodeBase32 {
    fn name(&self) -> &str {
        "decode base32"
    }

    fn signature(&self) -> Signature {
        Signature::build("decode base32")
            .input_output_types(vec![(Type::String, Type::Binary)])
            .allow_variants_without_examples(true)
            .switch("nopad", "Do not pad the output.", None)
            .category(Category::Formats)
    }

    fn description(&self) -> &str {
        "Decode a Base32 value."
    }

    fn extra_description(&self) -> &str {
        EXTRA_USAGE
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Decode arbitrary binary data",
                example: r#""AEBAGBAF" | decode base32"#,
                result: Some(Value::test_binary(vec![1, 2, 3, 4, 5])),
            },
            Example {
                description: "Decode an encoded string",
                example: r#""NBUQ====" | decode base32 | decode"#,
                result: None,
            },
            Example {
                description: "Parse a string without padding",
                example: r#""NBUQ" | decode base32 --nopad"#,
                result: Some(Value::test_binary(vec![0x68, 0x69])),
            },
        ]
    }

    fn is_const(&self) -> bool {
        true
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let encoding = if call.has_flag(engine_state, stack, "nopad")? {
            data_encoding::BASE32_NOPAD
        } else {
            data_encoding::BASE32
        };
        super::decode(encoding, call.span(), input)
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let encoding = if call.has_flag_const(working_set, "nopad")? {
            data_encoding::BASE32_NOPAD
        } else {
            data_encoding::BASE32
        };
        super::decode(encoding, call.span(), input)
    }
}

#[derive(Clone)]
pub struct EncodeBase32;

impl Command for EncodeBase32 {
    fn name(&self) -> &str {
        "encode base32"
    }

    fn signature(&self) -> Signature {
        Signature::build("encode base32")
            .input_output_types(vec![
                (Type::String, Type::String),
                (Type::Binary, Type::String),
            ])
            .switch("nopad", "Don't accept padding.", None)
            .category(Category::Formats)
    }

    fn description(&self) -> &str {
        "Encode a string or binary value using Base32."
    }

    fn extra_description(&self) -> &str {
        EXTRA_USAGE
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Encode a binary value",
                example: r#"0x[01 02 10] | encode base32"#,
                result: Some(Value::test_string("AEBBA===")),
            },
            Example {
                description: "Encode a string",
                example: r#""hello there" | encode base32"#,
                result: Some(Value::test_string("NBSWY3DPEB2GQZLSMU======")),
            },
            Example {
                description: "Don't apply padding to the output",
                example: r#""hi" | encode base32 --nopad"#,
                result: Some(Value::test_string("NBUQ")),
            },
        ]
    }

    fn is_const(&self) -> bool {
        true
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let encoding = if call.has_flag(engine_state, stack, "nopad")? {
            data_encoding::BASE32_NOPAD
        } else {
            data_encoding::BASE32
        };
        super::encode(encoding, call.span(), input)
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let encoding = if call.has_flag_const(working_set, "nopad")? {
            data_encoding::BASE32_NOPAD
        } else {
            data_encoding::BASE32
        };
        super::encode(encoding, call.span(), input)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_examples_decode() {
        crate::test_examples(DecodeBase32)
    }

    #[test]
    fn test_examples_encode() {
        crate::test_examples(EncodeBase32)
    }
}