diff options
author | Alex Crichton <alex@alexcrichton.com> | 2016-12-30 17:40:23 -0800 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2016-12-30 17:40:40 -0800 |
commit | c391600de19a326a5c9cacba3e833c95de9750a1 (patch) | |
tree | 2698d76c259adfb2e8a5c24813b74f0146e8e8af /src | |
parent | f6992255dff64e42e690a2ab02bf3ea054157dd2 (diff) | |
download | milf-rs-c391600de19a326a5c9cacba3e833c95de9750a1.tar.gz milf-rs-c391600de19a326a5c9cacba3e833c95de9750a1.zip |
Return encoding errors for NaN and infinite
TOML can't actually represent them, so we should bail out.
Closes #125
Diffstat (limited to 'src')
-rw-r--r-- | src/encoder/mod.rs | 19 |
1 files changed, 19 insertions, 0 deletions
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), } } |