nu_command/strings/base/
hex.rs1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct DecodeHex;
5
6impl Command for DecodeHex {
7 fn name(&self) -> &str {
8 "decode hex"
9 }
10
11 fn signature(&self) -> Signature {
12 Signature::build("decode hex")
13 .input_output_types(vec![(Type::String, Type::Binary)])
14 .category(Category::Formats)
15 }
16
17 fn description(&self) -> &str {
18 "Hex decode a value."
19 }
20
21 fn examples(&self) -> Vec<Example> {
22 vec![
23 Example {
24 description: "Decode arbitrary binary data",
25 example: r#""09FD" | decode hex"#,
26 result: Some(Value::test_binary(vec![0x09, 0xFD])),
27 },
28 Example {
29 description: "Lowercase Hex is also accepted",
30 example: r#""09fd" | decode hex"#,
31 result: Some(Value::test_binary(vec![0x09, 0xFD])),
32 },
33 ]
34 }
35
36 fn is_const(&self) -> bool {
37 true
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 super::decode(data_encoding::HEXLOWER_PERMISSIVE, call.head, input)
48 }
49
50 fn run_const(
51 &self,
52 working_set: &StateWorkingSet,
53 call: &Call,
54 input: PipelineData,
55 ) -> Result<PipelineData, ShellError> {
56 super::decode(data_encoding::HEXLOWER_PERMISSIVE, call.span(), input)
57 }
58}
59
60#[derive(Clone)]
61pub struct EncodeHex;
62
63impl Command for EncodeHex {
64 fn name(&self) -> &str {
65 "encode hex"
66 }
67
68 fn signature(&self) -> Signature {
69 Signature::build("encode hex")
70 .input_output_types(vec![
71 (Type::String, Type::String),
72 (Type::Binary, Type::String),
73 ])
74 .switch("lower", "Encode to lowercase hex.", None)
75 .category(Category::Formats)
76 }
77
78 fn description(&self) -> &str {
79 "Hex encode a binary value or a string."
80 }
81
82 fn examples(&self) -> Vec<Example> {
83 vec![
84 Example {
85 description: "Encode a binary value",
86 example: r#"0x[C3 06] | encode hex"#,
87 result: Some(Value::test_string("C306")),
88 },
89 Example {
90 description: "Encode a string",
91 example: r#""hello" | encode hex"#,
92 result: Some(Value::test_string("68656C6C6F")),
93 },
94 Example {
95 description: "Output a Lowercase version of the encoding",
96 example: r#"0x[AD EF] | encode hex --lower"#,
97 result: Some(Value::test_string("adef")),
98 },
99 ]
100 }
101
102 fn is_const(&self) -> bool {
103 true
104 }
105
106 fn run(
107 &self,
108 engine_state: &EngineState,
109 stack: &mut Stack,
110 call: &Call,
111 input: PipelineData,
112 ) -> Result<PipelineData, ShellError> {
113 let encoding = if call.has_flag(engine_state, stack, "lower")? {
114 data_encoding::HEXLOWER
115 } else {
116 data_encoding::HEXUPPER
117 };
118
119 super::encode(encoding, call.head, input)
120 }
121
122 fn run_const(
123 &self,
124 working_set: &StateWorkingSet,
125 call: &Call,
126 input: PipelineData,
127 ) -> Result<PipelineData, ShellError> {
128 let encoding = if call.has_flag_const(working_set, "lower")? {
129 data_encoding::HEXLOWER
130 } else {
131 data_encoding::HEXUPPER
132 };
133
134 super::encode(encoding, call.head, input)
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 #[test]
143 fn test_examples_decode() {
144 crate::test_examples(DecodeHex)
145 }
146
147 #[test]
148 fn test_examples_encode() {
149 crate::test_examples(EncodeHex)
150 }
151}