//!
//! ```rust
//! # #![feature(rustc_private)]
-//! extern crate serialize;
-//! use serialize::json::{self, ToJson, Json};
+//! extern crate serialize as rustc_serialize;
+//! use rustc_serialize::json::{self, ToJson, Json};
//!
//! // A custom data structure
//! struct ComplexNum {
//! }
//!
//! // Only generate `RustcEncodable` trait implementation
-//! #[derive(Encodable)]
+//! #[derive(RustcEncodable)]
//! pub struct ComplexNumRecord {
//! uid: u8,
//! dsc: String,
//!
//! ```rust
//! # #![feature(rustc_private)]
-//! extern crate serialize;
+//! extern crate serialize as rustc_serialize;
//! use std::collections::BTreeMap;
-//! use serialize::json::{self, Json, ToJson};
+//! use rustc_serialize::json::{self, Json, ToJson};
//!
-//! // Only generate `Decodable` trait implementation
-//! #[derive(Decodable)]
+//! // Only generate `RustcDecodable` trait implementation
+//! #[derive(RustcDecodable)]
//! pub struct TestStruct {
//! data_int: u8,
//! data_str: String,
//! }
//! ```
-use self::JsonEvent::*;
+use self::DecoderError::*;
use self::ErrorCode::*;
+use self::InternalStackElement::*;
+use self::JsonEvent::*;
use self::ParserError::*;
-use self::DecoderError::*;
use self::ParserState::*;
-use self::InternalStackElement::*;
use std::borrow::Cow;
-use std::collections::{HashMap, BTreeMap};
-use std::io::prelude::*;
+use std::collections::{BTreeMap, HashMap};
use std::io;
+use std::io::prelude::*;
use std::mem::swap;
use std::num::FpCategory as Fp;
use std::ops::Index;
pub type Array = Vec<Json>;
pub type Object = BTreeMap<string::String, Json>;
-pub struct PrettyJson<'a> { inner: &'a Json }
+pub struct PrettyJson<'a> {
+ inner: &'a Json,
+}
-pub struct AsJson<'a, T> { inner: &'a T }
-pub struct AsPrettyJson<'a, T> { inner: &'a T, indent: Option<usize> }
+pub struct AsJson<'a, T> {
+ inner: &'a T,
+}
+pub struct AsPrettyJson<'a, T> {
+ inner: &'a T,
+ indent: Option<usize>,
+}
/// The errors that can arise while parsing a JSON stream.
#[derive(Clone, Copy, PartialEq, Debug)]
ExpectedError(string::String, string::String),
MissingFieldError(string::String),
UnknownVariantError(string::String),
- ApplicationError(string::String)
+ ApplicationError(string::String),
}
#[derive(Copy, Clone, Debug)]
pub fn decode<T: crate::Decodable>(s: &str) -> DecodeResult<T> {
let json = match from_str(s) {
Ok(x) => x,
- Err(e) => return Err(ParseError(e))
+ Err(e) => return Err(ParseError(e)),
};
let mut decoder = Decoder::new(json);
}
}
-impl std::error::Error for DecoderError {
- fn description(&self) -> &str { "decoder error" }
-}
+impl std::error::Error for DecoderError {}
impl fmt::Display for EncoderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
-impl std::error::Error for EncoderError {
- fn description(&self) -> &str { "encoder error" }
-}
+impl std::error::Error for EncoderError {}
impl From<fmt::Error> for EncoderError {
/// Converts a [`fmt::Error`] into `EncoderError`
///
/// This conversion does not allocate memory.
- fn from(err: fmt::Error) -> EncoderError { EncoderError::FmtError(err) }
+ fn from(err: fmt::Error) -> EncoderError {
+ EncoderError::FmtError(err)
+ }
}
pub type EncodeResult = Result<(), EncoderError>;
b'\x1e' => "\\u001e",
b'\x1f' => "\\u001f",
b'\x7f' => "\\u007f",
- _ => { continue; }
+ _ => {
+ continue;
+ }
};
if start < i {
/// A structure for implementing serialization to JSON.
pub struct Encoder<'a> {
- writer: &'a mut (dyn fmt::Write+'a),
+ writer: &'a mut (dyn fmt::Write + 'a),
is_emitting_map_key: bool,
}
/// Creates a new JSON encoder whose output will be written to the writer
/// specified.
pub fn new(writer: &'a mut dyn fmt::Write) -> Encoder<'a> {
- Encoder { writer, is_emitting_map_key: false, }
+ Encoder { writer, is_emitting_map_key: false }
}
}
macro_rules! emit_enquoted_if_mapkey {
- ($enc:ident,$e:expr) => ({
+ ($enc:ident,$e:expr) => {{
if $enc.is_emitting_map_key {
write!($enc.writer, "\"{}\"", $e)?;
} else {
write!($enc.writer, "{}", $e)?;
}
Ok(())
- })
+ }};
}
impl<'a> crate::Encoder for Encoder<'a> {
type Error = EncoderError;
fn emit_unit(&mut self) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "null")?;
Ok(())
}
- fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
+ fn emit_usize(&mut self, v: usize) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u128(&mut self, v: u128) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u64(&mut self, v: u64) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u32(&mut self, v: u32) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u16(&mut self, v: u16) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u8(&mut self, v: u8) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
- fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
+ fn emit_isize(&mut self, v: isize) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i128(&mut self, v: i128) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i64(&mut self, v: i64) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i32(&mut self, v: i32) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i16(&mut self, v: i16) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i8(&mut self, v: i8) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
fn emit_bool(&mut self, v: bool) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if v {
write!(self.writer, "true")?;
} else {
escape_str(self.writer, v)
}
- fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
+ fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
f(self)
}
- fn emit_enum_variant<F>(&mut self,
- name: &str,
- _id: usize,
- cnt: usize,
- f: F) -> EncodeResult where
+ fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
// enums are encoded as strings or objects
if cnt == 0 {
escape_str(self.writer, name)
} else {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "{{\"variant\":")?;
escape_str(self.writer, name)?;
write!(self.writer, ",\"fields\":[")?;
}
}
- fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx != 0 {
write!(self.writer, ",")?;
}
f(self)
}
- fn emit_enum_struct_variant<F>(&mut self,
- name: &str,
- id: usize,
- cnt: usize,
- f: F) -> EncodeResult where
+ fn emit_enum_struct_variant<F>(
+ &mut self,
+ name: &str,
+ id: usize,
+ cnt: usize,
+ f: F,
+ ) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_enum_variant(name, id, cnt, f)
}
- fn emit_enum_struct_variant_field<F>(&mut self,
- _: &str,
- idx: usize,
- f: F) -> EncodeResult where
+ fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_enum_variant_arg(idx, f)
}
- fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult where
+ fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "{{")?;
f(self)?;
write!(self.writer, "}}")?;
Ok(())
}
- fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
+ fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
- if idx != 0 { write!(self.writer, ",")?; }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
+ if idx != 0 {
+ write!(self.writer, ",")?;
+ }
escape_str(self.writer, name)?;
write!(self.writer, ":")?;
f(self)
}
- fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
+ fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq(len, f)
}
- fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq_elt(idx, f)
}
- fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult where
+ fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq(len, f)
}
- fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq_elt(idx, f)
}
- fn emit_option<F>(&mut self, f: F) -> EncodeResult where
+ fn emit_option<F>(&mut self, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
f(self)
}
fn emit_option_none(&mut self) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_unit()
}
- fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
+ fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
f(self)
}
- fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult where
+ fn emit_seq<F>(&mut self, _len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "[")?;
f(self)?;
write!(self.writer, "]")?;
Ok(())
}
- fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx != 0 {
write!(self.writer, ",")?;
}
f(self)
}
- fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult where
+ fn emit_map<F>(&mut self, _len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "{{")?;
f(self)?;
write!(self.writer, "}}")?;
Ok(())
}
- fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
- if idx != 0 { write!(self.writer, ",")? }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
+ if idx != 0 {
+ write!(self.writer, ",")?
+ }
self.is_emitting_map_key = true;
f(self)?;
self.is_emitting_map_key = false;
Ok(())
}
- fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
+ fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, ":")?;
f(self)
}
/// Another encoder for JSON, but prints out human-readable JSON instead of
/// compact data
pub struct PrettyEncoder<'a> {
- writer: &'a mut (dyn fmt::Write+'a),
+ writer: &'a mut (dyn fmt::Write + 'a),
curr_indent: usize,
indent: usize,
is_emitting_map_key: bool,
impl<'a> PrettyEncoder<'a> {
/// Creates a new encoder whose output will be written to the specified writer
pub fn new(writer: &'a mut dyn fmt::Write) -> PrettyEncoder<'a> {
- PrettyEncoder {
- writer,
- curr_indent: 0,
- indent: 2,
- is_emitting_map_key: false,
- }
+ PrettyEncoder { writer, curr_indent: 0, indent: 2, is_emitting_map_key: false }
}
/// Sets the number of spaces to indent for each level.
type Error = EncoderError;
fn emit_unit(&mut self) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, "null")?;
Ok(())
}
- fn emit_usize(&mut self, v: usize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u128(&mut self, v: u128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u64(&mut self, v: u64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u32(&mut self, v: u32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u16(&mut self, v: u16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_u8(&mut self, v: u8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
+ fn emit_usize(&mut self, v: usize) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u128(&mut self, v: u128) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u64(&mut self, v: u64) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u32(&mut self, v: u32) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u16(&mut self, v: u16) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_u8(&mut self, v: u8) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
- fn emit_isize(&mut self, v: isize) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i128(&mut self, v: i128) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i64(&mut self, v: i64) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i32(&mut self, v: i32) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i16(&mut self, v: i16) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
- fn emit_i8(&mut self, v: i8) -> EncodeResult { emit_enquoted_if_mapkey!(self, v) }
+ fn emit_isize(&mut self, v: isize) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i128(&mut self, v: i128) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i64(&mut self, v: i64) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i32(&mut self, v: i32) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i16(&mut self, v: i16) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
+ fn emit_i8(&mut self, v: i8) -> EncodeResult {
+ emit_enquoted_if_mapkey!(self, v)
+ }
fn emit_bool(&mut self, v: bool) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if v {
write!(self.writer, "true")?;
} else {
escape_str(self.writer, v)
}
- fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
+ fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
f(self)
}
- fn emit_enum_variant<F>(&mut self,
- name: &str,
- _id: usize,
- cnt: usize,
- f: F)
- -> EncodeResult where
+ fn emit_enum_variant<F>(&mut self, name: &str, _id: usize, cnt: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
if cnt == 0 {
escape_str(self.writer, name)
} else {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
writeln!(self.writer, "{{")?;
self.curr_indent += self.indent;
spaces(self.writer, self.curr_indent)?;
}
}
- fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_enum_variant_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx != 0 {
writeln!(self.writer, ",")?;
}
f(self)
}
- fn emit_enum_struct_variant<F>(&mut self,
- name: &str,
- id: usize,
- cnt: usize,
- f: F) -> EncodeResult where
+ fn emit_enum_struct_variant<F>(
+ &mut self,
+ name: &str,
+ id: usize,
+ cnt: usize,
+ f: F,
+ ) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_enum_variant(name, id, cnt, f)
}
- fn emit_enum_struct_variant_field<F>(&mut self,
- _: &str,
- idx: usize,
- f: F) -> EncodeResult where
+ fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_enum_variant_arg(idx, f)
}
-
- fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
+ fn emit_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if len == 0 {
write!(self.writer, "{{}}")?;
} else {
Ok(())
}
- fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult where
+ fn emit_struct_field<F>(&mut self, name: &str, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx == 0 {
writeln!(self.writer)?;
} else {
f(self)
}
- fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
+ fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq(len, f)
}
- fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq_elt(idx, f)
}
- fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
+ fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq(len, f)
}
- fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_seq_elt(idx, f)
}
- fn emit_option<F>(&mut self, f: F) -> EncodeResult where
+ fn emit_option<F>(&mut self, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
f(self)
}
fn emit_option_none(&mut self) -> EncodeResult {
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
self.emit_unit()
}
- fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
+ fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
f(self)
}
- fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
+ fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if len == 0 {
write!(self.writer, "[]")?;
} else {
Ok(())
}
- fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_seq_elt<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx == 0 {
writeln!(self.writer)?;
} else {
f(self)
}
- fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
+ fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if len == 0 {
write!(self.writer, "{{}}")?;
} else {
Ok(())
}
- fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult where
+ fn emit_map_elt_key<F>(&mut self, idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
if idx == 0 {
writeln!(self.writer)?;
} else {
Ok(())
}
- fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
+ fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
+ where
F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult,
{
- if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
+ if self.is_emitting_map_key {
+ return Err(EncoderError::BadHashmapKey);
+ }
write!(self.writer, ": ")?;
f(self)
}
PrettyJson { inner: self }
}
- /// If the Json value is an Object, returns the value associated with the provided key.
+ /// If the Json value is an Object, returns the value associated with the provided key.
/// Otherwise, returns None.
- pub fn find<'a>(&'a self, key: &str) -> Option<&'a Json>{
+ pub fn find(&self, key: &str) -> Option<&Json> {
match *self {
Json::Object(ref map) => map.get(key),
- _ => None
+ _ => None,
}
}
/// Attempts to get a nested Json Object for each key in `keys`.
/// If any key is found not to exist, `find_path` will return `None`.
/// Otherwise, it will return the Json value associated with the final key.
- pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{
+ pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json> {
let mut target = self;
for key in keys {
target = target.find(*key)?;
/// If the Json value is an Object, performs a depth-first search until
/// a value associated with the provided key is found. If no value is found
/// or the Json value is not an Object, returns `None`.
- pub fn search<'a>(&'a self, key: &str) -> Option<&'a Json> {
- match self {
- &Json::Object(ref map) => {
- match map.get(key) {
- Some(json_value) => Some(json_value),
- None => {
- for (_, v) in map {
- match v.search(key) {
- x if x.is_some() => return x,
- _ => ()
- }
+ pub fn search(&self, key: &str) -> Option<&Json> {
+ match *self {
+ Json::Object(ref map) => match map.get(key) {
+ Some(json_value) => Some(json_value),
+ None => {
+ for v in map.values() {
+ match v.search(key) {
+ x if x.is_some() => return x,
+ _ => (),
}
- None
}
+ None
}
},
- _ => None
+ _ => None,
}
}
pub fn as_object(&self) -> Option<&Object> {
match *self {
Json::Object(ref map) => Some(map),
- _ => None
+ _ => None,
}
}
pub fn as_array(&self) -> Option<&Array> {
match *self {
Json::Array(ref array) => Some(&*array),
- _ => None
+ _ => None,
}
}
pub fn as_string(&self) -> Option<&str> {
match *self {
Json::String(ref s) => Some(&s[..]),
- _ => None
+ _ => None,
}
}
match *self {
Json::I64(n) => Some(n),
Json::U64(n) => Some(n as i64),
- _ => None
+ _ => None,
}
}
match *self {
Json::I64(n) => Some(n as u64),
Json::U64(n) => Some(n),
- _ => None
+ _ => None,
}
}
Json::I64(n) => Some(n as f64),
Json::U64(n) => Some(n as f64),
Json::F64(n) => Some(n),
- _ => None
+ _ => None,
}
}
pub fn as_boolean(&self) -> Option<bool> {
match *self {
Json::Boolean(b) => Some(b),
- _ => None
+ _ => None,
}
}
pub fn as_null(&self) -> Option<()> {
match *self {
Json::Null => Some(()),
- _ => None
+ _ => None,
}
}
}
-impl<'a> Index<&'a str> for Json {
+impl<'a> Index<&'a str> for Json {
type Output = Json;
fn index(&self, idx: &'a str) -> &Json {
fn index(&self, idx: usize) -> &Json {
match *self {
Json::Array(ref v) => &v[idx],
- _ => panic!("can only index Json with usize if it is an array")
+ _ => panic!("can only index Json with usize if it is an array"),
}
}
}
}
/// Returns The number of elements in the Stack.
- pub fn len(&self) -> usize { self.stack.len() }
+ pub fn len(&self) -> usize {
+ self.stack.len()
+ }
/// Returns `true` if the stack is empty.
- pub fn is_empty(&self) -> bool { self.stack.is_empty() }
+ pub fn is_empty(&self) -> bool {
+ self.stack.is_empty()
+ }
/// Provides access to the StackElement at a given index.
/// lower indices are at the bottom of the stack while higher indices are
pub fn get(&self, idx: usize) -> StackElement<'_> {
match self.stack[idx] {
InternalIndex(i) => StackElement::Index(i),
- InternalKey(start, size) => {
- StackElement::Key(str::from_utf8(
- &self.str_buffer[start as usize .. start as usize + size as usize])
- .unwrap())
- }
+ InternalKey(start, size) => StackElement::Key(
+ str::from_utf8(&self.str_buffer[start as usize..start as usize + size as usize])
+ .unwrap(),
+ ),
}
}
/// Compares this stack with an array of StackElement<'_>s.
pub fn is_equal_to(&self, rhs: &[StackElement<'_>]) -> bool {
- if self.stack.len() != rhs.len() { return false; }
+ if self.stack.len() != rhs.len() {
+ return false;
+ }
for (i, r) in rhs.iter().enumerate() {
- if self.get(i) != *r { return false; }
+ if self.get(i) != *r {
+ return false;
+ }
}
true
}
/// Returns `true` if the bottom-most elements of this stack are the same as
/// the ones passed as parameter.
pub fn starts_with(&self, rhs: &[StackElement<'_>]) -> bool {
- if self.stack.len() < rhs.len() { return false; }
+ if self.stack.len() < rhs.len() {
+ return false;
+ }
for (i, r) in rhs.iter().enumerate() {
- if self.get(i) != *r { return false; }
+ if self.get(i) != *r {
+ return false;
+ }
}
true
}
/// Returns `true` if the top-most elements of this stack are the same as
/// the ones passed as parameter.
pub fn ends_with(&self, rhs: &[StackElement<'_>]) -> bool {
- if self.stack.len() < rhs.len() { return false; }
+ if self.stack.len() < rhs.len() {
+ return false;
+ }
let offset = self.stack.len() - rhs.len();
for (i, r) in rhs.iter().enumerate() {
- if self.get(i + offset) != *r { return false; }
+ if self.get(i + offset) != *r {
+ return false;
+ }
}
true
}
match self.stack.last() {
None => None,
Some(&InternalIndex(i)) => Some(StackElement::Index(i)),
- Some(&InternalKey(start, size)) => {
- Some(StackElement::Key(str::from_utf8(
- &self.str_buffer[start as usize .. (start+size) as usize]
- ).unwrap()))
- }
+ Some(&InternalKey(start, size)) => Some(StackElement::Key(
+ str::from_utf8(&self.str_buffer[start as usize..(start + size) as usize]).unwrap(),
+ )),
}
}
fn bump_index(&mut self) {
let len = self.stack.len();
let idx = match *self.stack.last().unwrap() {
- InternalIndex(i) => { i + 1 }
- _ => { panic!(); }
+ InternalIndex(i) => i + 1,
+ _ => {
+ panic!();
+ }
};
self.stack[len - 1] = InternalIndex(idx);
}
state: ParserState,
}
-impl<T: Iterator<Item=char>> Iterator for Parser<T> {
+impl<T: Iterator<Item = char>> Iterator for Parser<T> {
type Item = JsonEvent;
fn next(&mut self) -> Option<JsonEvent> {
}
}
-impl<T: Iterator<Item=char>> Parser<T> {
+impl<T: Iterator<Item = char>> Parser<T> {
/// Creates the JSON parser.
pub fn new(rdr: T) -> Parser<T> {
let mut p = Parser {
&self.stack
}
- fn eof(&self) -> bool { self.ch.is_none() }
- fn ch_or_null(&self) -> char { self.ch.unwrap_or('\x00') }
+ fn eof(&self) -> bool {
+ self.ch.is_none()
+ }
+ fn ch_or_null(&self) -> char {
+ self.ch.unwrap_or('\x00')
+ }
fn bump(&mut self) {
self.ch = self.rdr.next();
}
fn parse_whitespace(&mut self) {
- while self.ch_is(' ') ||
- self.ch_is('\n') ||
- self.ch_is('\t') ||
- self.ch_is('\r') { self.bump(); }
+ while self.ch_is(' ') || self.ch_is('\n') || self.ch_is('\t') || self.ch_is('\r') {
+ self.bump();
+ }
}
fn parse_number(&mut self) -> JsonEvent {
- let mut neg = false;
-
- if self.ch_is('-') {
+ let neg = if self.ch_is('-') {
self.bump();
- neg = true;
- }
+ true
+ } else {
+ false
+ };
let res = match self.parse_u64() {
Ok(res) => res,
- Err(e) => { return Error(e); }
+ Err(e) => {
+ return Error(e);
+ }
};
if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') {
if self.ch_is('.') {
res = match self.parse_decimal(res) {
Ok(res) => res,
- Err(e) => { return Error(e); }
+ Err(e) => {
+ return Error(e);
+ }
};
}
if self.ch_is('e') || self.ch_is('E') {
res = match self.parse_exponent(res) {
Ok(res) => res,
- Err(e) => { return Error(e); }
+ Err(e) => {
+ return Error(e);
+ }
};
}
self.bump();
// A leading '0' must be the only digit before the decimal point.
- if let '0' ..= '9' = self.ch_or_null() {
- return self.error(InvalidNumber)
+ if let '0'..='9' = self.ch_or_null() {
+ return self.error(InvalidNumber);
}
- },
- '1' ..= '9' => {
+ }
+ '1'..='9' => {
while !self.eof() {
match self.ch_or_null() {
- c @ '0' ..= '9' => {
+ c @ '0'..='9' => {
accum = accum.wrapping_mul(10);
accum = accum.wrapping_add((c as u64) - ('0' as u64));
// Detect overflow by comparing to the last value.
- if accum <= last_accum { return self.error(InvalidNumber); }
+ if accum <= last_accum {
+ return self.error(InvalidNumber);
+ }
self.bump();
}
// Make sure a digit follows the decimal place.
match self.ch_or_null() {
- '0' ..= '9' => (),
- _ => return self.error(InvalidNumber)
+ '0'..='9' => (),
+ _ => return self.error(InvalidNumber),
}
let mut dec = 1.0;
while !self.eof() {
match self.ch_or_null() {
- c @ '0' ..= '9' => {
+ c @ '0'..='9' => {
dec /= 10.0;
res += (((c as isize) - ('0' as isize)) as f64) * dec;
self.bump();
// Make sure a digit follows the exponent place.
match self.ch_or_null() {
- '0' ..= '9' => (),
- _ => return self.error(InvalidNumber)
+ '0'..='9' => (),
+ _ => return self.error(InvalidNumber),
}
while !self.eof() {
match self.ch_or_null() {
- c @ '0' ..= '9' => {
+ c @ '0'..='9' => {
exp *= 10;
exp += (c as usize) - ('0' as usize);
self.bump();
}
- _ => break
+ _ => break,
}
}
while i < 4 && !self.eof() {
self.bump();
n = match self.ch_or_null() {
- c @ '0' ..= '9' => n * 16 + ((c as u16) - ('0' as u16)),
+ c @ '0'..='9' => n * 16 + ((c as u16) - ('0' as u16)),
'a' | 'A' => n * 16 + 10,
'b' | 'B' => n * 16 + 11,
'c' | 'C' => n * 16 + 12,
'd' | 'D' => n * 16 + 13,
'e' | 'E' => n * 16 + 14,
'f' | 'F' => n * 16 + 15,
- _ => return self.error(InvalidEscape)
+ _ => return self.error(InvalidEscape),
};
i += 1;
'r' => res.push('\r'),
't' => res.push('\t'),
'u' => match self.decode_hex_escape()? {
- 0xDC00 ..= 0xDFFF => {
- return self.error(LoneLeadingSurrogateInHexEscape)
- }
+ 0xDC00..=0xDFFF => return self.error(LoneLeadingSurrogateInHexEscape),
// Non-BMP characters are encoded as a sequence of
// two hex escapes, representing UTF-16 surrogates.
- n1 @ 0xD800 ..= 0xDBFF => {
+ n1 @ 0xD800..=0xDBFF => {
match (self.next_char(), self.next_char()) {
(Some('\\'), Some('u')) => (),
_ => return self.error(UnexpectedEndOfHexEscape),
let n2 = self.decode_hex_escape()?;
if n2 < 0xDC00 || n2 > 0xDFFF {
- return self.error(LoneLeadingSurrogateInHexEscape)
+ return self.error(LoneLeadingSurrogateInHexEscape);
}
- let c = (u32::from(n1 - 0xD800) << 10 |
- u32::from(n2 - 0xDC00)) + 0x1_0000;
+ let c =
+ (u32::from(n1 - 0xD800) << 10 | u32::from(n2 - 0xDC00)) + 0x1_0000;
res.push(char::from_u32(c).unwrap());
}
Some('"') => {
self.bump();
return Ok(res);
- },
+ }
Some(c) => res.push(c),
- None => unreachable!()
+ None => unreachable!(),
}
}
}
}
fn parse_value(&mut self) -> JsonEvent {
- if self.eof() { return self.error_event(EOFWhileParsingValue); }
+ if self.eof() {
+ return self.error_event(EOFWhileParsingValue);
+ }
match self.ch_or_null() {
- 'n' => { self.parse_ident("ull", NullValue) }
- 't' => { self.parse_ident("rue", BooleanValue(true)) }
- 'f' => { self.parse_ident("alse", BooleanValue(false)) }
- '0' ..= '9' | '-' => self.parse_number(),
+ 'n' => self.parse_ident("ull", NullValue),
+ 't' => self.parse_ident("rue", BooleanValue(true)),
+ 'f' => self.parse_ident("alse", BooleanValue(false)),
+ '0'..='9' | '-' => self.parse_number(),
'"' => match self.parse_str() {
Ok(s) => StringValue(s),
Err(e) => Error(e),
self.bump();
ObjectStart
}
- _ => { self.error_event(InvalidSyntax) }
+ _ => self.error_event(InvalidSyntax),
}
}
token: Option<JsonEvent>,
}
-impl<T: Iterator<Item=char>> Builder<T> {
+impl<T: Iterator<Item = char>> Builder<T> {
/// Creates a JSON Builder.
pub fn new(src: T) -> Builder<T> {
- Builder { parser: Parser::new(src), token: None, }
+ Builder { parser: Parser::new(src), token: None }
}
// Decode a Json value from a Parser.
self.bump();
match self.token {
None => {}
- Some(Error(ref e)) => { return Err(e.clone()); }
- ref tok => { panic!("unexpected token {:?}", tok.clone()); }
+ Some(Error(ref e)) => {
+ return Err(e.clone());
+ }
+ ref tok => {
+ panic!("unexpected token {:?}", tok.clone());
+ }
}
result
}
}
match self.build_value() {
Ok(v) => values.push(v),
- Err(e) => { return Err(e) }
+ Err(e) => return Err(e),
}
self.bump();
}
loop {
match self.token {
- Some(ObjectEnd) => { return Ok(Json::Object(values)); }
- Some(Error(ref e)) => { return Err(e.clone()); }
- None => { break; }
+ Some(ObjectEnd) => {
+ return Ok(Json::Object(values));
+ }
+ Some(Error(ref e)) => {
+ return Err(e.clone());
+ }
+ None => {
+ break;
+ }
_ => {}
}
let key = match self.parser.stack().top() {
- Some(StackElement::Key(k)) => { k.to_owned() }
- _ => { panic!("invalid state"); }
+ Some(StackElement::Key(k)) => k.to_owned(),
+ _ => {
+ panic!("invalid state");
+ }
};
match self.build_value() {
- Ok(value) => { values.insert(key, value); }
- Err(e) => { return Err(e); }
+ Ok(value) => {
+ values.insert(key, value);
+ }
+ Err(e) => {
+ return Err(e);
+ }
}
self.bump();
}
pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
let mut contents = Vec::new();
match rdr.read_to_end(&mut contents) {
- Ok(c) => c,
- Err(e) => return Err(io_error_to_error(e))
+ Ok(c) => c,
+ Err(e) => return Err(io_error_to_error(e)),
};
let s = match str::from_utf8(&contents).ok() {
Some(s) => s,
- _ => return Err(SyntaxError(NotUtf8, 0, 0))
+ _ => return Err(SyntaxError(NotUtf8, 0, 0)),
};
let mut builder = Builder::new(s.chars());
builder.build()
}
macro_rules! expect {
- ($e:expr, Null) => ({
+ ($e:expr, Null) => {{
match $e {
Json::Null => Ok(()),
- other => Err(ExpectedError("Null".to_owned(),
- other.to_string()))
+ other => Err(ExpectedError("Null".to_owned(), other.to_string())),
}
- });
- ($e:expr, $t:ident) => ({
+ }};
+ ($e:expr, $t:ident) => {{
match $e {
Json::$t(v) => Ok(v),
- other => {
- Err(ExpectedError(stringify!($t).to_owned(),
- other.to_string()))
- }
+ other => Err(ExpectedError(stringify!($t).to_owned(), other.to_string())),
}
- })
+ }};
}
macro_rules! read_primitive {
read_primitive! { read_i64, i64 }
read_primitive! { read_i128, i128 }
- fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) }
+ fn read_f32(&mut self) -> DecodeResult<f32> {
+ self.read_f64().map(|x| x as f32)
+ }
fn read_f64(&mut self) -> DecodeResult<f64> {
match self.pop() {
Some(f) => Ok(f),
None => Err(ExpectedError("Number".to_owned(), s)),
}
- },
+ }
Json::Null => Ok(f64::NAN),
- value => Err(ExpectedError("Number".to_owned(), value.to_string()))
+ value => Err(ExpectedError("Number".to_owned(), value.to_string())),
}
}
let s = self.read_str()?;
{
let mut it = s.chars();
- match (it.next(), it.next()) {
+ if let (Some(c), None) = (it.next(), it.next()) {
// exactly one character
- (Some(c), None) => return Ok(c),
- _ => ()
+ return Ok(c);
}
}
Err(ExpectedError("single character string".to_owned(), s.to_string()))
expect!(self.pop(), String).map(Cow::Owned)
}
- fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where
+ fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
- fn read_enum_variant<T, F>(&mut self, names: &[&str],
- mut f: F) -> DecodeResult<T>
- where F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
+ fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodeResult<T>
+ where
+ F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
{
let name = match self.pop() {
Json::String(s) => s,
Json::Object(mut o) => {
let n = match o.remove(&"variant".to_owned()) {
Some(Json::String(s)) => s,
- Some(val) => {
- return Err(ExpectedError("String".to_owned(), val.to_string()))
- }
- None => {
- return Err(MissingFieldError("variant".to_owned()))
- }
+ Some(val) => return Err(ExpectedError("String".to_owned(), val.to_string())),
+ None => return Err(MissingFieldError("variant".to_owned())),
};
match o.remove(&"fields".to_string()) {
Some(Json::Array(l)) => {
self.stack.extend(l.into_iter().rev());
- },
- Some(val) => {
- return Err(ExpectedError("Array".to_owned(), val.to_string()))
- }
- None => {
- return Err(MissingFieldError("fields".to_owned()))
}
+ Some(val) => return Err(ExpectedError("Array".to_owned(), val.to_string())),
+ None => return Err(MissingFieldError("fields".to_owned())),
}
n
}
- json => {
- return Err(ExpectedError("String or Object".to_owned(), json.to_string()))
- }
+ json => return Err(ExpectedError("String or Object".to_owned(), json.to_string())),
};
let idx = match names.iter().position(|n| *n == &name[..]) {
Some(idx) => idx,
- None => return Err(UnknownVariantError(name))
+ None => return Err(UnknownVariantError(name)),
};
f(self, idx)
}
- fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
+ fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
- fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T> where
+ fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T>
+ where
F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
{
self.read_enum_variant(names, f)
}
-
- fn read_enum_struct_variant_field<T, F>(&mut self,
- _name: &str,
- idx: usize,
- f: F)
- -> DecodeResult<T> where
+ fn read_enum_struct_variant_field<T, F>(
+ &mut self,
+ _name: &str,
+ idx: usize,
+ f: F,
+ ) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_enum_variant_arg(idx, f)
}
- fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T> where
+ fn read_struct<T, F>(&mut self, _name: &str, _len: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
let value = f(self)?;
Ok(value)
}
- fn read_struct_field<T, F>(&mut self,
- name: &str,
- _idx: usize,
- f: F)
- -> DecodeResult<T> where
+ fn read_struct_field<T, F>(&mut self, name: &str, _idx: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
let mut obj = expect!(self.pop(), Object)?;
Ok(x) => x,
Err(_) => return Err(MissingFieldError(name.to_string())),
}
- },
+ }
Some(json) => {
self.stack.push(json);
f(self)?
Ok(value)
}
- fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T> where
+ fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_seq(move |d, len| {
})
}
- fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
+ fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_seq_elt(idx, f)
}
- fn read_tuple_struct<T, F>(&mut self,
- _name: &str,
- len: usize,
- f: F)
- -> DecodeResult<T> where
+ fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_tuple(len, f)
}
- fn read_tuple_struct_arg<T, F>(&mut self,
- idx: usize,
- f: F)
- -> DecodeResult<T> where
+ fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
self.read_tuple_arg(idx, f)
}
- fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
+ fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
+ where
F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
{
match self.pop() {
Json::Null => f(self, false),
- value => { self.stack.push(value); f(self, true) }
+ value => {
+ self.stack.push(value);
+ f(self, true)
+ }
}
}
- fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where
+ fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
{
let array = expect!(self.pop(), Array)?;
f(self, len)
}
- fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
+ fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
- fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where
+ fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
+ where
F: FnOnce(&mut Decoder, usize) -> DecodeResult<T>,
{
let obj = expect!(self.pop(), Object)?;
f(self, len)
}
- fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
- F: FnOnce(&mut Decoder) -> DecodeResult<T>,
+ fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
+ where
+ F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
- fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T> where
- F: FnOnce(&mut Decoder) -> DecodeResult<T>,
+ fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> DecodeResult<T>
+ where
+ F: FnOnce(&mut Decoder) -> DecodeResult<T>,
{
f(self)
}
to_json_impl_u64! { usize, u8, u16, u32, u64 }
impl ToJson for Json {
- fn to_json(&self) -> Json { self.clone() }
+ fn to_json(&self) -> Json {
+ self.clone()
+ }
}
impl ToJson for f32 {
- fn to_json(&self) -> Json { f64::from(*self).to_json() }
+ fn to_json(&self) -> Json {
+ f64::from(*self).to_json()
+ }
}
impl ToJson for f64 {
fn to_json(&self) -> Json {
match self.classify() {
Fp::Nan | Fp::Infinite => Json::Null,
- _ => Json::F64(*self)
+ _ => Json::F64(*self),
}
}
}
impl ToJson for () {
- fn to_json(&self) -> Json { Json::Null }
+ fn to_json(&self) -> Json {
+ Json::Null
+ }
}
impl ToJson for bool {
- fn to_json(&self) -> Json { Json::Boolean(*self) }
+ fn to_json(&self) -> Json {
+ Json::Boolean(*self)
+ }
}
impl ToJson for str {
- fn to_json(&self) -> Json { Json::String(self.to_string()) }
+ fn to_json(&self) -> Json {
+ Json::String(self.to_string())
+ }
}
impl ToJson for string::String {
- fn to_json(&self) -> Json { Json::String((*self).clone()) }
+ fn to_json(&self) -> Json {
+ Json::String((*self).clone())
+ }
}
macro_rules! tuple_impl {
}
}
-tuple_impl!{A}
-tuple_impl!{A, B}
-tuple_impl!{A, B, C}
-tuple_impl!{A, B, C, D}
-tuple_impl!{A, B, C, D, E}
-tuple_impl!{A, B, C, D, E, F}
-tuple_impl!{A, B, C, D, E, F, G}
-tuple_impl!{A, B, C, D, E, F, G, H}
-tuple_impl!{A, B, C, D, E, F, G, H, I}
-tuple_impl!{A, B, C, D, E, F, G, H, I, J}
-tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
-tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
+tuple_impl! {A}
+tuple_impl! {A, B}
+tuple_impl! {A, B, C}
+tuple_impl! {A, B, C, D}
+tuple_impl! {A, B, C, D, E}
+tuple_impl! {A, B, C, D, E, F}
+tuple_impl! {A, B, C, D, E, F, G}
+tuple_impl! {A, B, C, D, E, F, G, H}
+tuple_impl! {A, B, C, D, E, F, G, H, I}
+tuple_impl! {A, B, C, D, E, F, G, H, I, J}
+tuple_impl! {A, B, C, D, E, F, G, H, I, J, K}
+tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}
impl<A: ToJson> ToJson for [A] {
- fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
+ fn to_json(&self) -> Json {
+ Json::Array(self.iter().map(|elt| elt.to_json()).collect())
+ }
}
impl<A: ToJson> ToJson for Vec<A> {
- fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
+ fn to_json(&self) -> Json {
+ Json::Array(self.iter().map(|elt| elt.to_json()).collect())
+ }
}
impl<A: ToJson> ToJson for BTreeMap<string::String, A> {
}
}
-impl<A:ToJson> ToJson for Option<A> {
+impl<A: ToJson> ToJson for Option<A> {
fn to_json(&self) -> Json {
match *self {
None => Json::Null,
- Some(ref value) => value.to_json()
+ Some(ref value) => value.to_json(),
}
}
}
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.inner.write_str(s) {
Ok(_) => Ok(()),
- Err(_) => Err(fmt::Error)
+ Err(_) => Err(fmt::Error),
}
}
}
let mut encoder = Encoder::new(&mut shim);
match self.encode(&mut encoder) {
Ok(_) => Ok(()),
- Err(_) => Err(fmt::Error)
+ Err(_) => Err(fmt::Error),
}
}
}
let mut encoder = PrettyEncoder::new(&mut shim);
match self.inner.encode(&mut encoder) {
Ok(_) => Ok(()),
- Err(_) => Err(fmt::Error)
+ Err(_) => Err(fmt::Error),
}
}
}
let mut encoder = Encoder::new(&mut shim);
match self.inner.encode(&mut encoder) {
Ok(_) => Ok(()),
- Err(_) => Err(fmt::Error)
+ Err(_) => Err(fmt::Error),
}
}
}
}
match self.inner.encode(&mut encoder) {
Ok(_) => Ok(()),
- Err(_) => Err(fmt::Error)
+ Err(_) => Err(fmt::Error),
}
}
}