forked from extern/nushell
Move Value to helpers, separate span call (#10121)
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
@ -90,8 +90,8 @@ fn action(
|
||||
"binhex" => GeneralPurpose::new(&alphabet::BIN_HEX, NO_PAD),
|
||||
"crypt" => GeneralPurpose::new(&alphabet::CRYPT, NO_PAD),
|
||||
"mutf7" => GeneralPurpose::new(&alphabet::IMAP_MUTF7, NO_PAD),
|
||||
not_valid => return Value::Error { error:
|
||||
Box::new(ShellError::GenericError(
|
||||
not_valid => return Value::error (
|
||||
ShellError::GenericError(
|
||||
"value is not an accepted character set".to_string(),
|
||||
format!(
|
||||
"{not_valid} is not a valid character-set.\nPlease use `help encode base64` to see a list of valid character sets."
|
||||
@ -99,48 +99,46 @@ fn action(
|
||||
Some(config_character_set.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)), span:config_character_set.span}
|
||||
), config_character_set.span)
|
||||
};
|
||||
let value_span = input.span();
|
||||
match input {
|
||||
// Propagate existing errors.
|
||||
Value::Error { .. } => input.clone(),
|
||||
Value::Binary { val, span } => match base64_config.action_type {
|
||||
Value::Binary { val, .. } => match base64_config.action_type {
|
||||
ActionType::Encode => {
|
||||
let mut enc_vec = Vec::new();
|
||||
enc_vec.resize(val.len() * 4 / 3 + 4, 0);
|
||||
let bytes_written = match base64_engine.encode_slice(val, &mut enc_vec) {
|
||||
Ok(bytes_written) => bytes_written,
|
||||
Err(err) => {
|
||||
return Value::Error {
|
||||
error: Box::new(ShellError::GenericError(
|
||||
return Value::error(
|
||||
ShellError::GenericError(
|
||||
"Error encoding data".into(),
|
||||
err.to_string(),
|
||||
Some(*span),
|
||||
Some(value_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
span: *span,
|
||||
}
|
||||
),
|
||||
value_span,
|
||||
)
|
||||
}
|
||||
};
|
||||
enc_vec.truncate(bytes_written);
|
||||
Value::string(std::str::from_utf8(&enc_vec).unwrap_or(""), command_span)
|
||||
}
|
||||
ActionType::Decode => Value::Error {
|
||||
error: Box::new(ShellError::UnsupportedInput(
|
||||
ActionType::Decode => Value::error(
|
||||
ShellError::UnsupportedInput(
|
||||
"Binary data can only be encoded".to_string(),
|
||||
"value originates from here".into(),
|
||||
command_span,
|
||||
// This line requires the Value::Error {} match above.
|
||||
input.span(),
|
||||
)),
|
||||
span: command_span,
|
||||
},
|
||||
),
|
||||
command_span,
|
||||
),
|
||||
},
|
||||
Value::String {
|
||||
val,
|
||||
span: value_span,
|
||||
} => {
|
||||
Value::String { val, .. } => {
|
||||
match base64_config.action_type {
|
||||
ActionType::Encode => {
|
||||
let mut enc_str = String::new();
|
||||
@ -160,22 +158,22 @@ fn action(
|
||||
} else {
|
||||
match String::from_utf8(decoded_value) {
|
||||
Ok(string_value) => Value::string(string_value, command_span),
|
||||
Err(e) => Value::Error {
|
||||
error: Box::new(ShellError::GenericError(
|
||||
Err(e) => Value::error(
|
||||
ShellError::GenericError(
|
||||
"base64 payload isn't a valid utf-8 sequence"
|
||||
.to_owned(),
|
||||
e.to_string(),
|
||||
Some(*value_span),
|
||||
Some(value_span),
|
||||
Some("consider using the `--binary` flag".to_owned()),
|
||||
Vec::new(),
|
||||
)),
|
||||
span: *value_span,
|
||||
},
|
||||
),
|
||||
value_span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => Value::Error {
|
||||
error: Box::new(ShellError::GenericError(
|
||||
Err(_) => Value::error(
|
||||
ShellError::GenericError(
|
||||
"value could not be base64 decoded".to_string(),
|
||||
format!(
|
||||
"invalid base64 input for character set {}",
|
||||
@ -184,20 +182,20 @@ fn action(
|
||||
Some(command_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
span: command_span,
|
||||
},
|
||||
),
|
||||
command_span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
other => Value::Error {
|
||||
error: Box::new(ShellError::TypeMismatch {
|
||||
other => Value::error(
|
||||
ShellError::TypeMismatch {
|
||||
err_message: format!("string or binary, not {}", other.get_type()),
|
||||
span: other.span(),
|
||||
}),
|
||||
span: other.span(),
|
||||
},
|
||||
},
|
||||
other.span(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@ -323,10 +321,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn base64_encode_binary() {
|
||||
let word = Value::Binary {
|
||||
val: vec![77, 97, 110],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let word = Value::binary(vec![77, 97, 110], Span::test_data());
|
||||
let expected = Value::test_string("TWFu");
|
||||
|
||||
let actual = action(
|
||||
@ -349,10 +344,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn base64_decode_binary_expect_error() {
|
||||
let word = Value::Binary {
|
||||
val: vec![77, 97, 110],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let word = Value::binary(vec![77, 97, 110], Span::test_data());
|
||||
|
||||
let actual = action(
|
||||
&word,
|
||||
|
@ -47,10 +47,7 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
|
||||
Example {
|
||||
description: "Decode an UTF-16 string into nushell UTF-8 string",
|
||||
example: r#"0x[00 53 00 6F 00 6D 00 65 00 20 00 44 00 61 00 74 00 61] | decode utf-16be"#,
|
||||
result: Some(Value::String {
|
||||
val: "Some Data".to_owned(),
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::string("Some Data".to_owned(), Span::test_data())),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -77,29 +74,29 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
|
||||
Some(encoding_name) => super::encoding::decode(head, encoding_name, &bytes),
|
||||
None => super::encoding::detect_encoding_name(head, input_span, &bytes)
|
||||
.map(|encoding| encoding.decode(&bytes).0.into_owned())
|
||||
.map(|s| Value::String { val: s, span: head }),
|
||||
.map(|s| Value::string(s, head)),
|
||||
}
|
||||
.map(|val| val.into_pipeline_data())
|
||||
}
|
||||
PipelineData::Value(v, ..) => match v {
|
||||
Value::Binary {
|
||||
val: bytes,
|
||||
span: input_span,
|
||||
} => match encoding {
|
||||
Some(encoding_name) => super::encoding::decode(head, encoding_name, &bytes),
|
||||
None => super::encoding::detect_encoding_name(head, input_span, &bytes)
|
||||
.map(|encoding| encoding.decode(&bytes).0.into_owned())
|
||||
.map(|s| Value::String { val: s, span: head }),
|
||||
PipelineData::Value(v, ..) => {
|
||||
let input_span = v.span();
|
||||
match v {
|
||||
Value::Binary { val: bytes, .. } => match encoding {
|
||||
Some(encoding_name) => super::encoding::decode(head, encoding_name, &bytes),
|
||||
None => super::encoding::detect_encoding_name(head, input_span, &bytes)
|
||||
.map(|encoding| encoding.decode(&bytes).0.into_owned())
|
||||
.map(|s| Value::string(s, head)),
|
||||
}
|
||||
.map(|val| val.into_pipeline_data()),
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
_ => Err(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "binary".into(),
|
||||
wrong_type: v.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: v.span(),
|
||||
}),
|
||||
}
|
||||
.map(|val| val.into_pipeline_data()),
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
_ => Err(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "binary".into(),
|
||||
wrong_type: v.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: v.span(),
|
||||
}),
|
||||
},
|
||||
}
|
||||
// This should be more precise, but due to difficulties in getting spans
|
||||
// from PipelineData::ListData, this is as it is.
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
|
@ -53,23 +53,23 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
|
||||
Example {
|
||||
description: "Encode an UTF-8 string into Shift-JIS",
|
||||
example: r#""負けると知って戦うのが、遥かに美しいのだ" | encode shift-jis"#,
|
||||
result: Some(Value::Binary {
|
||||
val: vec![
|
||||
result: Some(Value::binary(
|
||||
vec![
|
||||
0x95, 0x89, 0x82, 0xaf, 0x82, 0xe9, 0x82, 0xc6, 0x92, 0x6d, 0x82, 0xc1,
|
||||
0x82, 0xc4, 0x90, 0xed, 0x82, 0xa4, 0x82, 0xcc, 0x82, 0xaa, 0x81, 0x41,
|
||||
0x97, 0x79, 0x82, 0xa9, 0x82, 0xc9, 0x94, 0xfc, 0x82, 0xb5, 0x82, 0xa2,
|
||||
0x82, 0xcc, 0x82, 0xbe,
|
||||
],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
Span::test_data(),
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
description: "Replace characters with HTML entities if they can't be encoded",
|
||||
example: r#""🎈" | encode -i shift-jis"#,
|
||||
result: Some(Value::Binary {
|
||||
val: vec![0x26, 0x23, 0x31, 0x32, 0x37, 0x38, 0x38, 0x30, 0x3b],
|
||||
span: Span::test_data(),
|
||||
}),
|
||||
result: Some(Value::binary(
|
||||
vec![0x26, 0x23, 0x31, 0x32, 0x37, 0x38, 0x38, 0x30, 0x3b],
|
||||
Span::test_data(),
|
||||
)),
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -95,19 +95,22 @@ documentation link at https://docs.rs/encoding_rs/latest/encoding_rs/#statics"#
|
||||
super::encoding::encode(head, encoding, &s.item, s.span, ignore_errors)
|
||||
.map(|val| val.into_pipeline_data())
|
||||
}
|
||||
PipelineData::Value(v, ..) => match v {
|
||||
Value::String { val: s, span } => {
|
||||
super::encoding::encode(head, encoding, &s, span, ignore_errors)
|
||||
.map(|val| val.into_pipeline_data())
|
||||
PipelineData::Value(v, ..) => {
|
||||
let span = v.span();
|
||||
match v {
|
||||
Value::String { val: s, .. } => {
|
||||
super::encoding::encode(head, encoding, &s, span, ignore_errors)
|
||||
.map(|val| val.into_pipeline_data())
|
||||
}
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
_ => Err(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "string".into(),
|
||||
wrong_type: v.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: v.span(),
|
||||
}),
|
||||
}
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
_ => Err(ShellError::OnlySupportsThisInputType {
|
||||
exp_input_type: "string".into(),
|
||||
wrong_type: v.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: v.span(),
|
||||
}),
|
||||
},
|
||||
}
|
||||
// This should be more precise, but due to difficulties in getting spans
|
||||
// from PipelineData::ListStream, this is as it is.
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
|
@ -34,10 +34,7 @@ pub fn decode(
|
||||
parse_encoding(encoding_name.span, &encoding_name.item)
|
||||
}?;
|
||||
let (result, ..) = encoding.decode(bytes);
|
||||
Ok(Value::String {
|
||||
val: result.into_owned(),
|
||||
span: head,
|
||||
})
|
||||
Ok(Value::string(result.into_owned(), head))
|
||||
}
|
||||
|
||||
pub fn encode(
|
||||
@ -66,10 +63,7 @@ pub fn encode(
|
||||
vec![],
|
||||
))
|
||||
} else {
|
||||
Ok(Value::Binary {
|
||||
val: result.into_owned(),
|
||||
span: head,
|
||||
})
|
||||
Ok(Value::binary(result.into_owned(), head))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user