2021-11-12 21:46:39 +01:00
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2021-11-17 05:22:37 +01:00
|
|
|
use nu_protocol::{Category, Config, Example, PipelineData, ShellError, Signature, Span, Value};
|
2021-11-12 21:46:39 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FromUrl;
|
|
|
|
|
|
|
|
impl Command for FromUrl {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from url"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("from url").category(Category::Formats)
|
2021-11-12 21:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse url-encoded string as a table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
_engine_state: &EngineState,
|
2021-11-14 20:25:57 +01:00
|
|
|
stack: &mut Stack,
|
2021-11-12 21:46:39 +01:00
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, ShellError> {
|
|
|
|
let head = call.head;
|
2021-12-13 04:16:51 +01:00
|
|
|
let config = stack.get_config().unwrap_or_default();
|
2021-11-14 20:25:57 +01:00
|
|
|
from_url(input, head, &config)
|
2021-11-12 21:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
example: "'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url",
|
|
|
|
description: "Convert url encoded string into a table",
|
|
|
|
result: Some(Value::Record {
|
|
|
|
cols: vec![
|
|
|
|
"bread".to_string(),
|
|
|
|
"cheese".to_string(),
|
|
|
|
"meat".to_string(),
|
|
|
|
"fat".to_string(),
|
|
|
|
],
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("baguette"),
|
|
|
|
Value::test_string("comté"),
|
|
|
|
Value::test_string("ham"),
|
|
|
|
Value::test_string("butter"),
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-12 21:46:39 +01:00
|
|
|
}),
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 20:25:57 +01:00
|
|
|
fn from_url(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
|
|
|
|
let concat_string = input.collect_string("", config);
|
2021-11-12 21:46:39 +01:00
|
|
|
|
|
|
|
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(&concat_string);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => {
|
|
|
|
let mut cols = vec![];
|
|
|
|
let mut vals = vec![];
|
|
|
|
for (k, v) in result {
|
|
|
|
cols.push(k);
|
|
|
|
vals.push(Value::String { val: v, span: head })
|
|
|
|
}
|
|
|
|
|
2021-12-02 06:59:10 +01:00
|
|
|
Ok(PipelineData::Value(
|
|
|
|
Value::Record {
|
|
|
|
cols,
|
|
|
|
vals,
|
|
|
|
span: head,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
))
|
2021-11-12 21:46:39 +01:00
|
|
|
}
|
|
|
|
_ => Err(ShellError::UnsupportedInput(
|
|
|
|
"String not compatible with url-encoding".to_string(),
|
|
|
|
head,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(FromUrl {})
|
|
|
|
}
|
|
|
|
}
|