From 85da91a0ab3411e99e1e3913f2828bd9cdcf5166 Mon Sep 17 00:00:00 2001 From: Victor Porof Date: Wed, 1 Feb 2017 11:12:16 +0100 Subject: [PATCH] Add helper functions constructing OrderedFloat and BigInt to edn crate, r=ncalexan,rnewman. Fixes #198 Signed-off-by: Victor Porof --- edn/src/types.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/edn/src/types.rs b/edn/src/types.rs index 47e4d8b3..4fb651ae 100644 --- a/edn/src/types.rs +++ b/edn/src/types.rs @@ -8,12 +8,14 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +#![allow(unused_imports)] + use std::collections::{BTreeSet, BTreeMap, LinkedList}; use std::cmp::{Ordering, Ord, PartialOrd}; use std::fmt::{Display, Formatter}; use symbols; -use num::BigInt; +use num::bigint::{BigInt, ToBigInt, ParseBigIntError}; use ordered_float::OrderedFloat; /// Value represents one of the allowed values in an EDN string. @@ -73,6 +75,12 @@ impl Display for Value { } } +#[test] +fn test_value_from() { + assert_eq!(Value::from(42f64), Value::Float(OrderedFloat::from(42f64))); + assert_eq!(Value::from_bigint("42").unwrap(), Value::BigInteger(42.to_bigint().unwrap())); +} + #[test] fn test_print_edn() { assert_eq!("[ 1 2 [ 3.1 ] [ ] :five :six/seven eight nine/ten true ]", @@ -143,6 +151,16 @@ impl Value { def_as!(as_list, List, LinkedList); def_as!(as_set, Set, BTreeSet); def_as!(as_map, Map, BTreeMap); + + pub fn from_bigint(src: &str) -> Option { + src.parse::().map(Value::BigInteger).ok() + } +} + +impl From for Value { + fn from(src: f64) -> Value { + Value::Float(OrderedFloat::from(src)) + } } impl PartialOrd for Value {