Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jul 13, 2016
1 parent d01e055 commit 01d8fc4
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 137 deletions.
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn_arg_indent = "Tabbed"
fn_args_layout = "Block"
max_width = 80
reorder_imports = true
struct_lit_multiline_style = "ForceMulti"
where_trailing_comma = true
74 changes: 42 additions & 32 deletions yaml/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> de::SeqVisitor for SeqVisitor<'a> {
None => Ok(None),
Some(ref t) => {
Deserialize::deserialize(&mut Deserializer::new(t)).map(Some)
},
}
}
}

Expand All @@ -87,7 +87,7 @@ impl<'a> MapVisitor<'a> {

impl<'a> de::MapVisitor for MapVisitor<'a> {
type Error = Error;

fn visit_key<K>(&mut self) -> Result<Option<K>>
where K: Deserialize,
{
Expand All @@ -96,7 +96,7 @@ impl<'a> de::MapVisitor for MapVisitor<'a> {
Some((ref k, ref v)) => {
self.v = Some(v);
Deserialize::deserialize(&mut Deserializer::new(k)).map(Some)
},
}
}
}

Expand Down Expand Up @@ -128,8 +128,10 @@ impl<'a> de::MapVisitor for MapVisitor<'a> {
Err(de::Error::missing_field(self.0))
}

fn deserialize_option<V>(&mut self,
mut visitor: V) -> Result<V::Value>
fn deserialize_option<V>(
&mut self,
mut visitor: V
) -> Result<V::Value>
where V: de::Visitor,
{
visitor.visit_none()
Expand Down Expand Up @@ -157,11 +159,11 @@ impl<'a> VariantVisitor<'a> {
}
}

impl <'a> de::VariantVisitor for VariantVisitor<'a> {
impl<'a> de::VariantVisitor for VariantVisitor<'a> {
type Error = Error;

fn visit_variant<V>(&mut self) -> Result<V>
where V: Deserialize
where V: Deserialize,
{
Deserialize::deserialize(&mut Deserializer::new(self.variant))
}
Expand All @@ -176,20 +178,22 @@ impl <'a> de::VariantVisitor for VariantVisitor<'a> {
Deserialize::deserialize(&mut Deserializer::new(self.content))
}

fn visit_tuple<V>(&mut self,
_len: usize,
visitor: V) -> Result<V::Value>
fn visit_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::deserialize(&mut Deserializer::new(self.content), visitor)
de::Deserializer::deserialize(&mut Deserializer::new(self.content),
visitor)
}

fn visit_struct<V>(&mut self,
_fields: &'static [&'static str],
visitor: V) -> Result<V::Value>
fn visit_struct<V>(
&mut self,
_fields: &'static [&'static str],
visitor: V
) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::deserialize(&mut Deserializer::new(self.content), visitor)
de::Deserializer::deserialize(&mut Deserializer::new(self.content),
visitor)
}
}

Expand All @@ -201,7 +205,8 @@ impl<'a> de::Deserializer for Deserializer<'a> {
{
match *self.doc {
Yaml::Integer(i) => visitor.visit_i64(i),
Yaml::Real(ref s) | Yaml::String(ref s) => visitor.visit_str(s),
Yaml::Real(ref s) |
Yaml::String(ref s) => visitor.visit_str(s),
Yaml::Boolean(b) => visitor.visit_bool(b),
Yaml::Array(ref seq) => visitor.visit_seq(SeqVisitor::new(seq)),
Yaml::Hash(ref hash) => visitor.visit_map(MapVisitor::new(hash)),
Expand All @@ -213,7 +218,7 @@ impl<'a> de::Deserializer for Deserializer<'a> {
// conversion is invalid. Both of these are unexpected in our
// usage.
panic!("bad value")
},
}
}
}

Expand All @@ -228,9 +233,11 @@ impl<'a> de::Deserializer for Deserializer<'a> {
}

/// Parses a newtype struct as the underlying value.
fn deserialize_newtype_struct<V>(&mut self,
_name: &str,
mut visitor: V) -> Result<V::Value>
fn deserialize_newtype_struct<V>(
&mut self,
_name: &str,
mut visitor: V
) -> Result<V::Value>
where V: de::Visitor,
{
visitor.visit_newtype_struct(self)
Expand All @@ -239,10 +246,12 @@ impl<'a> de::Deserializer for Deserializer<'a> {
/// Parses an enum as a single key:value pair where the key identifies the
/// variant and the value gives the content. A String will also parse correctly
/// to a unit enum value.
fn deserialize_enum<V>(&mut self,
name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> Result<V::Value>
fn deserialize_enum<V>(
&mut self,
name: &str,
_variants: &'static [&'static str],
mut visitor: V
) -> Result<V::Value>
where V: de::EnumVisitor,
{
match *self.doc {
Expand All @@ -252,34 +261,35 @@ impl<'a> de::Deserializer for Deserializer<'a> {
let (variant, content) = entry;
visitor.visit(VariantVisitor::new(variant, content))
} else {
Err(Error::VariantMapWrongSize(String::from(name), hash.len()))
Err(Error::VariantMapWrongSize(String::from(name),
hash.len()))
}
},
}
ref ystr @ Yaml::String(_) => {
visitor.visit(VariantVisitor::new(ystr, &Yaml::Null))
},
_ => Err(Error::VariantNotAMapOrString(String::from(name)))
}
_ => Err(Error::VariantNotAMapOrString(String::from(name))),
}
}
}

/// Decodes a YAML value from a `&str`.
pub fn from_str<T>(s: &str) -> Result<T>
where T: Deserialize
where T: Deserialize,
{
let docs = try!(YamlLoader::load_from_str(s));
match docs.len() {
0 => Err(Error::EndOfStream),
1 => {
let doc = &docs[0];
Deserialize::deserialize(&mut Deserializer::new(doc))
},
}
n => Err(Error::TooManyDocuments(n)),
}
}

pub fn from_iter<I, T>(iter: I) -> Result<T>
where I: Iterator<Item=io::Result<u8>>,
where I: Iterator<Item = io::Result<u8>>,
T: Deserialize,
{
let bytes: Vec<u8> = try!(iter.collect());
Expand All @@ -294,7 +304,7 @@ pub fn from_reader<R, T>(rdr: R) -> Result<T>
}

pub fn from_slice<T>(v: &[u8]) -> Result<T>
where T: Deserialize
where T: Deserialize,
{
from_iter(v.iter().map(|byte| Ok(*byte)))
}
66 changes: 36 additions & 30 deletions yaml/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::string;

use yaml_rust::{emitter, scanner};

use serde::{ser, de};
use serde::{de, ser};

/// This type represents all possible errors that can occur when serializing or
/// deserializing a value using YAML.
Expand All @@ -33,7 +33,7 @@ pub enum Error {
AliasUnsupported,
TooManyDocuments(usize),
VariantMapWrongSize(String, usize),
VariantNotAMapOrString(String)
VariantNotAMapOrString(String),
}

impl error::Error for Error {
Expand All @@ -47,12 +47,15 @@ impl error::Error for Error {
Error::Utf8(ref err) => err.description(),
Error::FromUtf8(ref err) => err.description(),
Error::AliasUnsupported => "YAML aliases are not supported",
Error::TooManyDocuments(_) =>
"expected a single YAML document but found multiple",
Error::VariantMapWrongSize(..) =>
"expected a YAML map of size 1 while parsing variant",
Error::VariantNotAMapOrString(_) =>
"expected a YAML map or string while parsing variant",
Error::TooManyDocuments(_) => {
"expected a single YAML document but found multiple"
}
Error::VariantMapWrongSize(..) => {
"expected a YAML map of size 1 while parsing variant"
}
Error::VariantNotAMapOrString(_) => {
"expected a YAML map or string while parsing variant"
}
}
}

Expand All @@ -69,28 +72,31 @@ impl error::Error for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Custom(ref msg) =>
write!(f, "{}", msg),
Error::EndOfStream =>
write!(f, "EOF while parsing a value"),
Error::Emit(ref err) =>
write!(f, "{:?}", err),
Error::Scan(ref err) =>
err.fmt(f),
Error::Io(ref err) =>
err.fmt(f),
Error::Utf8(ref err) =>
err.fmt(f),
Error::FromUtf8(ref err) =>
err.fmt(f),
Error::AliasUnsupported =>
write!(f, "YAML aliases are not supported"),
Error::TooManyDocuments(n) =>
write!(f, "Expected a single YAML document but found {}", n),
Error::VariantMapWrongSize(ref variant, size) =>
write!(f, "Expected a YAML map of size 1 while parsing variant {} but was size {}", variant, size),
Error::VariantNotAMapOrString(ref variant) =>
write!(f, "Expected a YAML map or string while parsing variant {}", variant),
Error::Custom(ref msg) => write!(f, "{}", msg),
Error::EndOfStream => write!(f, "EOF while parsing a value"),
Error::Emit(ref err) => write!(f, "{:?}", err),
Error::Scan(ref err) => err.fmt(f),
Error::Io(ref err) => err.fmt(f),
Error::Utf8(ref err) => err.fmt(f),
Error::FromUtf8(ref err) => err.fmt(f),
Error::AliasUnsupported => {
write!(f, "YAML aliases are not supported")
}
Error::TooManyDocuments(n) => {
write!(f, "Expected a single YAML document but found {}", n)
}
Error::VariantMapWrongSize(ref variant, size) => {
write!(f,
"Expected a YAML map of size 1 while parsing variant \
{} but was size {}",
variant,
size)
}
Error::VariantNotAMapOrString(ref variant) => {
write!(f,
"Expected a YAML map or string while parsing variant {}",
variant)
}
}
}
}
Expand Down
26 changes: 4 additions & 22 deletions yaml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,10 @@
extern crate serde;
extern crate yaml_rust;

pub use self::de::{
Deserializer,
from_str,
from_iter,
from_reader,
from_slice,
};
pub use self::ser::{
Serializer,
to_writer,
to_vec,
to_string,
};
pub use self::value::{
Value,
to_value,
from_value,
};
pub use self::error::{
Error,
Result,
};
pub use self::de::{Deserializer, from_iter, from_reader, from_slice, from_str};
pub use self::ser::{Serializer, to_string, to_vec, to_writer};
pub use self::value::{Value, from_value, to_value};
pub use self::error::{Error, Result};

pub mod de;
pub mod ser;
Expand Down
Loading

0 comments on commit 01d8fc4

Please sign in to comment.