nu_cmd_extra/extra/bits/
and.rs1use super::binary_op;
2use nu_engine::command_prelude::*;
3use nu_heavy_utils::Endian;
4
5#[derive(Clone)]
6pub struct BitsAnd;
7
8impl Command for BitsAnd {
9 fn name(&self) -> &str {
10 "bits and"
11 }
12
13 fn signature(&self) -> Signature {
14 Signature::build("bits and")
15 .input_output_types(vec![
16 (Type::Int, Type::Int),
17 (Type::Binary, Type::Binary),
18 (
19 Type::List(Box::new(Type::Int)),
20 Type::List(Box::new(Type::Int)),
21 ),
22 (
23 Type::List(Box::new(Type::Binary)),
24 Type::List(Box::new(Type::Binary)),
25 ),
26 ])
27 .required(
28 "target",
29 SyntaxShape::OneOf(vec![SyntaxShape::Binary, SyntaxShape::Int]),
30 "Right-hand side of the operation.",
31 )
32 .param(Endian::flag())
33 .category(Category::Bits)
34 }
35
36 fn description(&self) -> &str {
37 "Performs bitwise and for ints or binary values."
38 }
39
40 fn search_terms(&self) -> Vec<&str> {
41 vec!["logic and"]
42 }
43
44 fn run(
45 &self,
46 engine_state: &EngineState,
47 stack: &mut Stack,
48 call: &Call,
49 input: PipelineData,
50 ) -> Result<PipelineData, ShellError> {
51 let head = call.head;
52 let target: Value = call.req(engine_state, stack, 0)?;
53 let endian = call
54 .get_flag::<Endian>(engine_state, stack, "endian")?
55 .unwrap_or_default();
56
57 if let PipelineData::Empty = input {
59 return Err(ShellError::PipelineEmpty { dst_span: head });
60 }
61
62 input.map(
63 move |value| binary_op(&value, &target, endian, |(l, r)| l & r, head),
64 engine_state.signals(),
65 )
66 }
67
68 fn examples(&self) -> Vec<Example<'_>> {
69 vec![
70 Example {
71 description: "Apply bitwise and to two numbers",
72 example: "2 | bits and 2",
73 result: Some(Value::test_int(2)),
74 },
75 Example {
76 description: "Apply bitwise and to two binary values",
77 example: "0x[ab cd] | bits and 0x[99 99]",
78 result: Some(Value::test_binary([0x89, 0x89])),
79 },
80 Example {
81 description: "Apply bitwise and to a list of numbers",
82 example: "[4 3 2] | bits and 2",
83 result: Some(Value::test_list(vec![
84 Value::test_int(0),
85 Value::test_int(2),
86 Value::test_int(2),
87 ])),
88 },
89 Example {
90 description: "Apply bitwise and to a list of binary data",
91 example: "[0x[7f ff] 0x[ff f0]] | bits and 0x[99 99]",
92 result: Some(Value::test_list(vec![
93 Value::test_binary([0x19, 0x99]),
94 Value::test_binary([0x99, 0x90]),
95 ])),
96 },
97 Example {
98 description: "Apply bitwise and to binary data of varying lengths with specified endianness",
99 example: "0x[c0 ff ee] | bits and 0x[ff] --endian big",
100 result: Some(Value::test_binary(vec![0x00, 0x00, 0xee])),
101 },
102 Example {
103 description: "Apply bitwise and to input binary data smaller than the operand",
104 example: "0x[ff] | bits and 0x[12 34 56] --endian little",
105 result: Some(Value::test_binary(vec![0x12, 0x00, 0x00])),
106 },
107 ]
108 }
109}
110
111#[cfg(test)]
112mod test {
113 use super::*;
114
115 #[test]
116 fn test_examples() -> nu_test_support::Result {
117 nu_test_support::test().examples(BitsAnd)
118 }
119}