Name the Value conversion functions more clearly (#11851)

# Description
This PR renames the conversion functions on `Value` to be more consistent.
It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions.
The conversion functions on `Value` now come in a few forms:
- `coerce_{type}` takes a `&Value` and attempts to convert the value to
`type` (e.g., `i64` are converted to `f64`). This is the old behavior of
some of the `as_{type}` functions -- these functions have simply been
renamed to better reflect what they do.
- The new `as_{type}` functions take a `&Value` and returns an `Ok`
result only if the value is of `type` (no conversion is attempted). The
returned value will be borrowed if `type` is non-`Copy`, otherwise an
owned value is returned.
- `into_{type}` exists for non-`Copy` types, but otherwise does not
attempt conversion just like `as_type`. It takes an owned `Value` and
always returns an owned result.
- `coerce_into_{type}` has the same relationship with `coerce_{type}` as
`into_{type}` does with `as_{type}`.
- `to_{kind}_string`: conversion to different string formats (debug,
abbreviated, etc.). Only two of the old string conversion functions were
removed, the rest have been renamed only.
- `to_{type}`: other conversion functions. Currently, only `to_path`
exists. (And `to_string` through `Display`.)

This table summaries the above:
| Form | Cost | Input Ownership | Output Ownership | Converts `Value`
case/`type` |
| ---------------------------- | ----- | --------------- |
---------------- | -------- |
| `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No |
| `into_{type}` | Cheap | Owned | Owned | No |
| `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes |
| `coerce_into_{type}` | Cheap | Owned | Owned | Yes |
| `to_{kind}_string` | Expensive | Borrowed | Owned | Yes |
| `to_{type}` | Expensive | Borrowed | Owned | Yes |

# User-Facing Changes
Breaking API change for `Value` in `nu-protocol` which is exposed as
part of the plugin API.
This commit is contained in:
Ian Manske
2024-02-17 18:14:16 +00:00
committed by GitHub
parent 360ebeb0bc
commit 1c49ca503a
117 changed files with 903 additions and 745 deletions

View File

@ -227,7 +227,7 @@ fn detect_columns(
.iter()
.take(end_index)
.skip(start_index)
.map(|v| v.as_string().unwrap_or_default())
.map(|v| v.coerce_string().unwrap_or_default())
.join(" ");
let binding = Value::string(combined, Span::unknown());
let last_seg = vals.split_off(end_index);

View File

@ -115,10 +115,10 @@ mod test {
};
let encoded = encode(test_span, encoding.clone(), expected, test_span, true).unwrap();
let encoded = encoded.as_binary().unwrap();
let encoded = encoded.coerce_into_binary().unwrap();
let decoded = decode(test_span, encoding, encoded).unwrap();
let decoded = decoded.as_string().unwrap();
let decoded = decode(test_span, encoding, &encoded).unwrap();
let decoded = decoded.coerce_into_string().unwrap();
assert_eq!(decoded, expected);
}

View File

@ -159,7 +159,7 @@ fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span:
}
_ => Value::error(
ShellError::DatetimeParseError {
msg: value.debug_value(),
msg: value.to_debug_string(),
span: head_span,
},
head_span,
@ -180,7 +180,7 @@ fn format_helper_rfc2822(value: Value, span: Span) -> Value {
}
_ => Value::error(
ShellError::DatetimeParseError {
msg: value.debug_value(),
msg: value.to_debug_string(),
span,
},
span,

View File

@ -67,7 +67,7 @@ impl Command for FormatDuration {
) -> Result<PipelineData, ShellError> {
let format_value = call
.req::<Value>(engine_state, stack, 0)?
.as_string()?
.coerce_into_string()?
.to_ascii_lowercase();
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);

View File

@ -64,7 +64,7 @@ impl Command for FormatFilesize {
) -> Result<PipelineData, ShellError> {
let format_value = call
.req::<Value>(engine_state, stack, 0)?
.as_string()?
.coerce_into_string()?
.to_ascii_lowercase();
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);

View File

@ -150,7 +150,8 @@ fn operate(
let mut parsed: Vec<Value> = Vec::new();
for v in input {
match v.as_string() {
let v_span = v.span();
match v.coerce_into_string() {
Ok(s) => {
let results = regex_pattern.captures_iter(&s);
@ -168,7 +169,6 @@ fn operate(
}
};
let v_span = v.span();
let record = columns
.iter()
.zip(captures.iter().skip(1))
@ -185,7 +185,7 @@ fn operate(
return Err(ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: head,
src_span: v.span(),
src_span: v_span,
})
}
}
@ -322,21 +322,22 @@ impl Iterator for ParseStreamer {
}
let v = self.stream.next()?;
let span = v.span();
let Ok(s) = v.as_string() else {
let Ok(s) = v.coerce_into_string() else {
return Some(Value::error(
ShellError::PipelineMismatch {
exp_input_type: "string".into(),
dst_span: self.span,
src_span: v.span(),
src_span: span,
},
v.span(),
span,
));
};
let parsed = stream_helper(
self.regex.clone(),
v.span(),
span,
s,
self.columns.clone(),
&mut self.excess,

View File

@ -124,7 +124,7 @@ fn split_chars_helper(v: &Value, name: Span, graphemes: bool) -> Value {
Value::Error { error, .. } => Value::error(*error.clone(), span),
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
if let Ok(s) = v.coerce_string() {
Value::list(
if graphemes {
s.graphemes(true)

View File

@ -148,7 +148,7 @@ fn split_column_helper(
collapse_empty: bool,
head: Span,
) -> Vec<Value> {
if let Ok(s) = v.as_string() {
if let Ok(s) = v.coerce_string() {
let split_result: Vec<_> = separator
.split(&s)
.filter(|x| !(collapse_empty && x.is_empty()))

View File

@ -27,8 +27,8 @@ impl Command for SubCommand {
"The value that denotes what separates the list.",
)
.switch(
"regex",
"separator is a regular expression, matching values that can be coerced into a string",
"regex",
"separator is a regular expression, matching values that can be coerced into a string",
Some('r'))
.category(Category::Filters)
}
@ -160,7 +160,7 @@ enum Matcher {
impl Matcher {
pub fn new(regex: bool, lhs: Value) -> Result<Self, ShellError> {
if regex {
Ok(Matcher::Regex(Regex::new(&lhs.as_string()?).map_err(
Ok(Matcher::Regex(Regex::new(&lhs.coerce_string()?).map_err(
|e| ShellError::GenericError {
error: "Error with regular expression".into(),
msg: e.to_string(),
@ -180,7 +180,7 @@ impl Matcher {
pub fn compare(&self, rhs: &Value) -> Result<bool, ShellError> {
Ok(match self {
Matcher::Regex(regex) => {
if let Ok(rhs_str) = rhs.as_string() {
if let Ok(rhs_str) = rhs.coerce_string() {
regex.is_match(&rhs_str)
} else {
false

View File

@ -152,7 +152,7 @@ fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Sp
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
if let Ok(s) = v.coerce_string() {
match max_split {
Some(max_split) => regex
.splitn(&s, max_split)

View File

@ -158,7 +158,7 @@ fn split_words_helper(v: &Value, word_length: Option<usize>, span: Span, graphem
Value::Error { error, .. } => Value::error(*error.clone(), v_span),
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
if let Ok(s) = v.coerce_string() {
// let splits = s.unicode_words();
// let words = trim_to_words(s);
// let words: Vec<&str> = s.split_whitespace().collect();

View File

@ -199,10 +199,10 @@ impl Command for SubCommand {
input.map(
move |v| {
let value_span = v.span();
match v.as_string() {
match v.coerce_into_string() {
Ok(s) => {
let contents = if is_path { s.replace('\\', "\\\\") } else { s };
str_expand(&contents, span, v.span())
str_expand(&contents, span, value_span)
}
Err(_) => Value::error(
ShellError::PipelineMismatch {

View File

@ -49,19 +49,19 @@ impl Command for StrJoin {
let config = engine_state.get_config();
// let output = input.collect_string(&separator.unwrap_or_default(), &config)?;
// Hmm, not sure what we actually want. If you don't use debug_string, Date comes out as human readable
// which feels funny
// Hmm, not sure what we actually want.
// `to_formatted_string` formats dates as human readable which feels funny.
let mut strings: Vec<String> = vec![];
for value in input {
match value {
let str = match value {
Value::Error { error, .. } => {
return Err(*error);
}
value => {
strings.push(value.debug_string("\n", config));
}
}
Value::Date { val, .. } => format!("{val:?}"),
value => value.to_expanded_string("\n", config),
};
strings.push(str);
}
let output = if let Some(separator) = separator {

View File

@ -100,7 +100,7 @@ fn stats(
return Value::error(*error, span);
}
// Now, check if it's a string.
match v.as_string() {
match v.coerce_into_string() {
Ok(s) => counter(&s, span),
Err(_) => Value::error(
ShellError::PipelineMismatch {