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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
use num_bigint::{BigInt, ToBigInt};

pub struct SubCommand;

impl WholeStreamCommand for SubCommand {
    fn name(&self) -> &str {
        "into binary"
    }

    fn signature(&self) -> Signature {
        Signature::build("into binary")
            .rest(
                SyntaxShape::ColumnPath,
                "column paths to convert to binary (for table input)",
            )
            .named(
                "skip",
                SyntaxShape::Int,
                "skip x number of bytes",
                Some('s'),
            )
            .named(
                "bytes",
                SyntaxShape::Int,
                "show y number of bytes",
                Some('b'),
            )
    }

    fn usage(&self) -> &str {
        "Convert value to a binary primitive"
    }

    fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
        into_binary(args)
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "convert string to a nushell binary primitive",
                example:
                    "echo 'This is a string that is exactly 52 characters long.' | into binary",
                result: Some(vec![UntaggedValue::binary(
                    "This is a string that is exactly 52 characters long."
                        .to_string()
                        .as_bytes()
                        .to_vec(),
                )
                .into()]),
            },
            Example {
                description: "convert string to a nushell binary primitive",
                example:
                    "echo 'This is a string that is exactly 52 characters long.' | into binary --skip 10",
                result: Some(vec![UntaggedValue::binary(
                    "string that is exactly 52 characters long."
                        .to_string()
                        .as_bytes()
                        .to_vec(),
                )
                .into()]),
            },
            Example {
                description: "convert string to a nushell binary primitive",
                example:
                    "echo 'This is a string that is exactly 52 characters long.' | into binary --skip 10 --bytes 10",
                result: Some(vec![UntaggedValue::binary(
                    "string tha"
                        .to_string()
                        .as_bytes()
                        .to_vec(),
                )
                .into()]),
            },
            Example {
                description: "convert a number to a nushell binary primitive",
                example: "echo 1 | into binary",
                result: Some(vec![
                    UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
                ]),
            },
            Example {
                description: "convert a boolean to a nushell binary primitive",
                example: "echo $true | into binary",
                result: Some(vec![
                    UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
                ]),
            },
            Example {
                description: "convert a filesize to a nushell binary primitive",
                example: "ls | where name == LICENSE | get size | into binary",
                result: None,
            },
            Example {
                description: "convert a filepath to a nushell binary primitive",
                example: "ls | where name == LICENSE | get name | path expand | into binary",
                result: None,
            },
            Example {
                description: "convert a decimal to a nushell binary primitive",
                example: "echo 1.234 | into binary",
                result: Some(vec![
                    UntaggedValue::binary(BigInt::from(1).to_bytes_le().1).into()
                ]),
            },
        ]
    }
}

fn into_binary(args: CommandArgs) -> Result<OutputStream, ShellError> {
    let skip: Option<Value> = args.get_flag("skip")?;
    let bytes: Option<Value> = args.get_flag("bytes")?;
    let column_paths: Vec<ColumnPath> = args.rest(0)?;

    Ok(args
        .input
        .map(move |v| {
            if column_paths.is_empty() {
                action(&v, v.tag(), &skip, &bytes)
            } else {
                let mut ret = v;
                for path in &column_paths {
                    let skip_clone = skip.clone();
                    let bytes_clone = bytes.clone();
                    ret = ret.swap_data_by_column_path(
                        path,
                        Box::new(move |old| action(old, old.tag(), &skip_clone, &bytes_clone)),
                    )?;
                }

                Ok(ret)
            }
        })
        .into_input_stream())
}

fn int_to_endian(n: i64) -> Vec<u8> {
    if cfg!(target_endian = "little") {
        // eprintln!("Little Endian");
        n.to_le_bytes().to_vec()
    } else {
        // eprintln!("Big Endian");
        n.to_be_bytes().to_vec()
    }
}

fn bigint_to_endian(n: &BigInt) -> Vec<u8> {
    if cfg!(target_endian = "little") {
        // eprintln!("Little Endian");
        n.to_bytes_le().1
    } else {
        // eprintln!("Big Endian");
        n.to_bytes_be().1
    }
}

pub fn action(
    input: &Value,
    tag: impl Into<Tag>,
    skip: &Option<Value>,
    bytes: &Option<Value>,
) -> Result<Value, ShellError> {
    let tag = tag.into();
    let skip_bytes = match skip {
        Some(s) => s.as_usize().unwrap_or(0),
        None => 0usize,
    };

    let num_bytes = match bytes {
        Some(b) => b.as_usize().unwrap_or(0),
        None => 0usize,
    };

    match &input.value {
        UntaggedValue::Primitive(prim) => Ok(UntaggedValue::binary(match prim {
            Primitive::Binary(b) => {
                if num_bytes == 0usize {
                    b.to_vec().into_iter().skip(skip_bytes).collect()
                } else {
                    b.to_vec()
                        .into_iter()
                        .skip(skip_bytes)
                        .take(num_bytes)
                        .collect()
                }
            }
            Primitive::Int(n_ref) => int_to_endian(*n_ref),
            Primitive::BigInt(n_ref) => bigint_to_endian(n_ref),
            Primitive::Decimal(dec) => match dec.to_bigint() {
                Some(n) => bigint_to_endian(&n),
                None => {
                    return Err(ShellError::unimplemented(
                        "failed to convert decimal to int",
                    ));
                }
            },
            Primitive::Filesize(a_filesize) => match a_filesize.to_bigint() {
                Some(n) => bigint_to_endian(&n),
                None => {
                    return Err(ShellError::unimplemented(
                        "failed to convert filesize to bigint",
                    ));
                }
            },
            Primitive::String(a_string) => {
                // a_string.as_bytes().to_vec()
                if num_bytes == 0usize {
                    a_string
                        .as_bytes()
                        .to_vec()
                        .into_iter()
                        .skip(skip_bytes)
                        .collect()
                } else {
                    a_string
                        .as_bytes()
                        .to_vec()
                        .into_iter()
                        .skip(skip_bytes)
                        .take(num_bytes)
                        .collect()
                }
            }
            Primitive::Boolean(a_bool) => match a_bool {
                false => int_to_endian(0),
                true => int_to_endian(1),
            },
            Primitive::Date(a_date) => a_date.format("%c").to_string().as_bytes().to_vec(),
            Primitive::FilePath(a_filepath) => a_filepath
                .as_path()
                .display()
                .to_string()
                .as_bytes()
                .to_vec(),
            _ => {
                return Err(ShellError::unimplemented(
                    "'into binary' for non-numeric primitives",
                ))
            }
        })
        .into_value(&tag)),
        UntaggedValue::Row(_) => Err(ShellError::labeled_error(
            "specify column name to use, with 'into binary COLUMN'",
            "found table",
            tag,
        )),
        _ => Err(ShellError::unimplemented(
            "'into binary' for unsupported type",
        )),
    }
}

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

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(SubCommand {})
    }
}