1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; pub struct ToUrl; impl WholeStreamCommand for ToUrl { fn name(&self) -> &str { "to url" } fn signature(&self) -> Signature { Signature::build("to url") } fn usage(&self) -> &str { "Convert table into url-encoded text" } fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { to_url(args) } } fn to_url(args: CommandArgs) -> Result<OutputStream, ShellError> { let tag = args.name_tag(); let input = args.input; Ok(input .map(move |value| match value { Value { value: UntaggedValue::Row(row), .. } => { let mut row_vec = vec![]; for (k, v) in row.entries { match v.as_string() { Ok(s) => { row_vec.push((k.clone(), s.to_string())); } _ => { return Value::error(ShellError::labeled_error_with_secondary( "Expected table with string values", "requires table with strings", &tag, "value originates from here", v.tag, )); } } } match serde_urlencoded::to_string(row_vec) { Ok(s) => UntaggedValue::string(s).into_value(&tag), _ => Value::error(ShellError::labeled_error( "Failed to convert to url-encoded", "cannot url-encode", &tag, )), } } Value { tag: value_tag, .. } => Value::error(ShellError::labeled_error_with_secondary( "Expected a table from pipeline", "requires table input", &tag, "value originates from here", value_tag.span, )), }) .into_output_stream()) } #[cfg(test)] mod tests { use super::ShellError; use super::ToUrl; #[test] fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; test_examples(ToUrl {}) } }