mirror of
https://github.com/nushell/nushell.git
synced 2024-12-23 07:30:13 +01:00
Reduced LOC by replacing several instances of Value::Int {}
, Value::Float{}
, Value::Bool {}
, and Value::String {}
with Value::int()
, Value::float()
, Value::boolean()
and Value::string()
(#7412)
# Description While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` constructors, which seem designed to make it easier to construct various Values, but which aren't used often at all in the codebase. So, using a few find-replaces regexes, I increased their usage. This reduces overall LOC because structures like this: ``` Value::Int { val: a, span: head } ``` are changed into ``` Value::int(a, head) ``` and are respected as such by the project's formatter. There are little readability concerns because the second argument to all of these is `span`, and it's almost always extremely obvious which is the span at every callsite. # User-Facing Changes None. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
b56ad92e25
commit
220b105efb
@ -77,10 +77,7 @@ impl NuCompleter {
|
||||
Value::List {
|
||||
vals: spans
|
||||
.iter()
|
||||
.map(|it| Value::String {
|
||||
val: it.to_string(),
|
||||
span: Span::unknown(),
|
||||
})
|
||||
.map(|it| Value::string(it, Span::unknown()))
|
||||
.collect(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
|
@ -42,20 +42,14 @@ impl Completer for NuMenuCompleter {
|
||||
|
||||
if let Some(buffer) = block.signature.get_positional(0) {
|
||||
if let Some(buffer_id) = &buffer.var_id {
|
||||
let line_buffer = Value::String {
|
||||
val: parsed.remainder.to_string(),
|
||||
span: self.span,
|
||||
};
|
||||
let line_buffer = Value::string(parsed.remainder, self.span);
|
||||
self.stack.add_var(*buffer_id, line_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(position) = block.signature.get_positional(1) {
|
||||
if let Some(position_id) = &position.var_id {
|
||||
let line_buffer = Value::Int {
|
||||
val: pos as i64,
|
||||
span: self.span,
|
||||
};
|
||||
let line_buffer = Value::int(pos as i64, self.span);
|
||||
self.stack.add_var(*position_id, line_buffer);
|
||||
}
|
||||
}
|
||||
|
@ -993,10 +993,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_send_event() {
|
||||
let cols = vec!["send".to_string()];
|
||||
let vals = vec![Value::String {
|
||||
val: "Enter".to_string(),
|
||||
span: Span::test_data(),
|
||||
}];
|
||||
let vals = vec![Value::string("Enter", Span::test_data())];
|
||||
|
||||
let span = Span::test_data();
|
||||
let b = EventType::try_from_columns(&cols, &vals, &span).unwrap();
|
||||
@ -1016,10 +1013,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_edit_event() {
|
||||
let cols = vec!["edit".to_string()];
|
||||
let vals = vec![Value::String {
|
||||
val: "Clear".to_string(),
|
||||
span: Span::test_data(),
|
||||
}];
|
||||
let vals = vec![Value::string("Clear", Span::test_data())];
|
||||
|
||||
let span = Span::test_data();
|
||||
let b = EventType::try_from_columns(&cols, &vals, &span).unwrap();
|
||||
@ -1043,14 +1037,8 @@ mod test {
|
||||
fn test_send_menu() {
|
||||
let cols = vec!["send".to_string(), "name".to_string()];
|
||||
let vals = vec![
|
||||
Value::String {
|
||||
val: "Menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "history_menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("Menu", Span::test_data()),
|
||||
Value::string("history_menu", Span::test_data()),
|
||||
];
|
||||
|
||||
let span = Span::test_data();
|
||||
@ -1076,14 +1064,8 @@ mod test {
|
||||
// Menu event
|
||||
let cols = vec!["send".to_string(), "name".to_string()];
|
||||
let vals = vec![
|
||||
Value::String {
|
||||
val: "Menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "history_menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("Menu", Span::test_data()),
|
||||
Value::string("history_menu", Span::test_data()),
|
||||
];
|
||||
|
||||
let menu_event = Value::Record {
|
||||
@ -1094,10 +1076,7 @@ mod test {
|
||||
|
||||
// Enter event
|
||||
let cols = vec!["send".to_string()];
|
||||
let vals = vec![Value::String {
|
||||
val: "Enter".to_string(),
|
||||
span: Span::test_data(),
|
||||
}];
|
||||
let vals = vec![Value::string("Enter", Span::test_data())];
|
||||
|
||||
let enter_event = Value::Record {
|
||||
cols,
|
||||
@ -1138,14 +1117,8 @@ mod test {
|
||||
// Menu event
|
||||
let cols = vec!["send".to_string(), "name".to_string()];
|
||||
let vals = vec![
|
||||
Value::String {
|
||||
val: "Menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "history_menu".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("Menu", Span::test_data()),
|
||||
Value::string("history_menu", Span::test_data()),
|
||||
];
|
||||
|
||||
let menu_event = Value::Record {
|
||||
@ -1156,10 +1129,7 @@ mod test {
|
||||
|
||||
// Enter event
|
||||
let cols = vec!["send".to_string()];
|
||||
let vals = vec![Value::String {
|
||||
val: "Enter".to_string(),
|
||||
span: Span::test_data(),
|
||||
}];
|
||||
let vals = vec![Value::string("Enter", Span::test_data())];
|
||||
|
||||
let enter_event = Value::Record {
|
||||
cols,
|
||||
@ -1187,10 +1157,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_error() {
|
||||
let cols = vec!["not_exist".to_string()];
|
||||
let vals = vec![Value::String {
|
||||
val: "Enter".to_string(),
|
||||
span: Span::test_data(),
|
||||
}];
|
||||
let vals = vec![Value::string("Enter", Span::test_data())];
|
||||
|
||||
let span = Span::test_data();
|
||||
let b = EventType::try_from_columns(&cols, &vals, &span);
|
||||
|
@ -74,19 +74,10 @@ pub fn evaluate_repl(
|
||||
// seed env vars
|
||||
stack.add_env_var(
|
||||
"CMD_DURATION_MS".into(),
|
||||
Value::String {
|
||||
val: "0823".to_string(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::string("0823", Span::unknown()),
|
||||
);
|
||||
|
||||
stack.add_env_var(
|
||||
"LAST_EXIT_CODE".into(),
|
||||
Value::Int {
|
||||
val: 0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
);
|
||||
stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown()));
|
||||
|
||||
info!(
|
||||
"load config initially {}:{}:{}",
|
||||
|
@ -289,10 +289,7 @@ pub fn eval_source(
|
||||
fn set_last_exit_code(stack: &mut Stack, exit_code: i64) {
|
||||
stack.add_env_var(
|
||||
"LAST_EXIT_CODE".to_string(),
|
||||
Value::Int {
|
||||
val: exit_code,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::int(exit_code, Span::unknown()),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -54,10 +54,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Apply bits and to two numbers",
|
||||
example: "2 | bits and 2",
|
||||
result: Some(Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(2, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Apply logical and to a list of numbers",
|
||||
|
@ -54,10 +54,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Apply bits or to two numbers",
|
||||
example: "2 | bits or 6",
|
||||
result: Some(Value::Int {
|
||||
val: 6,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(6, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Apply logical or to a list of numbers",
|
||||
|
@ -76,10 +76,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Rotate left a number with 2 bits",
|
||||
example: "17 | bits rol 2",
|
||||
result: Some(Value::Int {
|
||||
val: 68,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(68, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Rotate left a list of numbers with 2 bits",
|
||||
|
@ -76,10 +76,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Rotate right a number with 60 bits",
|
||||
example: "17 | bits ror 60",
|
||||
result: Some(Value::Int {
|
||||
val: 272,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(272, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Rotate right a list of numbers of one byte",
|
||||
|
@ -76,26 +76,17 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Shift left a number by 7 bits",
|
||||
example: "2 | bits shl 7",
|
||||
result: Some(Value::Int {
|
||||
val: 256,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(256, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Shift left a number with 1 byte by 7 bits",
|
||||
example: "2 | bits shl 7 -n 1",
|
||||
result: Some(Value::Int {
|
||||
val: 0,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(0, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Shift left a signed number by 1 bit",
|
||||
example: "0x7F | bits shl 1 -s",
|
||||
result: Some(Value::Int {
|
||||
val: 254,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(254, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Shift left a list of numbers",
|
||||
|
@ -76,10 +76,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Shift right a number with 2 bits",
|
||||
example: "8 | bits shr 2",
|
||||
result: Some(Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(2, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Shift right a list of numbers",
|
||||
|
@ -54,10 +54,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Apply bits xor to two numbers",
|
||||
example: "2 | bits xor 2",
|
||||
result: Some(Value::Int {
|
||||
val: 0,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(0, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Apply logical xor to a list of numbers",
|
||||
|
@ -68,26 +68,17 @@ impl Command for BytesEndsWith {
|
||||
Example {
|
||||
description: "Checks if binary ends with `0x[AA]`",
|
||||
example: "0x[1F FF AA AA] | bytes ends-with 0x[AA]",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Checks if binary ends with `0x[FF AA AA]`",
|
||||
example: "0x[1F FF AA AA] | bytes ends-with 0x[FF AA AA]",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Checks if binary ends with `0x[11]`",
|
||||
example: "0x[1F FF AA AA] | bytes ends-with 0x[11]",
|
||||
result: Some(Value::Bool {
|
||||
val: false,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(false, Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -98,10 +89,7 @@ fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value {
|
||||
Value::Binary {
|
||||
val,
|
||||
span: val_span,
|
||||
} => Value::Bool {
|
||||
val: val.ends_with(&args.pattern),
|
||||
span: *val_span,
|
||||
},
|
||||
} => Value::boolean(val.ends_with(&args.pattern), *val_span),
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
|
@ -70,10 +70,7 @@ fn length(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
Value::Binary {
|
||||
val,
|
||||
span: val_span,
|
||||
} => Value::Int {
|
||||
val: val.len() as i64,
|
||||
span: *val_span,
|
||||
},
|
||||
} => Value::int(val.len() as i64, *val_span),
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
|
@ -74,26 +74,17 @@ impl Command for BytesStartsWith {
|
||||
Example {
|
||||
description: "Checks if binary starts with `0x[1F FF AA]`",
|
||||
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F FF AA]",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Checks if binary starts with `0x[1F]`",
|
||||
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F]",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Checks if binary starts with `0x[1F]`",
|
||||
example: "0x[1F FF AA AA] | bytes starts-with 0x[11]",
|
||||
result: Some(Value::Bool {
|
||||
val: false,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(false, Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -104,10 +95,7 @@ fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value {
|
||||
Value::Binary {
|
||||
val,
|
||||
span: val_span,
|
||||
} => Value::Bool {
|
||||
val: val.starts_with(&args.pattern),
|
||||
span: *val_span,
|
||||
},
|
||||
} => Value::boolean(val.starts_with(&args.pattern), *val_span),
|
||||
other => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!(
|
||||
|
@ -44,38 +44,14 @@ impl Command for Fmt {
|
||||
"upperhex".into(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "0b101010".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "42".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "42".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "4.2e1".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "0x2a".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "0o52".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "4.2E1".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "0x2A".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("0b101010", Span::test_data()),
|
||||
Value::string("42", Span::test_data()),
|
||||
Value::string("42", Span::test_data()),
|
||||
Value::string("4.2e1", Span::test_data()),
|
||||
Value::string("0x2a", Span::test_data()),
|
||||
Value::string("0o52", Span::test_data()),
|
||||
Value::string("4.2E1", Span::test_data()),
|
||||
Value::string("0x2A", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -86,7 +86,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
|
||||
let other = s.trim();
|
||||
|
||||
match other.parse::<f64>() {
|
||||
Ok(x) => Value::Float { val: x, span: head },
|
||||
Ok(x) => Value::float(x, head),
|
||||
Err(reason) => Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
"float".to_string(),
|
||||
@ -97,10 +97,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
|
||||
},
|
||||
}
|
||||
}
|
||||
Value::Int { val: v, span } => Value::Float {
|
||||
val: *v as f64,
|
||||
span: *span,
|
||||
},
|
||||
Value::Int { val: v, span } => Value::float(*v as f64, *span),
|
||||
Value::Bool { val: b, span } => Value::Float {
|
||||
val: match b {
|
||||
true => 1.0,
|
||||
|
@ -113,10 +113,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Convert file size to integer",
|
||||
example: "4KB | into int",
|
||||
result: Some(Value::Int {
|
||||
val: 4000,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(4000, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Convert bool to integer",
|
||||
@ -233,20 +230,14 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
}
|
||||
val.resize(8, 0);
|
||||
|
||||
Value::Int {
|
||||
val: LittleEndian::read_i64(&val),
|
||||
span: *span,
|
||||
}
|
||||
Value::int(LittleEndian::read_i64(&val), *span)
|
||||
} else {
|
||||
while val.len() < 8 {
|
||||
val.insert(0, 0);
|
||||
}
|
||||
val.resize(8, 0);
|
||||
|
||||
Value::Int {
|
||||
val: BigEndian::read_i64(&val),
|
||||
span: *span,
|
||||
}
|
||||
Value::int(BigEndian::read_i64(&val), *span)
|
||||
}
|
||||
}
|
||||
_ => Value::Error {
|
||||
@ -269,13 +260,13 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
// octal
|
||||
{
|
||||
match int_from_string(val, head) {
|
||||
Ok(x) => return Value::Int { val: x, span: head },
|
||||
Ok(x) => return Value::int(x, head),
|
||||
Err(e) => return Value::Error { error: e },
|
||||
}
|
||||
} else if val.starts_with("00") {
|
||||
// It's a padded string
|
||||
match i64::from_str_radix(val, radix) {
|
||||
Ok(n) => return Value::Int { val: n, span: head },
|
||||
Ok(n) => return Value::int(n, head),
|
||||
Err(e) => {
|
||||
return Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
@ -300,7 +291,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
||||
}
|
||||
};
|
||||
match i64::from_str_radix(i.trim(), radix) {
|
||||
Ok(n) => Value::Int { val: n, span: head },
|
||||
Ok(n) => Value::int(n, head),
|
||||
Err(_reason) => Value::Error {
|
||||
error: ShellError::CantConvert("string".to_string(), "int".to_string(), head, None),
|
||||
},
|
||||
|
@ -79,34 +79,22 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "convert integer to string and append three decimal places",
|
||||
example: "5 | into string -d 3",
|
||||
result: Some(Value::String {
|
||||
val: "5.000".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("5.000", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "convert decimal to string and round to nearest integer",
|
||||
example: "1.7 | into string -d 0",
|
||||
result: Some(Value::String {
|
||||
val: "2".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("2", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "convert decimal to string",
|
||||
example: "1.7 | into string -d 1",
|
||||
result: Some(Value::String {
|
||||
val: "1.7".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("1.7", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "convert decimal to string and limit to 2 decimals",
|
||||
example: "1.734 | into string -d 2",
|
||||
result: Some(Value::String {
|
||||
val: "1.73".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("1.73", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "try to convert decimal to string and provide negative decimal points",
|
||||
@ -123,26 +111,17 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "convert decimal to string",
|
||||
example: "4.3 | into string",
|
||||
result: Some(Value::String {
|
||||
val: "4.3".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("4.3", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "convert string to string",
|
||||
example: "'1234' | into string",
|
||||
result: Some(Value::String {
|
||||
val: "1234".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("1234", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "convert boolean to string",
|
||||
example: "true | into string",
|
||||
result: Some(Value::String {
|
||||
val: "true".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("true", Span::test_data())),
|
||||
},
|
||||
// TODO: This should work but does not; see https://github.com/nushell/nushell/issues/7032
|
||||
// Example {
|
||||
|
@ -72,10 +72,7 @@ def-env cd_with_fallback [arg = ""] {
|
||||
vec![Example {
|
||||
description: "Set environment variable by call a custom command",
|
||||
example: r#"def-env foo [] { let-env BAR = "BAZ" }; foo; $env.BAR"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("BAZ", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -51,13 +51,7 @@ little reason to use this over just writing the values as-is."#
|
||||
std::cmp::Ordering::Equal => PipelineData::Value(to_be_echoed[0].clone(), None),
|
||||
|
||||
// When there are no elements, we echo the empty string
|
||||
std::cmp::Ordering::Less => PipelineData::Value(
|
||||
Value::String {
|
||||
val: "".to_string(),
|
||||
span: call.head,
|
||||
},
|
||||
None,
|
||||
),
|
||||
std::cmp::Ordering::Less => PipelineData::Value(Value::string("", call.head), None),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -56,10 +56,7 @@ impl Command for ExportCommand {
|
||||
vec![Example {
|
||||
description: "Export a definition from a module",
|
||||
example: r#"module utils { export def my-command [] { "hello" } }; use utils my-command; my-command"#,
|
||||
result: Some(Value::String {
|
||||
val: "hello".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("hello", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -46,10 +46,7 @@ impl Command for ExportDef {
|
||||
vec![Example {
|
||||
description: "Define a custom command in a module and call it",
|
||||
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
|
||||
result: Some(Value::String {
|
||||
val: "foo".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("foo", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -72,10 +72,7 @@ export def-env cd_with_fallback [arg = ""] {
|
||||
vec![Example {
|
||||
description: "Define a custom command that participates in the environment in a module and call it",
|
||||
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("BAZ", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -48,10 +48,7 @@ impl Command for ExportUse {
|
||||
use eggs foo
|
||||
foo
|
||||
"#,
|
||||
result: Some(Value::String {
|
||||
val: "foo".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("foo", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -91,13 +91,7 @@ impl Command for For {
|
||||
if numbered {
|
||||
Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: idx as i64,
|
||||
span: head,
|
||||
},
|
||||
x,
|
||||
],
|
||||
vals: vec![Value::int(idx as i64, head), x],
|
||||
span: head,
|
||||
}
|
||||
} else {
|
||||
@ -136,13 +130,7 @@ impl Command for For {
|
||||
if numbered {
|
||||
Value::Record {
|
||||
cols: vec!["index".into(), "item".into()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: idx as i64,
|
||||
span: head,
|
||||
},
|
||||
x,
|
||||
],
|
||||
vals: vec![Value::int(idx as i64, head), x],
|
||||
span: head,
|
||||
}
|
||||
} else {
|
||||
|
@ -131,10 +131,7 @@ fn help(
|
||||
});
|
||||
|
||||
cols.push("category".into());
|
||||
vals.push(Value::String {
|
||||
val: sig.category.to_string(),
|
||||
span: head,
|
||||
});
|
||||
vals.push(Value::string(sig.category.to_string(), head));
|
||||
|
||||
cols.push("command_type".into());
|
||||
vals.push(Value::String {
|
||||
@ -231,10 +228,7 @@ fn help(
|
||||
});
|
||||
|
||||
cols.push("category".into());
|
||||
vals.push(Value::String {
|
||||
val: sig.category.to_string(),
|
||||
span: head,
|
||||
});
|
||||
vals.push(Value::string(sig.category.to_string(), head));
|
||||
|
||||
cols.push("command_type".into());
|
||||
vals.push(Value::String {
|
||||
@ -334,11 +328,7 @@ Get the processes on your system actively using CPU:
|
||||
|
||||
You can also learn more at https://www.nushell.sh/book/"#;
|
||||
|
||||
Ok(Value::String {
|
||||
val: msg.into(),
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::string(msg, head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,10 +80,7 @@ impl Command for Loop {
|
||||
vec![Example {
|
||||
description: "Loop while a condition is true",
|
||||
example: "mut x = 0; loop { if $x > 10 { break }; $x = $x + 1 }; $x",
|
||||
result: Some(Value::Int {
|
||||
val: 11,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(11, Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -81,19 +81,13 @@ impl Command for Metadata {
|
||||
data_source: DataSource::Ls,
|
||||
} => {
|
||||
cols.push("source".into());
|
||||
vals.push(Value::String {
|
||||
val: "ls".into(),
|
||||
span: head,
|
||||
})
|
||||
vals.push(Value::string("ls", head))
|
||||
}
|
||||
PipelineMetadata {
|
||||
data_source: DataSource::HtmlThemes,
|
||||
} => {
|
||||
cols.push("source".into());
|
||||
vals.push(Value::String {
|
||||
val: "into html --list".into(),
|
||||
span: head,
|
||||
})
|
||||
vals.push(Value::string("into html --list", head))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -152,19 +146,13 @@ fn build_metadata_record(arg: &Value, metadata: &Option<PipelineMetadata>, head:
|
||||
data_source: DataSource::Ls,
|
||||
} => {
|
||||
cols.push("source".into());
|
||||
vals.push(Value::String {
|
||||
val: "ls".into(),
|
||||
span: head,
|
||||
})
|
||||
vals.push(Value::string("ls", head))
|
||||
}
|
||||
PipelineMetadata {
|
||||
data_source: DataSource::HtmlThemes,
|
||||
} => {
|
||||
cols.push("source".into());
|
||||
vals.push(Value::String {
|
||||
val: "into html --list".into(),
|
||||
span: head,
|
||||
})
|
||||
vals.push(Value::string("into html --list", head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,26 +46,17 @@ impl Command for Module {
|
||||
Example {
|
||||
description: "Define a custom command in a module and call it",
|
||||
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
|
||||
result: Some(Value::String {
|
||||
val: "foo".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("foo", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Define an environment variable in a module",
|
||||
example: r#"module foo { export-env { let-env FOO = "BAZ" } }; use foo; $env.FOO"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("BAZ", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Define a custom command that participates in the environment in a module and call it",
|
||||
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("BAZ", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -50,10 +50,7 @@ impl Command for OverlayList {
|
||||
example: r#"module spam { export def foo [] { "foo" } }
|
||||
overlay use spam
|
||||
overlay list | last"#,
|
||||
result: Some(Value::String {
|
||||
val: "spam".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("spam", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -135,10 +135,7 @@ impl Command for OverlayUse {
|
||||
let mut parent = path.clone();
|
||||
parent.pop();
|
||||
|
||||
let file_pwd = Value::String {
|
||||
val: parent.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
};
|
||||
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
||||
|
||||
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||
}
|
||||
|
@ -79,10 +79,7 @@ impl Command for Use {
|
||||
|
||||
// If so, set the currently evaluated directory (file-relative PWD)
|
||||
if let Some(parent) = maybe_parent {
|
||||
let file_pwd = Value::String {
|
||||
val: parent.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
};
|
||||
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
||||
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||
}
|
||||
|
||||
@ -120,18 +117,12 @@ impl Command for Use {
|
||||
Example {
|
||||
description: "Define a custom command in a module and call it",
|
||||
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
|
||||
result: Some(Value::String {
|
||||
val: "foo".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("foo", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Define a custom command that participates in the environment in a module and call it",
|
||||
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
|
||||
result: Some(Value::String {
|
||||
val: "BAZ".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("BAZ", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -54,97 +54,64 @@ pub fn version(
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("version".to_string());
|
||||
vals.push(Value::String {
|
||||
val: env!("CARGO_PKG_VERSION").to_string(),
|
||||
span: tag,
|
||||
});
|
||||
vals.push(Value::string(env!("CARGO_PKG_VERSION"), tag));
|
||||
|
||||
cols.push("branch".to_string());
|
||||
vals.push(Value::String {
|
||||
val: shadow::BRANCH.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(shadow::BRANCH, call.head));
|
||||
|
||||
let commit_hash: Option<&str> = option_env!("NU_COMMIT_HASH");
|
||||
if let Some(commit_hash) = commit_hash {
|
||||
cols.push("commit_hash".to_string());
|
||||
vals.push(Value::String {
|
||||
val: commit_hash.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(commit_hash, call.head));
|
||||
}
|
||||
|
||||
let build_os: Option<&str> = Some(shadow::BUILD_OS).filter(|x| !x.is_empty());
|
||||
if let Some(build_os) = build_os {
|
||||
cols.push("build_os".to_string());
|
||||
vals.push(Value::String {
|
||||
val: build_os.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(build_os, call.head));
|
||||
}
|
||||
|
||||
let build_target: Option<&str> = Some(shadow::BUILD_TARGET).filter(|x| !x.is_empty());
|
||||
if let Some(build_target) = build_target {
|
||||
cols.push("build_target".to_string());
|
||||
vals.push(Value::String {
|
||||
val: build_target.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(build_target, call.head));
|
||||
}
|
||||
|
||||
let rust_version: Option<&str> = Some(shadow::RUST_VERSION).filter(|x| !x.is_empty());
|
||||
if let Some(rust_version) = rust_version {
|
||||
cols.push("rust_version".to_string());
|
||||
vals.push(Value::String {
|
||||
val: rust_version.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(rust_version, call.head));
|
||||
}
|
||||
|
||||
let rust_channel: Option<&str> = Some(shadow::RUST_CHANNEL).filter(|x| !x.is_empty());
|
||||
if let Some(rust_channel) = rust_channel {
|
||||
cols.push("rust_channel".to_string());
|
||||
vals.push(Value::String {
|
||||
val: rust_channel.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(rust_channel, call.head));
|
||||
}
|
||||
|
||||
let cargo_version: Option<&str> = Some(shadow::CARGO_VERSION).filter(|x| !x.is_empty());
|
||||
if let Some(cargo_version) = cargo_version {
|
||||
cols.push("cargo_version".to_string());
|
||||
vals.push(Value::String {
|
||||
val: cargo_version.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(cargo_version, call.head));
|
||||
}
|
||||
|
||||
let pkg_version: Option<&str> = Some(shadow::PKG_VERSION).filter(|x| !x.is_empty());
|
||||
if let Some(pkg_version) = pkg_version {
|
||||
cols.push("pkg_version".to_string());
|
||||
vals.push(Value::String {
|
||||
val: pkg_version.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(pkg_version, call.head));
|
||||
}
|
||||
|
||||
let build_time: Option<&str> = Some(shadow::BUILD_TIME).filter(|x| !x.is_empty());
|
||||
if let Some(build_time) = build_time {
|
||||
cols.push("build_time".to_string());
|
||||
vals.push(Value::String {
|
||||
val: build_time.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(build_time, call.head));
|
||||
}
|
||||
|
||||
let build_rust_channel: Option<&str> =
|
||||
Some(shadow::BUILD_RUST_CHANNEL).filter(|x| !x.is_empty());
|
||||
if let Some(build_rust_channel) = build_rust_channel {
|
||||
cols.push("build_rust_channel".to_string());
|
||||
vals.push(Value::String {
|
||||
val: build_rust_channel.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
vals.push(Value::string(build_rust_channel, call.head));
|
||||
}
|
||||
|
||||
cols.push("features".to_string());
|
||||
|
@ -30,14 +30,8 @@ impl Command for ColumnsDF {
|
||||
example: "[[a b]; [1 2] [3 4]] | into df | columns",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "a".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "b".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("a", Span::test_data()),
|
||||
Value::string("b", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -67,10 +61,7 @@ fn command(
|
||||
.as_ref()
|
||||
.get_column_names()
|
||||
.iter()
|
||||
.map(|v| Value::String {
|
||||
val: v.to_string(),
|
||||
span: call.head,
|
||||
})
|
||||
.map(|v| Value::string(*v, call.head))
|
||||
.collect();
|
||||
|
||||
let names = Value::List {
|
||||
|
@ -82,10 +82,7 @@ fn command(
|
||||
span: call.head,
|
||||
});
|
||||
|
||||
Value::String {
|
||||
val: v.to_string(),
|
||||
span: call.head,
|
||||
}
|
||||
Value::string(*v, call.head)
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -61,15 +61,9 @@ impl Command for ListDF {
|
||||
span: call.head,
|
||||
};
|
||||
|
||||
let columns = Value::Int {
|
||||
val: df.as_ref().width() as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let columns = Value::int(df.as_ref().width() as i64, call.head);
|
||||
|
||||
let rows = Value::Int {
|
||||
val: df.as_ref().height() as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let rows = Value::int(df.as_ref().height() as i64, call.head);
|
||||
|
||||
let cols = vec![
|
||||
"name".to_string(),
|
||||
|
@ -61,15 +61,9 @@ fn command(
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||
|
||||
let rows = Value::Int {
|
||||
val: df.as_ref().height() as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let rows = Value::int(df.as_ref().height() as i64, call.head);
|
||||
|
||||
let cols = Value::Int {
|
||||
val: df.as_ref().width() as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let cols = Value::int(df.as_ref().width() as i64, call.head);
|
||||
|
||||
let rows_col = Column::new("rows".to_string(), vec![rows]);
|
||||
let cols_col = Column::new("columns".to_string(), vec![cols]);
|
||||
|
@ -32,14 +32,8 @@ impl Command for ExprAsNu {
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["expr".into(), "value".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "column".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "a".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("column", Span::test_data()),
|
||||
Value::string("a", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -38,14 +38,8 @@ impl Command for ExprCol {
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["expr".into(), "value".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "column".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "a".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("column", Span::test_data()),
|
||||
Value::string("a", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -37,14 +37,8 @@ impl Command for ExprLit {
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["expr".into(), "value".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "literal".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "2i64".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("literal", Span::test_data()),
|
||||
Value::string("2i64", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -86,10 +86,7 @@ fn command(
|
||||
)
|
||||
})?;
|
||||
|
||||
let value = Value::Bool {
|
||||
val: !bool.any(),
|
||||
span: call.head,
|
||||
};
|
||||
let value = Value::boolean(!bool.any(), call.head);
|
||||
|
||||
NuDataFrame::try_from_columns(vec![Column::new("all_false".to_string(), vec![value])])
|
||||
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
||||
|
@ -86,10 +86,7 @@ fn command(
|
||||
)
|
||||
})?;
|
||||
|
||||
let value = Value::Bool {
|
||||
val: bool.all(),
|
||||
span: call.head,
|
||||
};
|
||||
let value = Value::boolean(bool.all(), call.head);
|
||||
|
||||
NuDataFrame::try_from_columns(vec![Column::new("all_true".to_string(), vec![value])])
|
||||
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
||||
|
@ -61,10 +61,7 @@ fn command(
|
||||
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
||||
|
||||
let res = df.as_series(call.head)?.null_count();
|
||||
let value = Value::Int {
|
||||
val: res as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let value = Value::int(res as i64, call.head);
|
||||
|
||||
NuDataFrame::try_from_columns(vec![Column::new("count_null".to_string(), vec![value])])
|
||||
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
||||
|
@ -67,10 +67,7 @@ fn command(
|
||||
)
|
||||
})?;
|
||||
|
||||
let value = Value::Int {
|
||||
val: res as i64,
|
||||
span: call.head,
|
||||
};
|
||||
let value = Value::int(res as i64, call.head);
|
||||
|
||||
NuDataFrame::try_from_columns(vec![Column::new("count_unique".to_string(), vec![value])])
|
||||
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
||||
|
@ -110,10 +110,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Format a given date using a given format string.",
|
||||
example: r#""2021-10-22 20:00:12 +01:00" | date format "%Y-%m-%d""#,
|
||||
result: Some(Value::String {
|
||||
val: "2021-10-22".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("2021-10-22", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -429,18 +426,9 @@ pub(crate) fn generate_strftime_list(head: Span, show_parse_only_formats: bool)
|
||||
.map(|s| Value::Record {
|
||||
cols: column_names.clone(),
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: s.spec.to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::String {
|
||||
val: now.format(s.spec).to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::String {
|
||||
val: s.description.to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::string(s.spec, head),
|
||||
Value::string(now.format(s.spec).to_string(), head),
|
||||
Value::string(s.description, head),
|
||||
],
|
||||
span: head,
|
||||
})
|
||||
@ -459,19 +447,15 @@ pub(crate) fn generate_strftime_list(head: Span, show_parse_only_formats: bool)
|
||||
records.push(Value::Record {
|
||||
cols: column_names,
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "%#z".to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::string("%#z", head),
|
||||
Value::String {
|
||||
val: example,
|
||||
span: head,
|
||||
},
|
||||
Value::String {
|
||||
val: "Parsing only: Same as %z but allows minutes to be missing or present."
|
||||
.to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::string(
|
||||
"Parsing only: Same as %z but allows minutes to be missing or present.",
|
||||
head,
|
||||
),
|
||||
],
|
||||
span: head,
|
||||
});
|
||||
|
@ -110,34 +110,13 @@ fn parse_date_into_table(date: Result<DateTime<FixedOffset>, Value>, head: Span)
|
||||
match date {
|
||||
Ok(x) => {
|
||||
let vals = vec![
|
||||
Value::Int {
|
||||
val: x.year() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.month() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.day() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.hour() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.minute() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.second() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::String {
|
||||
val: x.offset().to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::int(x.year() as i64, head),
|
||||
Value::int(x.month() as i64, head),
|
||||
Value::int(x.day() as i64, head),
|
||||
Value::int(x.hour() as i64, head),
|
||||
Value::int(x.minute() as i64, head),
|
||||
Value::int(x.second() as i64, head),
|
||||
Value::string(x.offset().to_string(), head),
|
||||
];
|
||||
Value::Record {
|
||||
cols,
|
||||
|
@ -113,34 +113,13 @@ fn parse_date_into_table(date: Result<DateTime<FixedOffset>, Value>, head: Span)
|
||||
match date {
|
||||
Ok(x) => {
|
||||
let vals = vec![
|
||||
Value::Int {
|
||||
val: x.year() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.month() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.day() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.hour() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.minute() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: x.second() as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::String {
|
||||
val: x.offset().to_string(),
|
||||
span: head,
|
||||
},
|
||||
Value::int(x.year() as i64, head),
|
||||
Value::int(x.month() as i64, head),
|
||||
Value::int(x.day() as i64, head),
|
||||
Value::int(x.hour() as i64, head),
|
||||
Value::int(x.minute() as i64, head),
|
||||
Value::int(x.second() as i64, head),
|
||||
Value::string(x.offset().to_string(), head),
|
||||
];
|
||||
Value::List {
|
||||
vals: vec![Value::Record {
|
||||
|
13
crates/nu-command/src/env/load_env.rs
vendored
13
crates/nu-command/src/env/load_env.rs
vendored
@ -46,13 +46,7 @@ impl Command for LoadEnv {
|
||||
let cwd = current_dir(engine_state, stack)?;
|
||||
let rhs = rhs.as_string()?;
|
||||
let rhs = nu_path::expand_path_with(rhs, cwd);
|
||||
stack.add_env_var(
|
||||
env_var,
|
||||
Value::String {
|
||||
val: rhs.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
},
|
||||
);
|
||||
stack.add_env_var(env_var, Value::string(rhs.to_string_lossy(), call.head));
|
||||
} else {
|
||||
stack.add_env_var(env_var, rhs);
|
||||
}
|
||||
@ -72,10 +66,7 @@ impl Command for LoadEnv {
|
||||
let rhs = nu_path::expand_path_with(rhs, cwd);
|
||||
stack.add_env_var(
|
||||
env_var,
|
||||
Value::String {
|
||||
val: rhs.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
},
|
||||
Value::string(rhs.to_string_lossy(), call.head),
|
||||
);
|
||||
} else {
|
||||
stack.add_env_var(env_var, rhs);
|
||||
|
5
crates/nu-command/src/env/source_env.rs
vendored
5
crates/nu-command/src/env/source_env.rs
vendored
@ -53,10 +53,7 @@ impl Command for SourceEnv {
|
||||
};
|
||||
parent.pop();
|
||||
|
||||
let file_pwd = Value::String {
|
||||
val: parent.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
};
|
||||
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
||||
|
||||
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||
|
||||
|
@ -208,10 +208,7 @@ mod test_examples {
|
||||
// Set up PWD
|
||||
stack.add_env_var(
|
||||
"PWD".to_string(),
|
||||
Value::String {
|
||||
val: cwd.to_string_lossy().to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string(cwd.to_string_lossy(), Span::test_data()),
|
||||
);
|
||||
|
||||
engine_state
|
||||
@ -296,10 +293,7 @@ mod test_examples {
|
||||
|
||||
stack.add_env_var(
|
||||
"PWD".to_string(),
|
||||
Value::String {
|
||||
val: cwd.to_string_lossy().to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string(cwd.to_string_lossy(), Span::test_data()),
|
||||
);
|
||||
|
||||
match nu_engine::eval_block(engine_state, &mut stack, &block, input, true, true) {
|
||||
|
@ -30,11 +30,7 @@ impl Command for IsAdmin {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(Value::Bool {
|
||||
val: is_root(),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::boolean(is_root(), call.head).into_pipeline_data())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -42,10 +38,7 @@ impl Command for IsAdmin {
|
||||
Example {
|
||||
description: "Echo 'iamroot' if nushell is running with admin/root privileges, and 'iamnotroot' if not.",
|
||||
example: r#"if is-admin { echo "iamroot" } else { echo "iamnotroot" }"#,
|
||||
result: Some(Value::String {
|
||||
val: "iamnotroot".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("iamnotroot", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -166,42 +166,27 @@ impl Command for ViewSource {
|
||||
Example {
|
||||
description: "View the source of a code block",
|
||||
example: r#"let abc = { echo 'hi' }; view-source $abc"#,
|
||||
result: Some(Value::String {
|
||||
val: "{ echo 'hi' }".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("{ echo 'hi' }", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a custom command",
|
||||
example: r#"def hi [] { echo 'Hi!' }; view-source hi"#,
|
||||
result: Some(Value::String {
|
||||
val: "{ echo 'Hi!' }".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("{ echo 'Hi!' }", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a custom command, which participates in the caller environment",
|
||||
example: r#"def-env foo [] { let-env BAR = 'BAZ' }; view-source foo"#,
|
||||
result: Some(Value::String {
|
||||
val: "{ let-env BAR = 'BAZ' }".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("{ let-env BAR = 'BAZ' }", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a module",
|
||||
example: r#"module mod-foo { export-env { let-env FOO_ENV = 'BAZ' } }; view-source mod-foo"#,
|
||||
result: Some(Value::String {
|
||||
val: " export-env { let-env FOO_ENV = 'BAZ' }".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string(" export-env { let-env FOO_ENV = 'BAZ' }", Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of an alias",
|
||||
example: r#"alias hello = echo hi; view-source hello"#,
|
||||
result: Some(Value::String {
|
||||
val: "echo hi".to_string(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("echo hi", Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -163,10 +163,7 @@ impl Command for Cd {
|
||||
|
||||
let path_tointo = path.clone();
|
||||
let path_value = Value::String { val: path, span };
|
||||
let cwd = Value::String {
|
||||
val: cwd.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
};
|
||||
let cwd = Value::string(cwd.to_string_lossy(), call.head);
|
||||
|
||||
let mut shells = get_shells(engine_state, stack, cwd);
|
||||
let current_shell = get_current_shell(engine_state, stack);
|
||||
@ -181,10 +178,7 @@ impl Command for Cd {
|
||||
);
|
||||
stack.add_env_var(
|
||||
"NUSHELL_CURRENT_SHELL".into(),
|
||||
Value::Int {
|
||||
val: current_shell as i64,
|
||||
span: call.head,
|
||||
},
|
||||
Value::int(current_shell as i64, call.head),
|
||||
);
|
||||
|
||||
if let Some(oldpwd) = stack.get_env_var(engine_state, "PWD") {
|
||||
|
@ -170,13 +170,7 @@ impl Command for Watch {
|
||||
|
||||
if let Some(position) = block.signature.get_positional(0) {
|
||||
if let Some(position_id) = &position.var_id {
|
||||
stack.add_var(
|
||||
*position_id,
|
||||
Value::String {
|
||||
val: operation.to_string(),
|
||||
span: call.span(),
|
||||
},
|
||||
);
|
||||
stack.add_var(*position_id, Value::string(operation, call.span()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,10 +178,7 @@ impl Command for Watch {
|
||||
if let Some(position_id) = &position.var_id {
|
||||
stack.add_var(
|
||||
*position_id,
|
||||
Value::String {
|
||||
val: path.to_string_lossy().to_string(),
|
||||
span: call.span(),
|
||||
},
|
||||
Value::string(path.to_string_lossy(), call.span()),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -196,13 +187,10 @@ impl Command for Watch {
|
||||
if let Some(position_id) = &position.var_id {
|
||||
stack.add_var(
|
||||
*position_id,
|
||||
Value::String {
|
||||
val: new_path
|
||||
.unwrap_or_else(|| "".into())
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
span: call.span(),
|
||||
},
|
||||
Value::string(
|
||||
new_path.unwrap_or_else(|| "".into()).to_string_lossy(),
|
||||
call.span(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -55,28 +55,16 @@ with 'transpose' first."#
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let stream_test_1 = vec![
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 6,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(2, Span::test_data()),
|
||||
Value::int(4, Span::test_data()),
|
||||
Value::int(6, Span::test_data()),
|
||||
];
|
||||
|
||||
let stream_test_2 = vec![
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "found 2!".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("found 2!", Span::test_data()),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
@ -96,14 +84,8 @@ with 'transpose' first."#
|
||||
description: "Produce a list that has \"two\" for each 2 in the input",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "two".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "two".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("two", Span::test_data()),
|
||||
Value::string("two", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
@ -113,10 +95,7 @@ with 'transpose' first."#
|
||||
description:
|
||||
"Iterate over each element, producing a list showing indexes of any 2s",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::String {
|
||||
val: "found 2 at 1!".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("found 2 at 1!", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -43,24 +43,12 @@ impl Command for EachWhile {
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let stream_test_1 = vec![
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(2, Span::test_data()),
|
||||
Value::int(4, Span::test_data()),
|
||||
];
|
||||
let stream_test_2 = vec![
|
||||
Value::String {
|
||||
val: "Output: 1".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "Output: 2".into(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("Output: 1", Span::test_data()),
|
||||
Value::string("Output: 2", Span::test_data()),
|
||||
];
|
||||
|
||||
vec![
|
||||
@ -84,10 +72,7 @@ impl Command for EachWhile {
|
||||
example: r#"[1 2 3] | each while {|el ind| if $el < 2 { $"value ($el) at ($ind)!"} }"#,
|
||||
description: "Iterate over each element, printing the matching value and its index",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::String {
|
||||
val: "value 1 at 0!".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("value 1 at 0!", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -43,27 +43,18 @@ impl Command for Empty {
|
||||
Example {
|
||||
description: "Check if a string is empty",
|
||||
example: "'' | is-empty",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Check if a list is empty",
|
||||
example: "[] | is-empty",
|
||||
result: Some(Value::Bool {
|
||||
val: true,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(true, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
// TODO: revisit empty cell path semantics for a record.
|
||||
description: "Check if more than one column are empty",
|
||||
example: "[[meal size]; [arepa small] [taco '']] | is-empty meal size",
|
||||
result: Some(Value::Bool {
|
||||
val: false,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(false, Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -84,23 +75,13 @@ fn empty(
|
||||
let val = val.clone();
|
||||
match val.follow_cell_path(&column.members, false) {
|
||||
Ok(Value::Nothing { .. }) => {}
|
||||
Ok(_) => {
|
||||
return Ok(Value::Bool {
|
||||
val: false,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
Ok(_) => return Ok(Value::boolean(false, head).into_pipeline_data()),
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Bool {
|
||||
val: true,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::boolean(true, head).into_pipeline_data())
|
||||
} else {
|
||||
match input {
|
||||
PipelineData::Empty => Ok(PipelineData::Empty),
|
||||
@ -109,30 +90,18 @@ fn empty(
|
||||
let bytes = s.into_bytes();
|
||||
|
||||
match bytes {
|
||||
Ok(s) => Ok(Value::Bool {
|
||||
val: s.item.is_empty(),
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
Ok(s) => Ok(Value::boolean(s.item.is_empty(), head).into_pipeline_data()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
None => Ok(Value::Bool {
|
||||
val: true,
|
||||
span: head,
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
None => Ok(Value::boolean(true, head).into_pipeline_data()),
|
||||
},
|
||||
PipelineData::ListStream(s, ..) => Ok(Value::Bool {
|
||||
val: s.count() == 0,
|
||||
span: head,
|
||||
PipelineData::ListStream(s, ..) => {
|
||||
Ok(Value::boolean(s.count() == 0, head).into_pipeline_data())
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
PipelineData::Value(value, ..) => Ok(Value::Bool {
|
||||
val: value.is_empty(),
|
||||
span: head,
|
||||
PipelineData::Value(value, ..) => {
|
||||
Ok(Value::boolean(value.is_empty(), head).into_pipeline_data())
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,27 +36,15 @@ impl Command for Group {
|
||||
let stream_test_1 = vec![
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 3,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(3, Span::test_data()),
|
||||
Value::int(4, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -149,10 +149,7 @@ pub fn group_by(
|
||||
}
|
||||
|
||||
let value = match collection.get(0) {
|
||||
Some(Value::Error { .. }) | None => Value::String {
|
||||
val: error_key.to_string(),
|
||||
span: name,
|
||||
},
|
||||
Some(Value::Error { .. }) | None => Value::string(error_key, name),
|
||||
Some(return_value) => return_value.clone(),
|
||||
};
|
||||
|
||||
|
@ -78,16 +78,10 @@ fn length_col(
|
||||
|
||||
fn length_row(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
|
||||
match input {
|
||||
PipelineData::Value(Value::Nothing { .. }, ..) => Ok(Value::Int {
|
||||
val: 0,
|
||||
span: call.head,
|
||||
PipelineData::Value(Value::Nothing { .. }, ..) => {
|
||||
Ok(Value::int(0, call.head).into_pipeline_data())
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Int {
|
||||
val: input.into_iter().count() as i64,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::int(input.into_iter().count() as i64, call.head).into_pipeline_data()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,10 +173,10 @@ impl Iterator for RawStreamLinesAdapter {
|
||||
// inner is complete, feed out remaining state
|
||||
if self.inner_complete {
|
||||
if !self.incomplete_line.is_empty() {
|
||||
let r = Some(Ok(Value::String {
|
||||
val: self.incomplete_line.to_string(),
|
||||
span: self.span,
|
||||
}));
|
||||
let r = Some(Ok(Value::string(
|
||||
self.incomplete_line.to_string(),
|
||||
self.span,
|
||||
)));
|
||||
self.incomplete_line = String::new();
|
||||
return r;
|
||||
}
|
||||
|
@ -52,10 +52,7 @@ impl Command for ParEach {
|
||||
example: r#"[1 2 3] | par-each -n { |it| if $it.item == 2 { echo $"found 2 at ($it.index)!"} }"#,
|
||||
description: "Iterate over each element, print the matching value and its index",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::String {
|
||||
val: "found 2 at 1!".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("found 2 at 1!", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -54,26 +54,17 @@ impl Command for Reduce {
|
||||
Example {
|
||||
example: "[ 1 2 3 4 ] | reduce {|it, acc| $it + $acc }",
|
||||
description: "Sum values of a list (same as 'math sum')",
|
||||
result: Some(Value::Int {
|
||||
val: 10,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(10, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
example: "[ 8 7 6 ] | reduce {|it, acc, ind| $acc + $it + $ind }",
|
||||
description: "Sum values of a list, plus their indexes",
|
||||
result: Some(Value::Int {
|
||||
val: 22,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(22, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
example: "[ 1 2 3 4 ] | reduce -f 10 {|it, acc| $acc + $it }",
|
||||
description: "Sum values with a starting value (fold)",
|
||||
result: Some(Value::Int {
|
||||
val: 20,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(20, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
example: r#"[ i o t ] | reduce -f "Arthur, King of the Britons" {|it, acc| $acc | str replace -a $it "X" }"#,
|
||||
|
@ -68,10 +68,7 @@ impl Command for Reject {
|
||||
example: "{a: 1, b: 2} | reject a",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["b".into()],
|
||||
vals: vec![Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(2, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -82,10 +79,7 @@ impl Command for Reject {
|
||||
cols: vec!["a".into()],
|
||||
vals: vec![Value::Record {
|
||||
cols: vec!["c".into()],
|
||||
vals: vec![Value::Int {
|
||||
val: 5,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(5, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
span: Span::test_data(),
|
||||
|
@ -313,10 +313,7 @@ pub fn rotate(
|
||||
for (idx, val) in columns_iter {
|
||||
// when rotating counter clockwise, the old columns names become the first column's values
|
||||
let mut res = if ccw {
|
||||
vec![Value::String {
|
||||
val: val.to_string(),
|
||||
span: call.head,
|
||||
}]
|
||||
vec![Value::string(val, call.head)]
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
@ -329,10 +326,7 @@ pub fn rotate(
|
||||
}
|
||||
// when rotating clockwise, the old column names become the last column's values
|
||||
if !ccw {
|
||||
res.push(Value::String {
|
||||
val: val.to_string(),
|
||||
span: call.head,
|
||||
});
|
||||
res.push(Value::string(val, call.head));
|
||||
}
|
||||
res.to_vec()
|
||||
};
|
||||
|
@ -206,13 +206,7 @@ fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Ve
|
||||
.into_iter()
|
||||
.map(|item| Value::Record {
|
||||
cols: vec!["value".to_string(), "count".to_string()],
|
||||
vals: vec![
|
||||
item.val,
|
||||
Value::Int {
|
||||
val: item.count,
|
||||
span: head,
|
||||
},
|
||||
],
|
||||
vals: vec![item.val, Value::int(item.count, head)],
|
||||
span: head,
|
||||
})
|
||||
.collect()
|
||||
|
@ -43,40 +43,22 @@ impl Command for Window {
|
||||
let stream_test_1 = vec![
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 3,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(2, Span::test_data()),
|
||||
Value::int(3, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 3,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(3, Span::test_data()),
|
||||
Value::int(4, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
@ -85,40 +67,22 @@ impl Command for Window {
|
||||
let stream_test_2 = vec![
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 5,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(4, Span::test_data()),
|
||||
Value::int(5, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 7,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 8,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(7, Span::test_data()),
|
||||
Value::int(8, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
@ -127,31 +91,16 @@ impl Command for Window {
|
||||
let stream_test_3 = vec![
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 3,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
Value::int(3, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 5,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(4, Span::test_data()),
|
||||
Value::int(5, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -37,42 +37,24 @@ impl Command for Zip {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let test_row_1 = Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 4,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(4, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
||||
let test_row_2 = Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 5,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(2, Span::test_data()),
|
||||
Value::int(5, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
||||
let test_row_3 = Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 3,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 6,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(3, Span::test_data()),
|
||||
Value::int(6, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
@ -168,10 +168,7 @@ fn headerfieldvalue_to_value(head: Span, value: &HeaderFieldValue) -> Value {
|
||||
.collect(),
|
||||
span: head,
|
||||
},
|
||||
Unstructured(s) => Value::String {
|
||||
val: s.to_string(),
|
||||
span: head,
|
||||
},
|
||||
Unstructured(s) => Value::string(s, head),
|
||||
Empty => Value::nothing(head),
|
||||
}
|
||||
}
|
||||
|
@ -34,14 +34,8 @@ b=2' | from ini",
|
||||
vals: vec![Value::Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "1".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "2".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("1", Span::test_data()),
|
||||
Value::string("2", Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
|
@ -31,10 +31,7 @@ impl Command for FromJson {
|
||||
description: "Converts json formatted string to table",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string()],
|
||||
vals: vec![Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(1, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -44,20 +41,11 @@ impl Command for FromJson {
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -29,10 +29,7 @@ impl Command for FromNuon {
|
||||
description: "Converts nuon formatted string to table",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string()],
|
||||
vals: vec![Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(1, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -42,20 +39,11 @@ impl Command for FromNuon {
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -133,18 +133,9 @@ fn from_ods(
|
||||
let value = match cell {
|
||||
DataType::Empty => Value::nothing(head),
|
||||
DataType::String(s) => Value::string(s, head),
|
||||
DataType::Float(f) => Value::Float {
|
||||
val: *f,
|
||||
span: head,
|
||||
},
|
||||
DataType::Int(i) => Value::Int {
|
||||
val: *i,
|
||||
span: head,
|
||||
},
|
||||
DataType::Bool(b) => Value::Bool {
|
||||
val: *b,
|
||||
span: head,
|
||||
},
|
||||
DataType::Float(f) => Value::float(*f, head),
|
||||
DataType::Int(i) => Value::int(*i, head),
|
||||
DataType::Bool(b) => Value::boolean(*b, head),
|
||||
_ => Value::nothing(head),
|
||||
};
|
||||
|
||||
|
@ -44,13 +44,13 @@ impl Command for FromSsv {
|
||||
example: r#"'FOO BAR
|
||||
1 2' | from ssv"#,
|
||||
description: "Converts ssv formatted string to table",
|
||||
result: Some(Value::List { vals: vec![Value::Record { cols: vec!["FOO".to_string(), "BAR".to_string()], vals: vec![Value::String { val: "1".to_string(), span: Span::test_data() }, Value::String { val: "2".to_string(), span: Span::test_data() }], span: Span::test_data() }], span: Span::test_data() }),
|
||||
result: Some(Value::List { vals: vec![Value::Record { cols: vec!["FOO".to_string(), "BAR".to_string()], vals: vec![Value::string("1", Span::test_data()), Value::string("2", Span::test_data())], span: Span::test_data() }], span: Span::test_data() }),
|
||||
}, Example {
|
||||
example: r#"'FOO BAR
|
||||
1 2' | from ssv -n"#,
|
||||
description: "Converts ssv formatted string to table but not treating the first row as column names",
|
||||
result: Some(
|
||||
Value::List { vals: vec![Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::String { val: "FOO".to_string(), span: Span::test_data() }, Value::String { val: "BAR".to_string(), span: Span::test_data() }], span: Span::test_data() }, Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::String { val: "1".to_string(), span: Span::test_data() }, Value::String { val: "2".to_string(), span: Span::test_data() }], span: Span::test_data() }], span: Span::test_data() }),
|
||||
Value::List { vals: vec![Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::string("FOO", Span::test_data()), Value::string("BAR", Span::test_data())], span: Span::test_data() }, Value::Record { cols: vec!["column1".to_string(), "column2".to_string()], vals: vec![Value::string("1", Span::test_data()), Value::string("2", Span::test_data())], span: Span::test_data() }], span: Span::test_data() }),
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -29,10 +29,7 @@ impl Command for FromToml {
|
||||
description: "Converts toml formatted string to record",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string()],
|
||||
vals: vec![Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(1, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -43,20 +40,11 @@ b = [1, 2]' | from toml",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string(), "b".to_string()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: 2,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(1, Span::test_data()),
|
||||
Value::int(2, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -57,14 +57,8 @@ END:VCARD' | from vcf",
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "N".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "Foo".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("N", Span::test_data()),
|
||||
Value::string("Foo", Span::test_data()),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
@ -78,14 +72,8 @@ END:VCARD' | from vcf",
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "FN".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "Bar".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("FN", Span::test_data()),
|
||||
Value::string("Bar", Span::test_data()),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
@ -99,14 +87,8 @@ END:VCARD' | from vcf",
|
||||
"params".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: "EMAIL".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::String {
|
||||
val: "foo@bar.com".to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::string("EMAIL", Span::test_data()),
|
||||
Value::string("foo@bar.com", Span::test_data()),
|
||||
Value::Nothing {
|
||||
span: Span::test_data(),
|
||||
},
|
||||
|
@ -133,18 +133,9 @@ fn from_xlsx(
|
||||
let value = match cell {
|
||||
DataType::Empty => Value::nothing(head),
|
||||
DataType::String(s) => Value::string(s, head),
|
||||
DataType::Float(f) => Value::Float {
|
||||
val: *f,
|
||||
span: head,
|
||||
},
|
||||
DataType::Int(i) => Value::Int {
|
||||
val: *i,
|
||||
span: head,
|
||||
},
|
||||
DataType::Bool(b) => Value::Bool {
|
||||
val: *b,
|
||||
span: head,
|
||||
},
|
||||
DataType::Float(f) => Value::float(*f, head),
|
||||
DataType::Int(i) => Value::int(*i, head),
|
||||
DataType::Bool(b) => Value::boolean(*b, head),
|
||||
_ => Value::nothing(head),
|
||||
};
|
||||
|
||||
|
@ -54,10 +54,7 @@ impl Command for FromXml {
|
||||
cols: vec!["children".to_string(), "attributes".to_string()],
|
||||
vals: vec![
|
||||
Value::List {
|
||||
vals: vec![Value::String {
|
||||
val: "Event".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("Event", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Record {
|
||||
@ -201,10 +198,7 @@ mod tests {
|
||||
use nu_protocol::{Spanned, Value};
|
||||
|
||||
fn string(input: impl Into<String>) -> Value {
|
||||
Value::String {
|
||||
val: input.into(),
|
||||
span: Span::test_data(),
|
||||
}
|
||||
Value::string(input, Span::test_data())
|
||||
}
|
||||
|
||||
fn row(entries: IndexMap<String, Value>) -> Value {
|
||||
|
@ -181,10 +181,7 @@ pub fn get_examples() -> Vec<Example> {
|
||||
description: "Converts yaml formatted string to table",
|
||||
result: Some(Value::Record {
|
||||
cols: vec!["a".to_string()],
|
||||
vals: vec![Value::Int {
|
||||
val: 1,
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::int(1, Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -240,10 +237,7 @@ mod test {
|
||||
input: r#"value: "{{ something }}""#,
|
||||
expected: Ok(Value::Record {
|
||||
cols: vec!["value".to_string()],
|
||||
vals: vec![Value::String {
|
||||
val: "{{ something }}".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("{{ something }}", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
@ -252,10 +246,7 @@ mod test {
|
||||
input: r#"value: {{ something }}"#,
|
||||
expected: Ok(Value::Record {
|
||||
cols: vec!["value".to_string()],
|
||||
vals: vec![Value::String {
|
||||
val: "{{ something }}".to_string(),
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
vals: vec![Value::string("{{ something }}", Span::test_data())],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
},
|
||||
|
@ -307,20 +307,14 @@ fn add_month_to_table(
|
||||
if should_show_year_column {
|
||||
indexmap.insert(
|
||||
"year".to_string(),
|
||||
Value::Int {
|
||||
val: month_helper.selected_year as i64,
|
||||
span: tag,
|
||||
},
|
||||
Value::int(month_helper.selected_year as i64, tag),
|
||||
);
|
||||
}
|
||||
|
||||
if should_show_quarter_column {
|
||||
indexmap.insert(
|
||||
"quarter".to_string(),
|
||||
Value::Int {
|
||||
val: month_helper.quarter_number as i64,
|
||||
span: tag,
|
||||
},
|
||||
Value::int(month_helper.quarter_number as i64, tag),
|
||||
);
|
||||
}
|
||||
|
||||
@ -331,10 +325,7 @@ fn add_month_to_table(
|
||||
span: tag,
|
||||
}
|
||||
} else {
|
||||
Value::Int {
|
||||
val: month_helper.selected_month as i64,
|
||||
span: tag,
|
||||
}
|
||||
Value::int(month_helper.selected_month as i64, tag)
|
||||
};
|
||||
|
||||
indexmap.insert("month".to_string(), month_value);
|
||||
@ -349,10 +340,7 @@ fn add_month_to_table(
|
||||
if should_add_day_number_to_table {
|
||||
let adjusted_day_number = day_number - total_start_offset;
|
||||
|
||||
value = Value::Int {
|
||||
val: adjusted_day_number as i64,
|
||||
span: tag,
|
||||
};
|
||||
value = Value::int(adjusted_day_number as i64, tag);
|
||||
|
||||
if let Some(current_day) = current_day_option {
|
||||
if current_day == adjusted_day_number {
|
||||
|
@ -187,10 +187,7 @@ impl Iterator for FloatSeq {
|
||||
return None;
|
||||
}
|
||||
self.index += 1;
|
||||
Some(Value::Float {
|
||||
val: count,
|
||||
span: self.span,
|
||||
})
|
||||
Some(Value::float(count, self.span))
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,10 +205,7 @@ impl Iterator for IntSeq {
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let ret = Some(Value::Int {
|
||||
val: self.count,
|
||||
span: self.span,
|
||||
});
|
||||
let ret = Some(Value::int(self.count, self.span));
|
||||
self.count += self.step;
|
||||
ret
|
||||
}
|
||||
|
@ -46,10 +46,7 @@ impl Command for SubCommand {
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::test_int(50),
|
||||
Value::Float {
|
||||
val: 100.0,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::float(100.0, Span::test_data()),
|
||||
Value::test_int(25),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
|
@ -40,24 +40,14 @@ impl Command for SubCommand {
|
||||
vec![Example {
|
||||
description: "Compute the average of a list of numbers",
|
||||
example: "[-50 100.0 25] | math avg",
|
||||
result: Some(Value::Float {
|
||||
val: 25.0,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(25.0, Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn average(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
let sum = reducer_for(Reduce::Summation);
|
||||
let total = &sum(
|
||||
Value::Int {
|
||||
val: 0,
|
||||
span: *head,
|
||||
},
|
||||
values.to_vec(),
|
||||
*head,
|
||||
)?;
|
||||
let total = &sum(Value::int(0, *head), values.to_vec(), *head)?;
|
||||
match total {
|
||||
Value::Filesize { val, span } => Ok(Value::Filesize {
|
||||
val: val / values.len() as i64,
|
||||
@ -67,14 +57,7 @@ pub fn average(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
val: val / values.len() as i64,
|
||||
span: *span,
|
||||
}),
|
||||
_ => total.div(
|
||||
*head,
|
||||
&Value::Int {
|
||||
val: values.len() as i64,
|
||||
span: *head,
|
||||
},
|
||||
*head,
|
||||
),
|
||||
_ => total.div(*head, &Value::int(values.len() as i64, *head), *head),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,11 +40,7 @@ impl Command for SubCommand {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(Value::Float {
|
||||
val: std::f64::consts::E,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::float(std::f64::consts::E, call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,10 +47,7 @@ impl Command for SubCommand {
|
||||
vec![Example {
|
||||
description: "Evaluate math in the pipeline",
|
||||
example: "'10 / 4' | math eval",
|
||||
result: Some(Value::Float {
|
||||
val: 2.5,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(2.5, Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
@ -104,10 +101,7 @@ pub fn parse(math_expression: &str, span: &Span) -> Result<Value, String> {
|
||||
ctx.var("tau", std::f64::consts::TAU);
|
||||
match meval::eval_str_with_context(math_expression, &ctx) {
|
||||
Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()),
|
||||
Ok(num) => Ok(Value::Float {
|
||||
val: num,
|
||||
span: *span,
|
||||
}),
|
||||
Ok(num) => Ok(Value::float(num, *span)),
|
||||
Err(error) => Err(error.to_string().to_lowercase()),
|
||||
}
|
||||
}
|
||||
|
@ -46,10 +46,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Compute the median of a list of numbers",
|
||||
example: "[3 8 9 12 12 15] | math median",
|
||||
result: Some(Value::Float {
|
||||
val: 10.5,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(10.5, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Compute the medians of the columns of a table",
|
||||
|
@ -171,14 +171,8 @@ pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
fn recreate_value(hashable_value: &HashableType, head: Span) -> Value {
|
||||
let bytes = hashable_value.bytes;
|
||||
match &hashable_value.original_type {
|
||||
NumberTypes::Int => Value::Int {
|
||||
val: i64::from_ne_bytes(bytes),
|
||||
span: head,
|
||||
},
|
||||
NumberTypes::Float => Value::Float {
|
||||
val: f64::from_ne_bytes(bytes),
|
||||
span: head,
|
||||
},
|
||||
NumberTypes::Int => Value::int(i64::from_ne_bytes(bytes), head),
|
||||
NumberTypes::Float => Value::float(f64::from_ne_bytes(bytes), head),
|
||||
NumberTypes::Duration => Value::Duration {
|
||||
val: i64::from_ne_bytes(bytes),
|
||||
span: head,
|
||||
|
@ -40,11 +40,7 @@ impl Command for SubCommand {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(Value::Float {
|
||||
val: std::f64::consts::PI,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::float(std::f64::consts::PI, call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,10 +80,7 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
val: 0,
|
||||
span: *span,
|
||||
}),
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::Int {
|
||||
val: 0,
|
||||
span: *span,
|
||||
}),
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(0, *span)),
|
||||
None => Err(ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
head,
|
||||
@ -114,10 +111,7 @@ pub fn product(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
let initial_value = data.get(0);
|
||||
|
||||
let mut acc = match initial_value {
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::Int {
|
||||
val: 1,
|
||||
span: *span,
|
||||
}),
|
||||
Some(Value::Int { span, .. }) | Some(Value::Float { span, .. }) => Ok(Value::int(1, *span)),
|
||||
None => Err(ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
head,
|
||||
|
@ -64,18 +64,9 @@ impl Command for SubCommand {
|
||||
example: "[1.555 2.333 -3.111] | math round -p 2",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Float {
|
||||
val: 1.56,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Float {
|
||||
val: 2.33,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Float {
|
||||
val: -3.11,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::float(1.56, Span::test_data()),
|
||||
Value::float(2.33, Span::test_data()),
|
||||
Value::float(-3.11, Span::test_data()),
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
|
@ -54,18 +54,12 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Compute the standard deviation of a list of numbers",
|
||||
example: "[1 2 3 4 5] | math stddev",
|
||||
result: Some(Value::Float {
|
||||
val: std::f64::consts::SQRT_2,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(std::f64::consts::SQRT_2, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Compute the sample standard deviation of a list of numbers",
|
||||
example: "[1 2 3 4 5] | math stddev -s",
|
||||
result: Some(Value::Float {
|
||||
val: 1.5811388300841898,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(1.5811388300841898, Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -40,11 +40,7 @@ impl Command for SubCommand {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(Value::Float {
|
||||
val: std::f64::consts::TAU,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::float(std::f64::consts::TAU, call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,36 +46,21 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Get the variance of a list of numbers",
|
||||
example: "echo [1 2 3 4 5] | math variance",
|
||||
result: Some(Value::Float {
|
||||
val: 2.0,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(2.0, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Get the sample variance of a list of numbers",
|
||||
example: "[1 2 3 4 5] | math variance -s",
|
||||
result: Some(Value::Float {
|
||||
val: 2.5,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::float(2.5, Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
|
||||
let n = Value::Int {
|
||||
val: values.len() as i64,
|
||||
span: *span,
|
||||
};
|
||||
let mut sum_x = Value::Int {
|
||||
val: 0,
|
||||
span: *span,
|
||||
};
|
||||
let mut sum_x2 = Value::Int {
|
||||
val: 0,
|
||||
span: *span,
|
||||
};
|
||||
let n = Value::int(values.len() as i64, *span);
|
||||
let mut sum_x = Value::int(0, *span);
|
||||
let mut sum_x2 = Value::int(0, *span);
|
||||
for value in values {
|
||||
let v = match &value {
|
||||
Value::Int { .. }
|
||||
@ -116,10 +101,7 @@ pub fn compute_variance(sample: bool) -> impl Fn(&[Value], &Span) -> Result<Valu
|
||||
)),
|
||||
other => other,
|
||||
}?;
|
||||
let n = Value::Int {
|
||||
val: n as i64,
|
||||
span: *span,
|
||||
};
|
||||
let n = Value::int(n as i64, *span);
|
||||
ss.div(*span, &n, *span)
|
||||
}
|
||||
}
|
||||
|
@ -93,10 +93,7 @@ impl Command for History {
|
||||
val: entry.command_line,
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: idx as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::int(idx as i64, head),
|
||||
],
|
||||
span: head,
|
||||
})
|
||||
@ -183,14 +180,8 @@ impl Command for History {
|
||||
},
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: entry.exit_status.unwrap_or(0),
|
||||
span: head,
|
||||
},
|
||||
Value::Int {
|
||||
val: idx as i64,
|
||||
span: head,
|
||||
},
|
||||
Value::int(entry.exit_status.unwrap_or(0), head),
|
||||
Value::int(idx as i64, head),
|
||||
],
|
||||
span: head,
|
||||
})
|
||||
|
@ -418,11 +418,7 @@ fn display(help: &str, engine_state: &EngineState, stack: &mut Stack, span: Span
|
||||
engine_state,
|
||||
stack,
|
||||
&Call::new(span),
|
||||
Value::String {
|
||||
val: item.to_string(),
|
||||
span: Span::unknown(),
|
||||
}
|
||||
.into_pipeline_data(),
|
||||
Value::string(item, Span::unknown()).into_pipeline_data(),
|
||||
) {
|
||||
let result = output.into_value(Span::unknown());
|
||||
match result.as_string() {
|
||||
|
@ -50,10 +50,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "get a free port between 3121 and 4000",
|
||||
example: "port 3121 4000",
|
||||
result: Some(Value::Int {
|
||||
val: 3121,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::int(3121, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "get a free port from system",
|
||||
@ -101,9 +98,5 @@ fn get_free_port(
|
||||
};
|
||||
|
||||
let free_port = listener.local_addr()?.port();
|
||||
Ok(Value::Int {
|
||||
val: free_port as i64,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::int(free_port as i64, call.head).into_pipeline_data())
|
||||
}
|
||||
|
@ -69,10 +69,7 @@ If you need to distinguish dirs and files, please use `path type`."#
|
||||
Example {
|
||||
description: "Check if a file exists",
|
||||
example: "'C:\\Users\\joe\\todo.txt' | path exists",
|
||||
result: Some(Value::Bool {
|
||||
val: false,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(false, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Check if a file exists in a column",
|
||||
@ -88,10 +85,7 @@ If you need to distinguish dirs and files, please use `path type`."#
|
||||
Example {
|
||||
description: "Check if a file exists",
|
||||
example: "'/home/joe/todo.txt' | path exists",
|
||||
result: Some(Value::Bool {
|
||||
val: false,
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::boolean(false, Span::test_data())),
|
||||
},
|
||||
Example {
|
||||
description: "Check if a file exists in a column",
|
||||
|
@ -95,15 +95,9 @@ fn get_records(entry_type: &str, span: &Span) -> Vec<Value> {
|
||||
}
|
||||
|
||||
fn convert_to_record(edit: &str, entry_type: &str, span: &Span) -> Value {
|
||||
let entry_type = Value::String {
|
||||
val: entry_type.to_string(),
|
||||
span: *span,
|
||||
};
|
||||
let entry_type = Value::string(entry_type, *span);
|
||||
|
||||
let name = Value::String {
|
||||
val: edit.to_string(),
|
||||
span: *span,
|
||||
};
|
||||
let name = Value::string(edit, *span);
|
||||
|
||||
Value::Record {
|
||||
cols: vec!["type".to_string(), "name".to_string()],
|
||||
|
@ -56,14 +56,8 @@ impl Command for TermSize {
|
||||
Ok(Value::Record {
|
||||
cols: vec!["columns".into(), "rows".into()],
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: cols.0 as i64,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::Int {
|
||||
val: rows.0 as i64,
|
||||
span: Span::test_data(),
|
||||
},
|
||||
Value::int(cols.0 as i64, Span::test_data()),
|
||||
Value::int(rows.0 as i64, Span::test_data()),
|
||||
],
|
||||
span: head,
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user