allow into duration to take an integer amount of ns#10286
Merged
fdncred merged 3 commits intonushell:mainfrom Sep 9, 2023
Merged
allow into duration to take an integer amount of ns#10286fdncred merged 3 commits intonushell:mainfrom
into duration to take an integer amount of ns#10286fdncred merged 3 commits intonushell:mainfrom
Conversation
Member
Author
|
i wanted to add a let unit: String = call
.get_flag::<String>(engine_state, stack, "unit")
.unwrap()
.unwrap_or("ns".into())
.clone();but keep getting the following error error[E0507]: cannot move out of `unit`, a captured variable in an `FnMut` closure
--> crates/nu-command/src/conversions/into/duration.rs:152:28
|
143 | let unit: String = call
| ---- captured outer variable
...
150 | move |v| {
| -------- captured by this `FnMut` closure
151 | if column_paths.is_empty() {
152 | action(&v, unit, span)
| ^^^^ move occurs because `unit` has type `std::string::String`, which does not implement the `Copy` trait |
Contributor
I don't understand why |
Member
Author
|
woopsie @fdncred |
Contributor
|
Maybe this will help @amtoine? I figured some of it out I think. Add to the signature .named(
"unit",
SyntaxShape::String,
"Unit to convert number into",
Some('u'),
)Parse the unit let unit = match call.get_flag::<String>(engine_state, stack, "unit")? {
Some(sep) => {
if ["ns", "us", "µs", "ms", "sec", "min", "hr", "day", "wk"]
.iter()
.any(|d| d == &sep)
{
sep
} else {
return Err(ShellError::CantConvertToDuration {
details: sep,
dst_span: span,
src_span: span,
help: Some(
"supported units are ns, us/µs, ms, sec, min, hr, day, and wk".to_string(),
),
});
}
}
None => "ns".to_string(),
};Use it in the map input.map(
move |v| {
if column_paths.is_empty() {
action(&v, &unit.clone(), span)
} else {
let unitclone = &unit.clone();
let mut ret = v;
for path in &column_paths {
let r = ret.update_cell_path(
&path.members,
Box::new(move |old| action(old, &unitclone, span)),
);
if let Err(error) = r {
return Value::error(error, span);
}
}
ret
}
},
engine_state.ctrlc.clone(),
)Change the action signaturefn action(input: &Value, unit: &str, span: Span) -> Value { |
Member
Author
|
mm so you use a cloned binding in the |
Member
Author
|
pretty cool now, right? 😏 > random dice --dice 4 --sides 1_000_000_000_000 | each { into duration }
╭───┬──────────────────────────────╮
│ 0 │ 9min 43sec 287ms 758µs 891ns │
│ 1 │ 3min 21sec 594ms 789µs 374ns │
│ 2 │ 5min 11sec 313ms 674µs 663ns │
│ 3 │ 4min 36sec 708ms 968µs 586ns │
╰───┴──────────────────────────────╯ |
Contributor
And made action take a I tested it, looks good to me. |
Contributor
|
thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
related to
Description
because
1_234 | into datetimetakes an integer number ofnsand1_234 | into filesizetakes an integer amount of bytes, i think1_234 | into durationshould also be valid and see1_234as an integer amount ofns😋User-Facing Changes
before
either
or
and
after
Tests + Formatting
new example test
After Submitting