Convert the rest of "random" subcommands to engine-p (#3399)

* Convert "random bool" to engine-p

Also implements FromValue for Tagged<BigDecimal> and Tagged<f64>.

* Convert "random dice" to engine-p

* Convert "random uuid" to engine-p

* Covert "random chars" to engine-p

* Convert "random" command to engine-p
This commit is contained in:
Jakub Žádník
2021-05-10 10:07:57 +03:00
committed by GitHub
parent 75156ab0c9
commit 7a583083b8
6 changed files with 69 additions and 31 deletions

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use bigdecimal::ToPrimitive;
use chrono::{DateTime, FixedOffset};
use nu_errors::ShellError;
use nu_protocol::{
@ -116,6 +117,36 @@ impl FromValue for bigdecimal::BigDecimal {
}
}
impl FromValue for Tagged<bigdecimal::BigDecimal> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let tag = v.tag.clone();
match &v.value {
UntaggedValue::Primitive(Primitive::Decimal(d)) => Ok(d.clone().tagged(tag)),
_ => Err(ShellError::labeled_error(
"Can't convert to decimal",
"can't convert to decimal",
tag.span,
)),
}
}
}
impl FromValue for Tagged<f64> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let tag = v.tag.clone();
let decimal: bigdecimal::BigDecimal = FromValue::from_value(v)?;
match decimal.to_f64() {
Some(d) => Ok(d.tagged(tag)),
None => Err(ShellError::labeled_error(
"Can't convert to decimal",
"can't convert to decimal",
tag.span,
)),
}
}
}
impl FromValue for String {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {