Add helper functions constructing OrderedFloat and BigInt to edn crate, r=ncalexan,rnewman. Fixes #198

Signed-off-by: Victor Porof <vporof@mozilla.com>
This commit is contained in:
Victor Porof 2017-02-01 11:12:16 +01:00
parent 93053a4297
commit 85da91a0ab

View file

@ -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<Value>);
def_as!(as_set, Set, BTreeSet<Value>);
def_as!(as_map, Map, BTreeMap<Value, Value>);
pub fn from_bigint(src: &str) -> Option<Value> {
src.parse::<BigInt>().map(Value::BigInteger).ok()
}
}
impl From<f64> for Value {
fn from(src: f64) -> Value {
Value::Float(OrderedFloat::from(src))
}
}
impl PartialOrd for Value {