1use nu_cmd_base::input_handler::{CmdArgument, operate};
2use nu_engine::command_prelude::*;
3
4struct Arguments {
5 cell_paths: Option<Vec<CellPath>>,
6 compact: bool,
7 little_endian: bool,
8}
9
10impl CmdArgument for Arguments {
11 fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
12 self.cell_paths.take()
13 }
14}
15
16#[derive(Clone)]
17pub struct IntoBinary;
18
19impl Command for IntoBinary {
20 fn name(&self) -> &str {
21 "into binary"
22 }
23
24 fn signature(&self) -> Signature {
25 Signature::build("into binary")
26 .input_output_types(vec![
27 (Type::Binary, Type::Binary),
28 (Type::Int, Type::Binary),
29 (Type::Number, Type::Binary),
30 (Type::String, Type::Binary),
31 (Type::Bool, Type::Binary),
32 (Type::Filesize, Type::Binary),
33 (Type::Date, Type::Binary),
34 (Type::table(), Type::table()),
35 (Type::record(), Type::record()),
36 ])
37 .allow_variants_without_examples(true) .switch("compact", "Output without padding zeros.", Some('c'))
39 .param(
40 Flag::new("endian")
41 .short('e')
42 .arg(SyntaxShape::String)
43 .desc("Byte encode endian. Does not affect string, date or binary. In containers, only individual elements are affected. Available options: native(default), little, big.")
44 .completion(Completion::new_list(&["native", "little", "big"])),
45 )
46 .rest(
47 "rest",
48 SyntaxShape::CellPath,
49 "For a data structure input, convert data at the given cell paths.",
50 )
51 .category(Category::Conversions)
52 }
53
54 fn description(&self) -> &str {
55 "Convert value to a binary primitive."
56 }
57
58 fn search_terms(&self) -> Vec<&str> {
59 vec!["convert", "bytes"]
60 }
61
62 fn run(
63 &self,
64 engine_state: &EngineState,
65 stack: &mut Stack,
66 call: &Call,
67 input: PipelineData,
68 ) -> Result<PipelineData, ShellError> {
69 into_binary(engine_state, stack, call, input)
70 }
71
72 fn examples(&self) -> Vec<Example<'_>> {
73 vec![
74 Example {
75 description: "convert string to a nushell binary primitive.",
76 example: "'This is a string that is exactly 52 characters long.' | into binary",
77 result: Some(Value::binary(
78 "This is a string that is exactly 52 characters long."
79 .to_string()
80 .as_bytes()
81 .to_vec(),
82 Span::test_data(),
83 )),
84 },
85 Example {
86 description: "convert a number to a nushell binary primitive.",
87 example: "1 | into binary",
88 result: Some(Value::binary(
89 i64::from(1).to_ne_bytes().to_vec(),
90 Span::test_data(),
91 )),
92 },
93 Example {
94 description: "convert a number to a nushell binary primitive (big endian).",
95 example: "258 | into binary --endian big",
96 result: Some(Value::binary(
97 i64::from(258).to_be_bytes().to_vec(),
98 Span::test_data(),
99 )),
100 },
101 Example {
102 description: "convert a number to a nushell binary primitive (little endian).",
103 example: "258 | into binary --endian little",
104 result: Some(Value::binary(
105 i64::from(258).to_le_bytes().to_vec(),
106 Span::test_data(),
107 )),
108 },
109 Example {
110 description: "convert a boolean to a nushell binary primitive.",
111 example: "true | into binary",
112 result: Some(Value::binary(
113 i64::from(1).to_ne_bytes().to_vec(),
114 Span::test_data(),
115 )),
116 },
117 Example {
118 description: "convert a filesize to a nushell binary primitive.",
119 example: "ls | where name == LICENSE | get size | into binary",
120 result: None,
121 },
122 Example {
123 description: "convert a filepath to a nushell binary primitive.",
124 example: "ls | where name == LICENSE | get name | path expand | into binary",
125 result: None,
126 },
127 Example {
128 description: "convert a float to a nushell binary primitive.",
129 example: "1.234 | into binary",
130 result: Some(Value::binary(
131 1.234f64.to_ne_bytes().to_vec(),
132 Span::test_data(),
133 )),
134 },
135 Example {
136 description: "convert an int to a nushell binary primitive with compact enabled.",
137 example: "10 | into binary --compact",
138 result: Some(Value::binary(vec![10], Span::test_data())),
139 },
140 ]
141 }
142}
143
144fn into_binary(
145 engine_state: &EngineState,
146 stack: &mut Stack,
147 call: &Call,
148 input: PipelineData,
149) -> Result<PipelineData, ShellError> {
150 let head = call.head;
151 let cell_paths = call.rest(engine_state, stack, 0)?;
152 let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
153
154 if let PipelineData::ByteStream(stream, metadata) = input {
155 Ok(PipelineData::byte_stream(
157 stream.with_type(ByteStreamType::Binary),
158 metadata,
159 ))
160 } else {
161 let endian = call.get_flag::<Spanned<String>>(engine_state, stack, "endian")?;
162
163 let little_endian = if let Some(endian) = endian {
164 match endian.item.as_str() {
165 "native" => cfg!(target_endian = "little"),
166 "little" => true,
167 "big" => false,
168 _ => {
169 return Err(ShellError::TypeMismatch {
170 err_message: "Endian must be one of native, little, big".to_string(),
171 span: endian.span,
172 });
173 }
174 }
175 } else {
176 cfg!(target_endian = "little")
177 };
178
179 let args = Arguments {
180 cell_paths,
181 compact: call.has_flag(engine_state, stack, "compact")?,
182 little_endian,
183 };
184 operate(action, args, input, head, engine_state.signals())
185 }
186}
187
188fn action(input: &Value, args: &Arguments, span: Span) -> Value {
189 let value = match input {
190 Value::Binary { .. } => input.clone(),
191 Value::Int { val, .. } => Value::binary(
192 if args.little_endian {
193 val.to_le_bytes()
194 } else {
195 val.to_be_bytes()
196 }
197 .to_vec(),
198 span,
199 ),
200 Value::Float { val, .. } => Value::binary(
201 if args.little_endian {
202 val.to_le_bytes()
203 } else {
204 val.to_be_bytes()
205 }
206 .to_vec(),
207 span,
208 ),
209 Value::Filesize { val, .. } => Value::binary(
210 if args.little_endian {
211 val.get().to_le_bytes()
212 } else {
213 val.get().to_be_bytes()
214 }
215 .to_vec(),
216 span,
217 ),
218 Value::String { val, .. } => Value::binary(val.as_bytes().to_vec(), span),
219 Value::Bool { val, .. } => Value::binary(
220 {
221 let as_int = i64::from(*val);
222 if args.little_endian {
223 as_int.to_le_bytes()
224 } else {
225 as_int.to_be_bytes()
226 }
227 .to_vec()
228 },
229 span,
230 ),
231 Value::Duration { val, .. } => Value::binary(
232 if args.little_endian {
233 val.to_le_bytes()
234 } else {
235 val.to_be_bytes()
236 }
237 .to_vec(),
238 span,
239 ),
240 Value::Date { val, .. } => {
241 Value::binary(val.format("%c").to_string().as_bytes().to_vec(), span)
242 }
243 Value::Error { .. } => input.clone(),
245 other => Value::error(
246 ShellError::OnlySupportsThisInputType {
247 exp_input_type: "int, float, filesize, string, date, duration, binary, or bool"
248 .into(),
249 wrong_type: other.get_type().to_string(),
250 dst_span: span,
251 src_span: other.span(),
252 },
253 span,
254 ),
255 };
256
257 if args.compact {
258 let val_span = value.span();
259 if let Value::Binary { val, .. } = value {
260 let val = if args.little_endian {
261 match val.iter().rposition(|&x| x != 0) {
262 Some(idx) => &val[..idx + 1],
263
264 None => &[0],
266 }
267 } else {
268 match val.iter().position(|&x| x != 0) {
269 Some(idx) => &val[idx..],
270 None => &[0],
271 }
272 };
273
274 Value::binary(val.to_vec(), val_span)
275 } else {
276 value
277 }
278 } else {
279 value
280 }
281}
282
283#[cfg(test)]
284mod test {
285 use rstest::rstest;
286
287 use super::*;
288
289 #[test]
290 fn test_examples() -> nu_test_support::Result {
291 nu_test_support::test().examples(IntoBinary)
292 }
293
294 #[rstest]
295 #[case(vec![10], vec![10], vec![10])]
296 #[case(vec![10, 0, 0], vec![10], vec![10, 0, 0])]
297 #[case(vec![0, 0, 10], vec![0, 0, 10], vec![10])]
298 #[case(vec![0, 10, 0, 0], vec![0, 10], vec![10, 0, 0])]
299 fn test_compact(#[case] input: Vec<u8>, #[case] little: Vec<u8>, #[case] big: Vec<u8>) {
300 let s = Value::test_binary(input);
301 let actual = action(
302 &s,
303 &Arguments {
304 cell_paths: None,
305 compact: true,
306 little_endian: cfg!(target_endian = "little"),
307 },
308 Span::test_data(),
309 );
310 if cfg!(target_endian = "little") {
311 assert_eq!(actual, Value::test_binary(little));
312 } else {
313 assert_eq!(actual, Value::test_binary(big));
314 }
315 }
316}