difftreelog
fix convert to integer number on zero fraction
in: master
1 file changed
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth1use 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}