From c391600de19a326a5c9cacba3e833c95de9750a1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 30 Dec 2016 17:40:23 -0800 Subject: Return encoding errors for NaN and infinite TOML can't actually represent them, so we should bail out. Closes #125 --- src/encoder/mod.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index cd52b66..910c970 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -62,6 +62,12 @@ pub enum Error { /// Indicates that a type other than a string was attempted to be used as a /// map key type. InvalidMapKeyType, + /// An error returned whenever a `NaN` value for a float is attempted to be + /// encoded + NanEncoded, + /// An error returned whenever an infinity value for a float is attempted to + /// be encoded + InfinityEncoded, /// A custom error type was generated Custom(String), } @@ -91,6 +97,17 @@ impl Encoder { } fn emit_value(&mut self, v: Value) -> Result<(), Error> { + match v { + Value::Float(f) => { + if f.is_nan() { + return Err(Error::NanEncoded) + } + if f.is_infinite() { + return Err(Error::InfinityEncoded) + } + } + _ => {} + } match mem::replace(&mut self.state, State::Start) { State::NextKey(key) => { self.toml.insert(key, v); Ok(()) } State::NextArray(mut vec) => { @@ -193,6 +210,8 @@ impl fmt::Display for Error { at this location"), Error::InvalidMapKeyType => write!(f, "only strings can be used as \ key types"), + Error::NanEncoded => write!(f, "cannot encode NaN"), + Error::InfinityEncoded => write!(f, "cannot encode infinity"), Error::Custom(ref s) => write!(f, "custom error: {}", s), } } -- cgit v1.2.3