Propogate write! errors to ser::Error

std::fmt::Errors are now converted to ser::Errors via ser::Error::custom
This commit is contained in:
Nick Hackman 2019-08-20 12:59:28 -04:00
parent 9f25cfa17b
commit c18d474acf

View file

@ -25,8 +25,6 @@
//! # fn main() {} //! # fn main() {}
//! ``` //! ```
#![allow(unused_must_use)]
use std::cell::Cell; use std::cell::Cell;
use std::error; use std::error;
use std::fmt::{self, Write}; use std::fmt::{self, Write};
@ -424,7 +422,7 @@ impl<'a> Serializer<'a> {
fn display<T: fmt::Display>(&mut self, t: T, type_: &'static str) -> Result<(), Error> { fn display<T: fmt::Display>(&mut self, t: T, type_: &'static str) -> Result<(), Error> {
self.emit_key(type_)?; self.emit_key(type_)?;
write!(self.dst, "{}", t); write!(self.dst, "{}", t).map_err(ser::Error::custom)?;
if let State::Table { .. } = self.state { if let State::Table { .. } = self.state {
self.dst.push_str("\n"); self.dst.push_str("\n");
} }
@ -517,7 +515,7 @@ impl<'a> Serializer<'a> {
_ => false, _ => false,
}); });
if ok { if ok {
write!(self.dst, "{}", key); write!(self.dst, "{}", key).map_err(ser::Error::custom)?;
} else { } else {
self.emit_str(key, true)?; self.emit_str(key, true)?;
} }
@ -650,7 +648,7 @@ impl<'a> Serializer<'a> {
'\u{22}' => self.dst.push_str("\\\""), '\u{22}' => self.dst.push_str("\\\""),
'\u{5c}' => self.dst.push_str("\\\\"), '\u{5c}' => self.dst.push_str("\\\\"),
c if c < '\u{1f}' => { c if c < '\u{1f}' => {
write!(self.dst, "\\u{:04X}", ch as u32); write!(self.dst, "\\u{:04X}", ch as u32).map_err(ser::Error::custom)?;
} }
ch => self.dst.push(ch), ch => self.dst.push(ch),
} }
@ -754,15 +752,15 @@ macro_rules! serialize_float {
($this:expr, $v:expr) => {{ ($this:expr, $v:expr) => {{
$this.emit_key("float")?; $this.emit_key("float")?;
if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() { if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() {
write!($this.dst, "-"); write!($this.dst, "-").map_err(ser::Error::custom)?;
} }
if $v.is_nan() { if $v.is_nan() {
write!($this.dst, "nan"); write!($this.dst, "nan").map_err(ser::Error::custom)?;
} else { } else {
write!($this.dst, "{}", $v); write!($this.dst, "{}", $v).map_err(ser::Error::custom)?;
} }
if $v % 1.0 == 0.0 { if $v % 1.0 == 0.0 {
write!($this.dst, ".0"); write!($this.dst, ".0").map_err(ser::Error::custom)?;
} }
if let State::Table { .. } = $this.state { if let State::Table { .. } = $this.state {
$this.dst.push_str("\n"); $this.dst.push_str("\n");