From a5c49b9922b2a6be589d455fbf6677217ebaa2be Mon Sep 17 00:00:00 2001 From: Yuheng Chen Date: Sat, 13 May 2017 21:22:19 +0800 Subject: [PATCH] Parse special f64 in tag --- parser/src/yaml.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/parser/src/yaml.rs b/parser/src/yaml.rs index 99000df..657de55 100644 --- a/parser/src/yaml.rs +++ b/parser/src/yaml.rs @@ -55,6 +55,17 @@ pub enum Yaml { pub type Array = Vec; pub type Hash = LinkedHashMap; +// parse f64 as Core schema +// See: https://github.com/chyh1990/yaml-rust/issues/51 +fn parse_f64(v: &str) -> Option { + match v { + ".inf" | ".Inf" | ".INF" | "+.inf" | "+.Inf" | "+.INF" => Some(f64::INFINITY), + "-.inf" | "-.Inf" | "-.INF" => Some(f64::NEG_INFINITY), + ".nan" | "NaN" | ".NAN" => Some(f64::NAN), + _ => v.parse::().ok() + } +} + pub struct YamlLoader { docs: Vec, // states @@ -116,9 +127,9 @@ impl MarkedEventReceiver for YamlLoader { } }, "float" => { - match v.parse::() { - Err(_) => Yaml::BadValue, - Ok(_) => Yaml::Real(v.clone()) + match parse_f64(v) { + Some(_) => Yaml::Real(v.clone()), + None => Yaml::BadValue, } }, "null" => { @@ -227,15 +238,6 @@ pub fn $name(self) -> Option<$t> { ); ); -fn parse_f64(v: &str) -> Option { - match v { - ".inf" | ".Inf" | ".INF" | "+.inf" | "+.Inf" | "+.INF" => Some(f64::INFINITY), - "-.inf" | "-.Inf" | "-.INF" => Some(f64::NEG_INFINITY), - ".nan" | "NaN" | ".NAN" => Some(f64::NAN), - _ => v.parse::().ok() - } -} - impl Yaml { define_as!(as_bool, bool, Boolean); define_as!(as_i64, i64, Integer); @@ -570,6 +572,7 @@ a1: &DEFAULT - +12345 - -.INF - .NAN +- !!float .INF "; let mut out = YamlLoader::load_from_str(&s).unwrap().into_iter(); let mut doc = out.next().unwrap().into_iter(); @@ -593,6 +596,7 @@ a1: &DEFAULT assert_eq!(doc.next().unwrap().into_i64().unwrap(), 12345); assert_eq!(doc.next().unwrap().into_f64().unwrap(), f64::NEG_INFINITY); assert!(doc.next().unwrap().into_f64().is_some()); + assert_eq!(doc.next().unwrap().into_f64().unwrap(), f64::INFINITY); } #[test]