nu_command/network/url/
decode.rs1use nu_cmd_base::input_handler::{CellPathOnlyArgs, operate};
2use nu_engine::command_prelude::*;
3
4use percent_encoding::percent_decode_str;
5
6#[derive(Clone)]
7pub struct UrlDecode;
8
9impl Command for UrlDecode {
10 fn name(&self) -> &str {
11 "url decode"
12 }
13
14 fn signature(&self) -> Signature {
15 Signature::build("url decode")
16 .input_output_types(vec![
17 (Type::String, Type::String),
18 (
19 Type::List(Box::new(Type::String)),
20 Type::List(Box::new(Type::String)),
21 ),
22 (Type::table(), Type::table()),
23 (Type::record(), Type::record()),
24 ])
25 .allow_variants_without_examples(true)
26 .rest(
27 "rest",
28 SyntaxShape::CellPath,
29 "For a data structure input, url decode strings at the given cell paths.",
30 )
31 .category(Category::Strings)
32 }
33
34 fn description(&self) -> &str {
35 "Converts a percent-encoded web safe string to a string."
36 }
37
38 fn search_terms(&self) -> Vec<&str> {
39 vec!["string", "text", "convert"]
40 }
41
42 fn run(
43 &self,
44 engine_state: &EngineState,
45 stack: &mut Stack,
46 call: &Call,
47 input: PipelineData,
48 ) -> Result<PipelineData, ShellError> {
49 let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
50 let args = CellPathOnlyArgs::from(cell_paths);
51 operate(action, args, input, call.head, engine_state.signals())
52 }
53
54 fn examples(&self) -> Vec<Example> {
55 vec![
56 Example {
57 description: "Decode a url with escape characters",
58 example: "'https://example.com/foo%20bar' | url decode",
59 result: Some(Value::test_string("https://example.com/foo bar")),
60 },
61 Example {
62 description: "Decode multiple urls with escape characters in list",
63 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",
64 result: Some(Value::list(
65 vec![
66 Value::test_string("https://example.com/foo bar"),
67 Value::test_string("https://example.com/a>b"),
68 Value::test_string("中文字/eng/12 34"),
69 ],
70 Span::test_data(),
71 )),
72 },
73 ]
74 }
75}
76
77fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
78 let input_span = input.span();
79 match input {
80 Value::String { val, .. } => {
81 let val = percent_decode_str(val).decode_utf8();
82 match val {
83 Ok(val) => Value::string(val, head),
84 Err(e) => Value::error(
85 ShellError::GenericError {
86 error: "Failed to decode string".into(),
87 msg: e.to_string(),
88 span: Some(input_span),
89 help: None,
90 inner: vec![],
91 },
92 head,
93 ),
94 }
95 }
96 Value::Error { .. } => input.clone(),
97 _ => Value::error(
98 ShellError::OnlySupportsThisInputType {
99 exp_input_type: "string".into(),
100 wrong_type: input.get_type().to_string(),
101 dst_span: head,
102 src_span: input.span(),
103 },
104 head,
105 ),
106 }
107}
108
109#[cfg(test)]
110mod test {
111 use super::*;
112
113 #[test]
114 fn test_examples() {
115 use crate::test_examples;
116
117 test_examples(UrlDecode {})
118 }
119}