forked from extern/nushell
This commit migrates Value's numeric types to BigInt and BigDecimal. The basic idea is that overflow errors aren't great in a shell environment, and not really necessary. The main immediate consequence is that new errors can occur when serializing Nu values to other formats. You can see this in changes to the various serialization formats (JSON, TOML, etc.). There's a new `CoerceInto` trait that uses the `ToPrimitive` trait from `num_traits` to attempt to coerce a `BigNum` or `BigDecimal` into a target type, and produces a `RangeError` (kind of `ShellError`) if the coercion fails. Another possible future consequence is that certain performance-critical numeric operations might be too slow. If that happens, we can introduce specialized numeric types to help improve the performance of those situations, based on the real-world experience.
98 lines
3.3 KiB
Rust
98 lines
3.3 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::object::{Primitive, Value};
|
|
use crate::prelude::*;
|
|
|
|
pub struct ToTOML;
|
|
|
|
impl WholeStreamCommand for ToTOML {
|
|
fn name(&self) -> &str {
|
|
"to-toml"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("to-toml")
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Convert table into .toml text"
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
to_toml(args, registry)
|
|
}
|
|
}
|
|
|
|
pub fn value_to_toml_value(v: &Tagged<Value>) -> Result<toml::Value, ShellError> {
|
|
Ok(match v.item() {
|
|
Value::Primitive(Primitive::Boolean(b)) => toml::Value::Boolean(*b),
|
|
Value::Primitive(Primitive::Bytes(b)) => toml::Value::Integer(*b as i64),
|
|
Value::Primitive(Primitive::Date(d)) => toml::Value::String(d.to_string()),
|
|
Value::Primitive(Primitive::EndOfStream) => {
|
|
toml::Value::String("<End of Stream>".to_string())
|
|
}
|
|
Value::Primitive(Primitive::BeginningOfStream) => {
|
|
toml::Value::String("<Beginning of Stream>".to_string())
|
|
}
|
|
Value::Primitive(Primitive::Decimal(f)) => {
|
|
toml::Value::Float(f.tagged(v.tag).coerce_into("converting to TOML float")?)
|
|
}
|
|
Value::Primitive(Primitive::Int(i)) => {
|
|
toml::Value::Integer(i.tagged(v.tag).coerce_into("converting to TOML integer")?)
|
|
}
|
|
Value::Primitive(Primitive::Nothing) => toml::Value::String("<Nothing>".to_string()),
|
|
Value::Primitive(Primitive::String(s)) => toml::Value::String(s.clone()),
|
|
Value::Primitive(Primitive::Path(s)) => toml::Value::String(s.display().to_string()),
|
|
|
|
Value::List(l) => toml::Value::Array(collect_values(l)?),
|
|
Value::Block(_) => toml::Value::String("<Block>".to_string()),
|
|
Value::Binary(b) => {
|
|
toml::Value::Array(b.iter().map(|x| toml::Value::Integer(*x as i64)).collect())
|
|
}
|
|
Value::Object(o) => {
|
|
let mut m = toml::map::Map::new();
|
|
for (k, v) in o.entries.iter() {
|
|
m.insert(k.clone(), value_to_toml_value(v)?);
|
|
}
|
|
toml::Value::Table(m)
|
|
}
|
|
})
|
|
}
|
|
|
|
fn collect_values(input: &Vec<Tagged<Value>>) -> Result<Vec<toml::Value>, ShellError> {
|
|
let mut out = vec![];
|
|
|
|
for value in input {
|
|
out.push(value_to_toml_value(value)?);
|
|
}
|
|
|
|
Ok(out)
|
|
}
|
|
|
|
fn to_toml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
let args = args.evaluate_once(registry)?;
|
|
let name_span = args.name_span();
|
|
let out = args.input;
|
|
|
|
Ok(out
|
|
.values
|
|
.map(move |a| match toml::to_string(&value_to_toml_value(&a)?) {
|
|
Ok(val) => {
|
|
return ReturnSuccess::value(
|
|
Value::Primitive(Primitive::String(val)).simple_spanned(name_span),
|
|
)
|
|
}
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
"Expected an object with TOML-compatible structure from pipeline",
|
|
"requires TOML-compatible input",
|
|
name_span,
|
|
format!("{} originates from here", a.item.type_name()),
|
|
a.span(),
|
|
)),
|
|
})
|
|
.to_output_stream())
|
|
}
|