git.delta.rocks / jrsonnet / refs/commits / 13d49041c689

difftreelog

fix convert to integer number on zero fraction

Lach2020-08-15parent: #76dc24d.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/integrations/serde.rs
1use crate::{2	error::{Error::*, LocError, Result},3	throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,4};5use jrsonnet_parser::Visibility;6use serde_json::{Map, Number, Value};7use std::{8	collections::HashMap,9	convert::{TryFrom, TryInto},10	rc::Rc,11};1213impl TryFrom<&Val> for Value {14	type Error = LocError;15	fn try_from(v: &Val) -> Result<Self> {16		Ok(match v {17			Val::Bool(b) => Value::Bool(*b),18			Val::Null => Value::Null,19			Val::Str(s) => Value::String((&s as &str).into()),20			Val::Num(n) => Value::Number(Number::from_f64(*n).expect("to json number")),21			Val::Lazy(v) => (&v.evaluate()?).try_into()?,22			Val::Arr(a) => {23				let mut out = Vec::with_capacity(a.len());24				for item in a.iter() {25					out.push(item.try_into()?);26				}27				Value::Array(out)28			}29			Val::Obj(o) => {30				let mut out = Map::new();31				for key in o.visible_fields() {32					out.insert(33						(&key as &str).into(),34						(&o.get(key)?.expect("field exists")).try_into()?,35					);36				}37				Value::Object(out)38			}39			Val::Func(_) | Val::Intristic(_, _) => {40				throw!(RuntimeError("tried to manifest function".into()))41			}42		})43	}44}4546impl From<&Value> for Val {47	fn from(v: &Value) -> Self {48		match v {49			Value::Null => Val::Null,50			Value::Bool(v) => Val::Bool(*v),51			Value::Number(n) => Val::Num(n.as_f64().expect("as f64")),52			Value::String(s) => Val::Str((s as &str).into()),53			Value::Array(a) => {54				let mut out = Vec::with_capacity(a.len());55				for v in a {56					out.push(v.into());57				}58				Val::Arr(Rc::new(out))59			}60			Value::Object(o) => {61				let mut entries = HashMap::with_capacity(o.len());62				for (k, v) in o {63					entries.insert(64						(k as &str).into(),65						ObjMember {66							add: false,67							visibility: Visibility::Normal,68							invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),69							location: None,70						},71					);72				}73				Val::Obj(ObjValue::new(None, Rc::new(entries)))74			}75		}76	}77}
after · crates/jrsonnet-evaluator/src/integrations/serde.rs
1use crate::{2	error::{Error::*, LocError, Result},3	throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,4};5use jrsonnet_parser::Visibility;6use serde_json::{Map, Number, Value};7use std::{8	collections::HashMap,9	convert::{TryFrom, TryInto},10	rc::Rc,11};1213impl TryFrom<&Val> for Value {14	type Error = LocError;15	fn try_from(v: &Val) -> Result<Self> {16		Ok(match v {17			Val::Bool(b) => Value::Bool(*b),18			Val::Null => Value::Null,19			Val::Str(s) => Value::String((&s as &str).into()),20			Val::Num(n) => Value::Number(if *n <= f64::EPSILON {21				(*n as i64).into()22			} else {23				Number::from_f64(*n).expect("to json number")24			}),25			Val::Lazy(v) => (&v.evaluate()?).try_into()?,26			Val::Arr(a) => {27				let mut out = Vec::with_capacity(a.len());28				for item in a.iter() {29					out.push(item.try_into()?);30				}31				Value::Array(out)32			}33			Val::Obj(o) => {34				let mut out = Map::new();35				for key in o.visible_fields() {36					out.insert(37						(&key as &str).into(),38						(&o.get(key)?.expect("field exists")).try_into()?,39					);40				}41				Value::Object(out)42			}43			Val::Func(_) | Val::Intristic(_, _) => {44				throw!(RuntimeError("tried to manifest function".into()))45			}46		})47	}48}4950impl From<&Value> for Val {51	fn from(v: &Value) -> Self {52		match v {53			Value::Null => Val::Null,54			Value::Bool(v) => Val::Bool(*v),55			Value::Number(n) => Val::Num(n.as_f64().expect("as f64")),56			Value::String(s) => Val::Str((s as &str).into()),57			Value::Array(a) => {58				let mut out = Vec::with_capacity(a.len());59				for v in a {60					out.push(v.into());61				}62				Val::Arr(Rc::new(out))63			}64			Value::Object(o) => {65				let mut entries = HashMap::with_capacity(o.len());66				for (k, v) in o {67					entries.insert(68						(k as &str).into(),69						ObjMember {70							add: false,71							visibility: Visibility::Normal,72							invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),73							location: None,74						},75					);76				}77				Val::Obj(ObjValue::new(None, Rc::new(entries)))78			}79		}80	}81}