Merge pull request #101 from leodasvacas/clippy-run

Clippy run and implement Default for State and Enconder
This commit is contained in:
Alex Crichton 2016-06-05 07:46:17 +02:00
commit fb8050d2d5
6 changed files with 42 additions and 37 deletions

View file

@ -126,11 +126,11 @@ impl Decoder {
fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder { fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder {
Decoder { Decoder {
toml: toml, toml: toml,
cur_field: if field.len() == 0 { cur_field: if field.is_empty() {
self.cur_field.clone() self.cur_field.clone()
} else { } else {
match self.cur_field { match self.cur_field {
None => Some(format!("{}", field)), None => Some(field.to_string()),
Some(ref s) => Some(format!("{}.{}", s, field)) Some(ref s) => Some(format!("{}.{}", s, field))
} }
}, },
@ -172,7 +172,7 @@ impl fmt::Display for DecodeError {
ExpectedType(expected, found) => { ExpectedType(expected, found) => {
fn humanize(s: &str) -> String { fn humanize(s: &str) -> String {
if s == "section" { if s == "section" {
format!("a section") "a section".to_string()
} else { } else {
format!("a value of type `{}`", s) format!("a value of type `{}`", s)
} }

View file

@ -171,7 +171,7 @@ impl rustc_serialize::Decoder for Decoder {
-> Result<T, DecodeError> -> Result<T, DecodeError>
where F: FnOnce(&mut Decoder) -> Result<T, DecodeError> where F: FnOnce(&mut Decoder) -> Result<T, DecodeError>
{ {
let field = format!("{}", f_name); let field = f_name.to_string();
let toml = match self.toml { let toml = match self.toml {
Some(Value::Table(ref mut table)) => { Some(Value::Table(ref mut table)) => {
table.remove(&field) table.remove(&field)
@ -324,7 +324,7 @@ impl rustc_serialize::Decoder for Decoder {
fn error(&mut self, err: &str) -> DecodeError { fn error(&mut self, err: &str) -> DecodeError {
DecodeError { DecodeError {
field: self.cur_field.clone(), field: self.cur_field.clone(),
kind: ApplicationError(format!("{}", err)) kind: ApplicationError(err.to_string())
} }
} }
} }

View file

@ -35,6 +35,7 @@ use {Value, Table};
/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4))) /// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4)))
/// # } /// # }
/// ``` /// ```
#[derive(Default)]
pub struct Encoder { pub struct Encoder {
/// Output TOML that is emitted. The current version of this encoder forces /// Output TOML that is emitted. The current version of this encoder forces
/// the top-level representation of a structure to be a table. /// the top-level representation of a structure to be a table.
@ -73,6 +74,10 @@ enum State {
NextMapKey, NextMapKey,
} }
impl Default for State {
fn default() -> State { State::Start }
}
impl Encoder { impl Encoder {
/// Constructs a new encoder which will emit to the given output stream. /// Constructs a new encoder which will emit to the given output stream.
pub fn new() -> Encoder { pub fn new() -> Encoder {

View file

@ -47,10 +47,10 @@ impl rustc_serialize::Encoder for Encoder {
self.emit_value(Value::Float(v)) self.emit_value(Value::Float(v))
} }
fn emit_char(&mut self, v: char) -> Result<(), Error> { fn emit_char(&mut self, v: char) -> Result<(), Error> {
self.emit_str(&*format!("{}", v)) self.emit_str(&v.to_string())
} }
fn emit_str(&mut self, v: &str) -> Result<(), Error> { fn emit_str(&mut self, v: &str) -> Result<(), Error> {
self.emit_value(Value::String(format!("{}", v))) self.emit_value(Value::String(v.to_string()))
} }
fn emit_enum<F>(&mut self, _name: &str, f: F) fn emit_enum<F>(&mut self, _name: &str, f: F)
-> Result<(), Error> -> Result<(), Error>
@ -98,7 +98,7 @@ impl rustc_serialize::Encoder for Encoder {
where F: FnOnce(&mut Encoder) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>
{ {
let old = mem::replace(&mut self.state, let old = mem::replace(&mut self.state,
State::NextKey(format!("{}", f_name))); State::NextKey(f_name.to_string()));
try!(f(self)); try!(f(self));
if self.state != State::Start { if self.state != State::Start {
return Err(NoValue) return Err(NoValue)
@ -453,7 +453,7 @@ mod tests {
match a { match a {
Ok(..) => panic!("should not have decoded"), Ok(..) => panic!("should not have decoded"),
Err(e) => { Err(e) => {
assert_eq!(format!("{}", e), assert_eq!(e.to_string(),
"expected a value of type `integer`, but \ "expected a value of type `integer`, but \
found a value of type `float` for the key `bar`"); found a value of type `float` for the key `bar`");
} }
@ -471,7 +471,7 @@ mod tests {
match a { match a {
Ok(..) => panic!("should not have decoded"), Ok(..) => panic!("should not have decoded"),
Err(e) => { Err(e) => {
assert_eq!(format!("{}", e), assert_eq!(e.to_string(),
"expected a value of type `integer` for the key `bar`"); "expected a value of type `integer` for the key `bar`");
} }
} }

View file

@ -72,10 +72,10 @@ pub enum Value {
Table(Table), Table(Table),
} }
/// Type representing a TOML array, payload of the Value::Array variant /// Type representing a TOML array, payload of the `Value::Array` variant
pub type Array = Vec<Value>; pub type Array = Vec<Value>;
/// Type representing a TOML table, payload of the Value::Table variant /// Type representing a TOML table, payload of the `Value::Table` variant
pub type Table = BTreeMap<String, Value>; pub type Table = BTreeMap<String, Value>;
impl Value { impl Value {
@ -186,7 +186,7 @@ impl Value {
None => return None, None => return None,
}; };
let mut cur_value = self; let mut cur_value = self;
if path.len() == 0 { if path.is_empty() {
return Some(cur_value) return Some(cur_value)
} }
@ -247,7 +247,7 @@ impl Value {
}; };
let mut cur = self; let mut cur = self;
if path.len() == 0 { if path.is_empty() {
return Some(cur) return Some(cur)
} }

View file

@ -1,5 +1,5 @@
use std::char; use std::char;
use std::collections::BTreeMap; use std::collections::btree_map::{BTreeMap, Entry};
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::str; use std::str;
@ -294,7 +294,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: start, hi: start,
desc: format!("expected a newline after table definition"), desc: "expected a newline after table definition".to_string(),
}); });
return None return None
} }
@ -306,8 +306,8 @@ impl<'a> Parser<'a> {
} else { } else {
self.insert_table(&mut ret, &keys, table, start) self.insert_table(&mut ret, &keys, table, start)
} }
} else { } else if !self.values(&mut ret) {
if !self.values(&mut ret) { return None } return None
} }
} }
if !self.errors.is_empty() { if !self.errors.is_empty() {
@ -324,7 +324,7 @@ impl<'a> Parser<'a> {
/// Parse a path into a vector of paths /// Parse a path into a vector of paths
pub fn lookup(&mut self) -> Option<Vec<String>> { pub fn lookup(&mut self) -> Option<Vec<String>> {
if self.input.len() == 0 { if self.input.is_empty() {
return Some(vec![]); return Some(vec![]);
} }
let mut keys = Vec::new(); let mut keys = Vec::new();
@ -367,7 +367,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: start, hi: start,
desc: format!("expected a key but found an empty string"), desc: "expected a key but found an empty string".to_string(),
}); });
None None
} }
@ -408,7 +408,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: key_lo, lo: key_lo,
hi: end, hi: end,
desc: format!("expected a newline after a key"), desc: "expected a newline after a key".to_string(),
}); });
return false return false
} }
@ -443,7 +443,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: lo, lo: lo,
hi: hi, hi: hi,
desc: format!("expected a value"), desc: "expected a value".to_string(),
}); });
None None
} }
@ -503,7 +503,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: self.input.len(), hi: self.input.len(),
desc: format!("unterminated string literal"), desc: "unterminated string literal".to_string(),
}); });
return None return None
} }
@ -569,7 +569,7 @@ impl<'a> Parser<'a> {
me.errors.push(ParserError { me.errors.push(ParserError {
lo: pos, lo: pos,
hi: pos + 1, hi: pos + 1,
desc: format!("unterminated escape sequence"), desc: "unterminated escape sequence".to_string(),
}); });
None None
} }
@ -603,7 +603,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: next, hi: next,
desc: format!("literal strings cannot contain newlines"), desc: "literal strings cannot contain newlines".to_string(),
}); });
return None return None
} }
@ -620,7 +620,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: self.input.len(), hi: self.input.len(),
desc: format!("unterminated string literal"), desc: "unterminated string literal".to_string(),
}); });
return None return None
} }
@ -647,8 +647,8 @@ impl<'a> Parser<'a> {
let input = &self.input[start..end]; let input = &self.input[start..end];
let ret = if decimal.is_none() && let ret = if decimal.is_none() &&
exponent.is_none() && exponent.is_none() &&
!input.starts_with("+") && !input.starts_with('+') &&
!input.starts_with("-") && !input.starts_with('-') &&
start + 4 == end && start + 4 == end &&
self.eat('-') { self.eat('-') {
self.datetime(start) self.datetime(start)
@ -670,7 +670,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: end, hi: end,
desc: format!("invalid numeric literal"), desc: "invalid numeric literal".to_string(),
}); });
} }
ret ret
@ -693,7 +693,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: pos, hi: pos,
desc: format!("leading zeroes are not allowed"), desc: "leading zeroes are not allowed".to_string(),
}); });
return None return None
} }
@ -708,7 +708,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: pos, lo: pos,
hi: pos, hi: pos,
desc: format!("expected start of a numeric literal"), desc: "expected start of a numeric literal".to_string(),
}); });
return None; return None;
} }
@ -733,7 +733,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: pos, lo: pos,
hi: pos, hi: pos,
desc: format!("numeral cannot end with an underscore"), desc: "numeral cannot end with an underscore".to_string(),
}); });
None None
} else { } else {
@ -830,7 +830,7 @@ impl<'a> Parser<'a> {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: start, lo: start,
hi: start + next, hi: start + next,
desc: format!("malformed date literal"), desc: "malformed date literal".to_string(),
}); });
None None
}; };
@ -907,14 +907,14 @@ impl<'a> Parser<'a> {
fn insert(&mut self, into: &mut TomlTable, key: String, value: Value, fn insert(&mut self, into: &mut TomlTable, key: String, value: Value,
key_lo: usize) { key_lo: usize) {
if into.values.contains_key(&key) { if let Entry::Vacant(entry) = into.values.entry(key.clone()) {
entry.insert(value);
} else {
self.errors.push(ParserError { self.errors.push(ParserError {
lo: key_lo, lo: key_lo,
hi: key_lo + key.len(), hi: key_lo + key.len(),
desc: format!("duplicate key: `{}`", key), desc: format!("duplicate key: `{}`", key),
}) });
} else {
into.values.insert(key, value);
} }
} }