Add a config variable with engine support (#332)

* Add a config variable with engine support

* Add a config variable with engine support

* Oops, cleanup
This commit is contained in:
JT
2021-11-15 08:25:57 +13:00
committed by GitHub
parent e76451866d
commit 0f107b2830
30 changed files with 333 additions and 96 deletions

View File

@ -78,6 +78,7 @@ fn from_csv(
let noheaders = call.has_flag("noheaders");
let separator: Option<Value> = call.get_flag(engine_state, stack, "separator")?;
let config = stack.get_config()?;
let sep = match separator {
Some(Value::String { val: s, span }) => {
@ -97,7 +98,7 @@ fn from_csv(
_ => ',',
};
from_delimited_data(noheaders, sep, input, name)
from_delimited_data(noheaders, sep, input, name, &config)
}
#[cfg(test)]

View File

@ -1,5 +1,5 @@
use csv::ReaderBuilder;
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Value};
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Value};
fn from_delimited_string_to_value(
s: String,
@ -50,8 +50,9 @@ pub fn from_delimited_data(
sep: char,
input: PipelineData,
name: Span,
config: &Config,
) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
let concat_string = input.collect_string("", config);
Ok(
from_delimited_string_to_value(concat_string, noheaders, sep, name)

View File

@ -4,6 +4,7 @@ use indexmap::map::IndexMap;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Config;
use nu_protocol::{
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
@ -41,7 +42,8 @@ impl Command for FromEml {
let head = call.head;
let preview_body: Option<Spanned<i64>> =
call.get_flag(engine_state, stack, "preview-body")?;
from_eml(input, preview_body, head)
let config = stack.get_config()?;
from_eml(input, preview_body, head, &config)
}
fn examples(&self) -> Vec<Example> {
@ -176,8 +178,9 @@ fn from_eml(
input: PipelineData,
preview_body: Option<Spanned<i64>>,
head: Span,
config: &Config,
) -> Result<PipelineData, ShellError> {
let value = input.collect_string("");
let value = input.collect_string("", config);
let body_preview = preview_body
.map(|b| b.item as usize)

View File

@ -72,12 +72,13 @@ impl Command for FromJson {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let span = call.head;
let mut string_input = input.collect_string("");
let config = stack.get_config()?;
let mut string_input = input.collect_string("", &config);
string_input.push('\n');
// TODO: turn this into a structured underline of the nu_json error

View File

@ -2,7 +2,7 @@ use super::delimited::from_delimited_data;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Signature};
use nu_protocol::{Config, PipelineData, ShellError, Signature};
#[derive(Clone)]
pub struct FromTsv;
@ -27,20 +27,21 @@ impl Command for FromTsv {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
from_tsv(call, input)
let config = stack.get_config()?;
from_tsv(call, input, &config)
}
}
fn from_tsv(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
fn from_tsv(call: &Call, input: PipelineData, config: &Config) -> Result<PipelineData, ShellError> {
let name = call.head;
let noheaders = call.has_flag("noheaders");
from_delimited_data(noheaders, '\t', input, name)
from_delimited_data(noheaders, '\t', input, name, config)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
use nu_protocol::{Config, Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct FromUrl;
@ -21,12 +21,13 @@ impl Command for FromUrl {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_url(input, head)
let config = stack.get_config()?;
from_url(input, head, &config)
}
fn examples(&self) -> Vec<Example> {
@ -52,8 +53,8 @@ impl Command for FromUrl {
}
}
fn from_url(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
fn from_url(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("", config);
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(&concat_string);

View File

@ -2,7 +2,7 @@ use itertools::Itertools;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Value,
Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Value,
};
use serde::de::Deserialize;
use std::collections::HashMap;
@ -65,12 +65,13 @@ impl Command for FromYaml {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_yaml(input, head)
let config = stack.get_config()?;
from_yaml(input, head, &config)
}
}
@ -93,12 +94,13 @@ impl Command for FromYml {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_yaml(input, head)
let config = stack.get_config()?;
from_yaml(input, head, &config)
}
}
@ -202,8 +204,8 @@ pub fn from_yaml_string_to_value(s: String, span: Span) -> Result<Value, ShellEr
}
}
fn from_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
fn from_yaml(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("", config);
match from_yaml_string_to_value(concat_string, head) {
Ok(x) => Ok(x.into_pipeline_data()),
@ -248,6 +250,7 @@ mod test {
}),
},
];
let config = Config::default();
for tc in tt {
let actual = from_yaml_string_to_value(tc.input.to_owned(), Span::unknown());
if actual.is_err() {
@ -259,8 +262,8 @@ mod test {
);
} else {
assert_eq!(
actual.unwrap().into_string(""),
tc.expected.unwrap().into_string("")
actual.unwrap().into_string("", &config),
tc.expected.unwrap().into_string("", &config)
);
}
}