nu_command/network/url/
decode.rs1use std::borrow::Cow;
2
3use nu_cmd_base::input_handler::{CmdArgument, operate};
4use nu_engine::command_prelude::*;
5
6use percent_encoding::percent_decode_str;
7
8struct Arguments {
9 cell_paths: Option<Vec<CellPath>>,
10 binary: bool,
11}
12
13impl CmdArgument for Arguments {
14 fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
15 self.cell_paths.take()
16 }
17}
18
19#[derive(Clone)]
20pub struct UrlDecode;
21
22impl Command for UrlDecode {
23 fn name(&self) -> &str {
24 "url decode"
25 }
26
27 fn signature(&self) -> Signature {
28 Signature::build("url decode")
29 .input_output_types(vec![
30 (Type::String, Type::String),
31 (Type::String, Type::Binary),
32 (
33 Type::List(Box::new(Type::String)),
34 Type::List(Box::new(Type::String)),
35 ),
36 (
37 Type::List(Box::new(Type::String)),
38 Type::List(Box::new(Type::Binary)),
39 ),
40 (Type::table(), Type::table()),
41 (Type::record(), Type::record()),
42 ])
43 .allow_variants_without_examples(true)
44 .switch(
45 "binary",
46 "Return a binary value, to allow decoding non UTF-8 text.",
47 Some('b'),
48 )
49 .rest(
50 "rest",
51 SyntaxShape::CellPath,
52 "For a data structure input, url decode strings at the given cell paths.",
53 )
54 .category(Category::Strings)
55 }
56
57 fn description(&self) -> &str {
58 "Converts a percent-encoded web safe string to a string."
59 }
60
61 fn search_terms(&self) -> Vec<&str> {
62 vec!["string", "text", "convert"]
63 }
64
65 fn run(
66 &self,
67 engine_state: &EngineState,
68 stack: &mut Stack,
69 call: &Call,
70 input: PipelineData,
71 ) -> Result<PipelineData, ShellError> {
72 let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
73 let cell_paths = Some(cell_paths).filter(|v| !v.is_empty());
74 let binary = call.has_flag(engine_state, stack, "binary")?;
75 let args = Arguments { cell_paths, binary };
76 operate(action, args, input, call.head, engine_state.signals())
77 }
78
79 fn examples(&self) -> Vec<Example<'_>> {
80 vec![
81 Example {
82 description: "Decode a URL with escape characters.",
83 example: "'https://example.com/foo%20bar' | url decode",
84 result: Some(Value::test_string("https://example.com/foo bar")),
85 },
86 Example {
87 description: "Decode multiple URLs with escape characters in list.",
88 example: "['https://example.com/foo%20bar' 'https://example.com/a%3Eb' '%E4%B8%AD%E6%96%87%E5%AD%97/eng/12%2034'] | url decode",
89 result: Some(Value::list(
90 vec![
91 Value::test_string("https://example.com/foo bar"),
92 Value::test_string("https://example.com/a>b"),
93 Value::test_string("中文字/eng/12 34"),
94 ],
95 Span::test_data(),
96 )),
97 },
98 Example {
99 description: "Decode a percent-encoded iso-8859-1 string.",
100 example: "'%A3%20rates' | url decode --binary | decode iso-8859-1",
101 result: Some(Value::test_string("£ rates")),
102 },
103 ]
104 }
105}
106
107fn action(input: &Value, args: &Arguments, head: Span) -> Value {
108 let input_span = input.span();
109 match input {
110 Value::String { val, .. } => {
111 let percent_decode_str = percent_decode_str(val);
112 match args.binary {
113 true => {
114 let data: Cow<'_, [u8]> = percent_decode_str.into();
115 Value::binary(data, head)
116 }
117 false => {
118 let val = percent_decode_str.decode_utf8();
119 match val {
120 Ok(val) => Value::string(val, head),
121 Err(_) => Value::error(
122 ShellError::NonUtf8Custom {
123 msg: "\
124 Input is not UTF-8 encoded.\n\
125 Try using the `--binary` flag together with `decode`."
126 .into(),
127 span: input_span,
128 },
129 head,
130 ),
131 }
132 }
133 }
134 }
135 Value::Error { .. } => input.clone(),
136 _ => Value::error(
137 ShellError::OnlySupportsThisInputType {
138 exp_input_type: "string".into(),
139 wrong_type: input.get_type().to_string(),
140 dst_span: head,
141 src_span: input.span(),
142 },
143 head,
144 ),
145 }
146}
147
148#[cfg(test)]
149mod test {
150 use super::*;
151
152 #[test]
153 fn test_examples() -> nu_test_support::Result {
154 nu_test_support::test().examples(UrlDecode)
155 }
156}