aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs8
-rw-r--r--src/parser.rs50
-rw-r--r--src/serialization.rs23
-rw-r--r--src/show.rs10
-rw-r--r--tests/valid.rs22
5 files changed, 56 insertions, 57 deletions
diff --git a/src/lib.rs b/src/lib.rs
index bbe13d1..b24d264 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -38,7 +38,7 @@
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
-#![allow(unstable)]
+#![cfg_attr(test, allow(unstable))]
extern crate "rustc-serialize" as rustc_serialize;
@@ -111,7 +111,7 @@ impl Value {
/// Extracts the string of this value if it is a string.
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
- match *self { Value::String(ref s) => Some(s.as_slice()), _ => None }
+ match *self { Value::String(ref s) => Some(&**s), _ => None }
}
/// Extracts the integer value if it is an integer.
@@ -138,12 +138,12 @@ impl Value {
/// 1979-05-27T07:32:00Z
/// ```
pub fn as_datetime<'a>(&'a self) -> Option<&'a str> {
- match *self { Value::Datetime(ref s) => Some(s.as_slice()), _ => None }
+ match *self { Value::Datetime(ref s) => Some(&**s), _ => None }
}
/// Extracts the array value if it is an array.
pub fn as_slice<'a>(&'a self) -> Option<&'a [Value]> {
- match *self { Value::Array(ref s) => Some(s.as_slice()), _ => None }
+ match *self { Value::Array(ref s) => Some(&**s), _ => None }
}
/// Extracts the table value if it is a table.
diff --git a/src/parser.rs b/src/parser.rs
index 6f4f3ce..4316df3 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -188,9 +188,9 @@ impl<'a> Parser<'a> {
let mut table = BTreeMap::new();
if !self.values(&mut table) { return None }
if array {
- self.insert_array(&mut ret, &keys[], Table(table), start)
+ self.insert_array(&mut ret, &*keys, Table(table), start)
} else {
- self.insert_table(&mut ret, &keys[], table, start)
+ self.insert_table(&mut ret, &*keys, table, start)
}
} else {
if !self.values(&mut ret) { return None }
@@ -282,7 +282,7 @@ impl<'a> Parser<'a> {
Some((pos, '[')) => self.array(pos),
Some((pos, '-')) |
Some((pos, '+')) => self.number_or_datetime(pos),
- Some((pos, ch)) if ch.is_digit(10) => self.number_or_datetime(pos),
+ Some((pos, ch)) if is_digit(ch) => self.number_or_datetime(pos),
_ => {
let mut it = self.cur.clone();
let lo = it.next().map(|p| p.0).unwrap_or(self.input.len());
@@ -574,7 +574,7 @@ impl<'a> Parser<'a> {
}
fn datetime(&mut self, start: usize, end_so_far: usize) -> Option<Value> {
- let mut date = self.input[start..end_so_far].to_string();
+ let mut date = format!("{}", &self.input[start..end_so_far]);
for _ in 0..15 {
match self.cur.next() {
Some((_, ch)) => date.push(ch),
@@ -588,27 +588,27 @@ impl<'a> Parser<'a> {
}
}
}
- let mut it = date.as_slice().chars();
+ let mut it = date.chars();
let mut valid = true;
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == '-').unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == '-').unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == 'T').unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == ':').unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == ':').unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
- valid = valid && it.next().map(|c| c.is_digit(10)).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
+ valid = valid && it.next().map(is_digit).unwrap_or(false);
valid = valid && it.next().map(|c| c == 'Z').unwrap_or(false);
if valid {
Some(Datetime(date.clone()))
@@ -726,7 +726,7 @@ impl<'a> Parser<'a> {
_ => unreachable!(),
}
}
- Some((cur, &keys.last().unwrap()[]))
+ Some((cur, &**keys.last().unwrap()))
}
fn insert_table(&mut self, into: &mut TomlTable, keys: &[String],
@@ -735,7 +735,7 @@ impl<'a> Parser<'a> {
Some(pair) => pair,
None => return,
};
- let key = key.to_string();
+ let key = format!("{}", key);
let mut added = false;
if !into.contains_key(&key) {
into.insert(key.clone(), Table(BTreeMap::new()));
@@ -778,13 +778,13 @@ impl<'a> Parser<'a> {
Some(pair) => pair,
None => return,
};
- let key = key.to_string();
+ let key = format!("{}", key);
if !into.contains_key(&key) {
into.insert(key.clone(), Array(Vec::new()));
}
match *into.get_mut(&key).unwrap() {
Array(ref mut vec) => {
- match vec.as_slice().first() {
+ match vec.first() {
Some(ref v) if !v.same_type(&value) => {
self.errors.push(ParserError {
lo: key_lo,
@@ -818,6 +818,10 @@ impl fmt::Display for ParserError {
}
}
+fn is_digit(c: char) -> bool {
+ match c { '0' ... '9' => true, _ => false }
+}
+
#[cfg(test)]
mod tests {
use Value::Table;
diff --git a/src/serialization.rs b/src/serialization.rs
index ebc9f39..a7ddaf3 100644
--- a/src/serialization.rs
+++ b/src/serialization.rs
@@ -203,10 +203,10 @@ impl rustc_serialize::Encoder for Encoder {
self.emit_value(Float(v))
}
fn emit_char(&mut self, v: char) -> Result<(), Error> {
- self.emit_str(v.to_string().as_slice())
+ self.emit_str(&*format!("{}", v))
}
fn emit_str(&mut self, v: &str) -> Result<(), Error> {
- self.emit_value(Value::String(v.to_string()))
+ self.emit_value(Value::String(format!("{}", v)))
}
fn emit_enum<F>(&mut self, _name: &str, f: F)
-> Result<(), Error>
@@ -269,7 +269,8 @@ impl rustc_serialize::Encoder for Encoder {
-> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error>
{
- let old = mem::replace(&mut self.state, NextKey(f_name.to_string()));
+ let old = mem::replace(&mut self.state,
+ NextKey(format!("{}", f_name)));
try!(f(self));
if self.state != Start {
return Err(NoValue)
@@ -406,7 +407,7 @@ impl Decoder {
self.cur_field.clone()
} else {
match self.cur_field {
- None => Some(field.to_string()),
+ None => Some(format!("{}", field)),
Some(ref s) => Some(format!("{}.{}", s, field))
}
}
@@ -491,7 +492,7 @@ impl rustc_serialize::Decoder for Decoder {
fn read_char(&mut self) -> Result<char, DecodeError> {
let ch = match self.toml {
Some(Value::String(ref s)) if s.chars().count() == 1 =>
- s.as_slice().char_at(0),
+ s.chars().next().unwrap(),
ref found => return Err(self.mismatch("string", found)),
};
self.toml.take();
@@ -521,7 +522,7 @@ impl rustc_serialize::Decoder for Decoder {
where F: FnMut(&mut Decoder, usize) -> Result<T, DecodeError>
{
let mut first_error = None;
- for i in range(0, names.len()) {
+ for i in 0..names.len() {
let mut d = self.sub_decoder(self.toml.clone(), "");
match f(&mut d, i) {
Ok(t) => { self.toml = d.toml; return Ok(t) }
@@ -578,7 +579,7 @@ impl rustc_serialize::Decoder for Decoder {
-> Result<T, DecodeError>
where F: FnOnce(&mut Decoder) -> Result<T, DecodeError>
{
- let field = f_name.to_string();
+ let field = format!("{}", f_name);
let toml = match self.toml {
Some(Table(ref mut table)) => {
table.remove(&field)
@@ -700,8 +701,8 @@ impl rustc_serialize::Decoder for Decoder {
Some(Table(ref table)) => {
match table.iter().skip(idx).next() {
Some((key, _)) => {
- f(&mut self.sub_decoder(Some(Value::String(key.to_string())),
- key.as_slice()))
+ let val = Value::String(format!("{}", key));
+ f(&mut self.sub_decoder(Some(val), &**key))
}
None => Err(self.err(ExpectedMapKey(idx))),
}
@@ -730,7 +731,7 @@ impl rustc_serialize::Decoder for Decoder {
fn error(&mut self, err: &str) -> DecodeError {
DecodeError {
field: self.cur_field.clone(),
- kind: ApplicationError(err.to_string())
+ kind: ApplicationError(format!("{}", err))
}
}
}
@@ -785,7 +786,7 @@ impl fmt::Display for DecodeError {
impl StdError for DecodeError {
fn description(&self) -> &str {
match self.kind {
- ApplicationError(ref s) => s.as_slice(),
+ ApplicationError(ref s) => &**s,
ExpectedField(..) => "expected a field",
ExpectedType(..) => "expected a type",
ExpectedMapKey(..) => "expected a map key",
diff --git a/src/show.rs b/src/show.rs
index fcbc277..21aec1e 100644
--- a/src/show.rs
+++ b/src/show.rs
@@ -13,7 +13,7 @@ impl fmt::Display for Value {
match *self {
String(ref s) => {
try!(write!(f, "\""));
- for ch in s.as_slice().chars() {
+ for ch in s.chars() {
match ch {
'\u{8}' => try!(write!(f, "\\b")),
'\u{9}' => try!(write!(f, "\\t")),
@@ -57,7 +57,7 @@ impl<'a, 'b> Printer<'a, 'b> {
match *v {
Table(..) => continue,
Array(ref a) => {
- match a.as_slice().first() {
+ match a.first() {
Some(&Table(..)) => continue,
_ => {}
}
@@ -69,18 +69,18 @@ impl<'a, 'b> Printer<'a, 'b> {
for (k, v) in table.iter() {
match *v {
Table(ref inner) => {
- self.stack.push(k.as_slice());
+ self.stack.push(&**k);
try!(writeln!(self.output, "\n[{}]",
self.stack.connect(".")));
try!(self.print(inner));
self.stack.pop();
}
Array(ref inner) => {
- match inner.as_slice().first() {
+ match inner.first() {
Some(&Table(..)) => {}
_ => continue
}
- self.stack.push(k.as_slice());
+ self.stack.push(&**k);
for inner in inner.iter() {
try!(writeln!(self.output, "\n[[{}]]",
self.stack.connect(".")));
diff --git a/tests/valid.rs b/tests/valid.rs
index 72e9d94..628130e 100644
--- a/tests/valid.rs
+++ b/tests/valid.rs
@@ -1,7 +1,6 @@
extern crate "rustc-serialize" as rustc_serialize;
extern crate toml;
-use std::num::strconv;
use std::collections::BTreeMap;
use rustc_serialize::json::Json;
@@ -11,27 +10,22 @@ use toml::Value::{Table, Integer, Float, Boolean, Datetime, Array};
fn to_json(toml: Value) -> Json {
fn doit(s: &str, json: Json) -> Json {
let mut map = BTreeMap::new();
- map.insert("type".to_string(), Json::String(s.to_string()));
- map.insert("value".to_string(), json);
+ map.insert(format!("{}", "type"), Json::String(format!("{}", s)));
+ map.insert(format!("{}", "value"), json);
Json::Object(map)
}
match toml {
Value::String(s) => doit("string", Json::String(s)),
- Integer(i) => doit("integer", Json::String(i.to_string())),
+ Integer(i) => doit("integer", Json::String(format!("{}", i))),
Float(f) => doit("float", Json::String({
- let (bytes, _) =
- strconv::float_to_str_bytes_common(f, 10, true,
- strconv::SignFormat::SignNeg,
- strconv::SignificantDigits::DigMax(15),
- strconv::ExponentFormat::ExpNone,
- false);
- let s = String::from_utf8(bytes).unwrap();
- if s.as_slice().contains(".") {s} else {format!("{}.0", s)}
+ let s = format!("{:.15}", f);
+ let s = format!("{}", s.trim_right_matches('0'));
+ if s.ends_with(".") {format!("{}0", s)} else {s}
})),
- Boolean(b) => doit("bool", Json::String(b.to_string())),
+ Boolean(b) => doit("bool", Json::String(format!("{}", b))),
Datetime(s) => doit("datetime", Json::String(s)),
Array(arr) => {
- let is_table = match arr.as_slice().first() {
+ let is_table = match arr.first() {
Some(&Table(..)) => true,
_ => false,
};