New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow into duration
to take an integer amount of ns
#10286
Conversation
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 |
I don't understand why |
woopsie @fdncred |
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 { |
mm so you use a cloned binding in the |
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 โ
โฐโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ |
And made action take a I tested it, looks good to me. |
thanks |
related to
Description
because
1_234 | into datetime
takes an integer number ofns
and1_234 | into filesize
takes an integer amount of bytes, i think1_234 | into duration
should also be valid and see1_234
as an integer amount ofns
๐User-Facing Changes
before
either
or
and
after
Tests + Formatting
new example test
After Submitting