aboutsummaryrefslogtreecommitdiff
path: root/src/ser.rs
diff options
context:
space:
mode:
authorEric Huss <eric@huss.org>2018-07-10 16:27:58 -0700
committerEric Huss <eric@huss.org>2018-07-11 00:50:04 -0700
commit69b4571c6bafcd7d9f675d3eb49d4c088d372eea (patch)
tree3a1715fa18ba7640c12fea20ffaa762206c95c62 /src/ser.rs
parent2be7ce9079fa02d9dd42958410ba1c40cef9811c (diff)
downloadmilf-rs-69b4571c6bafcd7d9f675d3eb49d4c088d372eea.tar.gz
milf-rs-69b4571c6bafcd7d9f675d3eb49d4c088d372eea.zip
0.5: Support floats nan, inf, and +/-0.0.
cc #224
Diffstat (limited to 'src/ser.rs')
-rw-r--r--src/ser.rs49
1 files changed, 23 insertions, 26 deletions
diff --git a/src/ser.rs b/src/ser.rs
index 8f4e72b..9d989db 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -737,6 +737,27 @@ impl<'a> Serializer<'a> {
}
}
+macro_rules! serialize_float {
+ ($this:expr, $v:expr) => {{
+ $this.emit_key("float")?;
+ if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() {
+ drop(write!($this.dst, "-"));
+ }
+ if $v.is_nan() {
+ drop(write!($this.dst, "nan"));
+ } else {
+ drop(write!($this.dst, "{}", $v));
+ }
+ if $v % 1.0 == 0.0 {
+ drop(write!($this.dst, ".0"));
+ }
+ if let State::Table { .. } = $this.state {
+ $this.dst.push_str("\n");
+ }
+ return Ok(());
+ }};
+}
+
impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
type Ok = ();
type Error = Error;
@@ -785,35 +806,11 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
}
fn serialize_f32(self, v: f32) -> Result<(), Self::Error> {
- if !v.is_finite() {
- return Err(Error::NumberInvalid);
- }
-
- self.emit_key("float")?;
- drop(write!(self.dst, "{}", v));
- if v % 1.0 == 0.0 {
- drop(write!(self.dst, ".0"));
- }
- if let State::Table { .. } = self.state {
- self.dst.push_str("\n");
- }
- Ok(())
+ serialize_float!(self, v)
}
fn serialize_f64(self, v: f64) -> Result<(), Self::Error> {
- if !v.is_finite() {
- return Err(Error::NumberInvalid);
- }
-
- self.emit_key("float")?;
- drop(write!(self.dst, "{}", v));
- if v % 1.0 == 0.0 {
- drop(write!(self.dst, ".0"));
- }
- if let State::Table { .. } = self.state {
- self.dst.push_str("\n");
- }
- Ok(())
+ serialize_float!(self, v)
}
fn serialize_char(self, v: char) -> Result<(), Self::Error> {