Migrate to as many stable functions as possible
This commit is contained in:
parent
909a85069b
commit
482752b5f4
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
#![cfg_attr(test, deny(warnings))]
|
#![cfg_attr(test, deny(warnings))]
|
||||||
#![allow(unstable)]
|
#![cfg_attr(test, allow(unstable))]
|
||||||
|
|
||||||
extern crate "rustc-serialize" as rustc_serialize;
|
extern crate "rustc-serialize" as rustc_serialize;
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ impl Value {
|
||||||
|
|
||||||
/// Extracts the string of this value if it is a string.
|
/// Extracts the string of this value if it is a string.
|
||||||
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
|
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.
|
/// Extracts the integer value if it is an integer.
|
||||||
|
@ -138,12 +138,12 @@ impl Value {
|
||||||
/// 1979-05-27T07:32:00Z
|
/// 1979-05-27T07:32:00Z
|
||||||
/// ```
|
/// ```
|
||||||
pub fn as_datetime<'a>(&'a self) -> Option<&'a str> {
|
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.
|
/// Extracts the array value if it is an array.
|
||||||
pub fn as_slice<'a>(&'a self) -> Option<&'a [Value]> {
|
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.
|
/// Extracts the table value if it is a table.
|
||||||
|
|
|
@ -188,9 +188,9 @@ impl<'a> Parser<'a> {
|
||||||
let mut table = BTreeMap::new();
|
let mut table = BTreeMap::new();
|
||||||
if !self.values(&mut table) { return None }
|
if !self.values(&mut table) { return None }
|
||||||
if array {
|
if array {
|
||||||
self.insert_array(&mut ret, &keys[], Table(table), start)
|
self.insert_array(&mut ret, &*keys, Table(table), start)
|
||||||
} 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) { return None }
|
if !self.values(&mut ret) { return None }
|
||||||
|
@ -282,7 +282,7 @@ impl<'a> Parser<'a> {
|
||||||
Some((pos, '[')) => self.array(pos),
|
Some((pos, '[')) => self.array(pos),
|
||||||
Some((pos, '-')) |
|
Some((pos, '-')) |
|
||||||
Some((pos, '+')) => self.number_or_datetime(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 mut it = self.cur.clone();
|
||||||
let lo = it.next().map(|p| p.0).unwrap_or(self.input.len());
|
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> {
|
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 {
|
for _ in 0..15 {
|
||||||
match self.cur.next() {
|
match self.cur.next() {
|
||||||
Some((_, ch)) => date.push(ch),
|
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;
|
let mut valid = true;
|
||||||
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(|c| c.is_digit(10)).unwrap_or(false);
|
valid = valid && it.next().map(is_digit).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(|c| c.is_digit(10)).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 == '-').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(|c| c.is_digit(10)).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 == '-').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(|c| c.is_digit(10)).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 == 'T').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(|c| c.is_digit(10)).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 == ':').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(|c| c.is_digit(10)).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 == ':').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(|c| c.is_digit(10)).unwrap_or(false);
|
valid = valid && it.next().map(is_digit).unwrap_or(false);
|
||||||
valid = valid && it.next().map(|c| c == 'Z').unwrap_or(false);
|
valid = valid && it.next().map(|c| c == 'Z').unwrap_or(false);
|
||||||
if valid {
|
if valid {
|
||||||
Some(Datetime(date.clone()))
|
Some(Datetime(date.clone()))
|
||||||
|
@ -726,7 +726,7 @@ impl<'a> Parser<'a> {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some((cur, &keys.last().unwrap()[]))
|
Some((cur, &**keys.last().unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_table(&mut self, into: &mut TomlTable, keys: &[String],
|
fn insert_table(&mut self, into: &mut TomlTable, keys: &[String],
|
||||||
|
@ -735,7 +735,7 @@ impl<'a> Parser<'a> {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
let key = key.to_string();
|
let key = format!("{}", key);
|
||||||
let mut added = false;
|
let mut added = false;
|
||||||
if !into.contains_key(&key) {
|
if !into.contains_key(&key) {
|
||||||
into.insert(key.clone(), Table(BTreeMap::new()));
|
into.insert(key.clone(), Table(BTreeMap::new()));
|
||||||
|
@ -778,13 +778,13 @@ impl<'a> Parser<'a> {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
let key = key.to_string();
|
let key = format!("{}", key);
|
||||||
if !into.contains_key(&key) {
|
if !into.contains_key(&key) {
|
||||||
into.insert(key.clone(), Array(Vec::new()));
|
into.insert(key.clone(), Array(Vec::new()));
|
||||||
}
|
}
|
||||||
match *into.get_mut(&key).unwrap() {
|
match *into.get_mut(&key).unwrap() {
|
||||||
Array(ref mut vec) => {
|
Array(ref mut vec) => {
|
||||||
match vec.as_slice().first() {
|
match vec.first() {
|
||||||
Some(ref v) if !v.same_type(&value) => {
|
Some(ref v) if !v.same_type(&value) => {
|
||||||
self.errors.push(ParserError {
|
self.errors.push(ParserError {
|
||||||
lo: key_lo,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use Value::Table;
|
use Value::Table;
|
||||||
|
|
|
@ -203,10 +203,10 @@ impl rustc_serialize::Encoder for Encoder {
|
||||||
self.emit_value(Float(v))
|
self.emit_value(Float(v))
|
||||||
}
|
}
|
||||||
fn emit_char(&mut self, v: char) -> Result<(), Error> {
|
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> {
|
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)
|
fn emit_enum<F>(&mut self, _name: &str, f: F)
|
||||||
-> Result<(), Error>
|
-> Result<(), Error>
|
||||||
|
@ -269,7 +269,8 @@ impl rustc_serialize::Encoder for Encoder {
|
||||||
-> Result<(), Error>
|
-> Result<(), Error>
|
||||||
where F: FnOnce(&mut 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));
|
try!(f(self));
|
||||||
if self.state != Start {
|
if self.state != Start {
|
||||||
return Err(NoValue)
|
return Err(NoValue)
|
||||||
|
@ -406,7 +407,7 @@ impl Decoder {
|
||||||
self.cur_field.clone()
|
self.cur_field.clone()
|
||||||
} else {
|
} else {
|
||||||
match self.cur_field {
|
match self.cur_field {
|
||||||
None => Some(field.to_string()),
|
None => Some(format!("{}", field)),
|
||||||
Some(ref s) => Some(format!("{}.{}", s, 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> {
|
fn read_char(&mut self) -> Result<char, DecodeError> {
|
||||||
let ch = match self.toml {
|
let ch = match self.toml {
|
||||||
Some(Value::String(ref s)) if s.chars().count() == 1 =>
|
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)),
|
ref found => return Err(self.mismatch("string", found)),
|
||||||
};
|
};
|
||||||
self.toml.take();
|
self.toml.take();
|
||||||
|
@ -521,7 +522,7 @@ impl rustc_serialize::Decoder for Decoder {
|
||||||
where F: FnMut(&mut Decoder, usize) -> Result<T, DecodeError>
|
where F: FnMut(&mut Decoder, usize) -> Result<T, DecodeError>
|
||||||
{
|
{
|
||||||
let mut first_error = None;
|
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(), "");
|
let mut d = self.sub_decoder(self.toml.clone(), "");
|
||||||
match f(&mut d, i) {
|
match f(&mut d, i) {
|
||||||
Ok(t) => { self.toml = d.toml; return Ok(t) }
|
Ok(t) => { self.toml = d.toml; return Ok(t) }
|
||||||
|
@ -578,7 +579,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 = f_name.to_string();
|
let field = format!("{}", f_name);
|
||||||
let toml = match self.toml {
|
let toml = match self.toml {
|
||||||
Some(Table(ref mut table)) => {
|
Some(Table(ref mut table)) => {
|
||||||
table.remove(&field)
|
table.remove(&field)
|
||||||
|
@ -700,8 +701,8 @@ impl rustc_serialize::Decoder for Decoder {
|
||||||
Some(Table(ref table)) => {
|
Some(Table(ref table)) => {
|
||||||
match table.iter().skip(idx).next() {
|
match table.iter().skip(idx).next() {
|
||||||
Some((key, _)) => {
|
Some((key, _)) => {
|
||||||
f(&mut self.sub_decoder(Some(Value::String(key.to_string())),
|
let val = Value::String(format!("{}", key));
|
||||||
key.as_slice()))
|
f(&mut self.sub_decoder(Some(val), &**key))
|
||||||
}
|
}
|
||||||
None => Err(self.err(ExpectedMapKey(idx))),
|
None => Err(self.err(ExpectedMapKey(idx))),
|
||||||
}
|
}
|
||||||
|
@ -730,7 +731,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(err.to_string())
|
kind: ApplicationError(format!("{}", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -785,7 +786,7 @@ impl fmt::Display for DecodeError {
|
||||||
impl StdError for DecodeError {
|
impl StdError for DecodeError {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
ApplicationError(ref s) => s.as_slice(),
|
ApplicationError(ref s) => &**s,
|
||||||
ExpectedField(..) => "expected a field",
|
ExpectedField(..) => "expected a field",
|
||||||
ExpectedType(..) => "expected a type",
|
ExpectedType(..) => "expected a type",
|
||||||
ExpectedMapKey(..) => "expected a map key",
|
ExpectedMapKey(..) => "expected a map key",
|
||||||
|
|
10
src/show.rs
10
src/show.rs
|
@ -13,7 +13,7 @@ impl fmt::Display for Value {
|
||||||
match *self {
|
match *self {
|
||||||
String(ref s) => {
|
String(ref s) => {
|
||||||
try!(write!(f, "\""));
|
try!(write!(f, "\""));
|
||||||
for ch in s.as_slice().chars() {
|
for ch in s.chars() {
|
||||||
match ch {
|
match ch {
|
||||||
'\u{8}' => try!(write!(f, "\\b")),
|
'\u{8}' => try!(write!(f, "\\b")),
|
||||||
'\u{9}' => try!(write!(f, "\\t")),
|
'\u{9}' => try!(write!(f, "\\t")),
|
||||||
|
@ -57,7 +57,7 @@ impl<'a, 'b> Printer<'a, 'b> {
|
||||||
match *v {
|
match *v {
|
||||||
Table(..) => continue,
|
Table(..) => continue,
|
||||||
Array(ref a) => {
|
Array(ref a) => {
|
||||||
match a.as_slice().first() {
|
match a.first() {
|
||||||
Some(&Table(..)) => continue,
|
Some(&Table(..)) => continue,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -69,18 +69,18 @@ impl<'a, 'b> Printer<'a, 'b> {
|
||||||
for (k, v) in table.iter() {
|
for (k, v) in table.iter() {
|
||||||
match *v {
|
match *v {
|
||||||
Table(ref inner) => {
|
Table(ref inner) => {
|
||||||
self.stack.push(k.as_slice());
|
self.stack.push(&**k);
|
||||||
try!(writeln!(self.output, "\n[{}]",
|
try!(writeln!(self.output, "\n[{}]",
|
||||||
self.stack.connect(".")));
|
self.stack.connect(".")));
|
||||||
try!(self.print(inner));
|
try!(self.print(inner));
|
||||||
self.stack.pop();
|
self.stack.pop();
|
||||||
}
|
}
|
||||||
Array(ref inner) => {
|
Array(ref inner) => {
|
||||||
match inner.as_slice().first() {
|
match inner.first() {
|
||||||
Some(&Table(..)) => {}
|
Some(&Table(..)) => {}
|
||||||
_ => continue
|
_ => continue
|
||||||
}
|
}
|
||||||
self.stack.push(k.as_slice());
|
self.stack.push(&**k);
|
||||||
for inner in inner.iter() {
|
for inner in inner.iter() {
|
||||||
try!(writeln!(self.output, "\n[[{}]]",
|
try!(writeln!(self.output, "\n[[{}]]",
|
||||||
self.stack.connect(".")));
|
self.stack.connect(".")));
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate "rustc-serialize" as rustc_serialize;
|
extern crate "rustc-serialize" as rustc_serialize;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
|
|
||||||
use std::num::strconv;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use rustc_serialize::json::Json;
|
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 to_json(toml: Value) -> Json {
|
||||||
fn doit(s: &str, json: Json) -> Json {
|
fn doit(s: &str, json: Json) -> Json {
|
||||||
let mut map = BTreeMap::new();
|
let mut map = BTreeMap::new();
|
||||||
map.insert("type".to_string(), Json::String(s.to_string()));
|
map.insert(format!("{}", "type"), Json::String(format!("{}", s)));
|
||||||
map.insert("value".to_string(), json);
|
map.insert(format!("{}", "value"), json);
|
||||||
Json::Object(map)
|
Json::Object(map)
|
||||||
}
|
}
|
||||||
match toml {
|
match toml {
|
||||||
Value::String(s) => doit("string", Json::String(s)),
|
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({
|
Float(f) => doit("float", Json::String({
|
||||||
let (bytes, _) =
|
let s = format!("{:.15}", f);
|
||||||
strconv::float_to_str_bytes_common(f, 10, true,
|
let s = format!("{}", s.trim_right_matches('0'));
|
||||||
strconv::SignFormat::SignNeg,
|
if s.ends_with(".") {format!("{}0", s)} else {s}
|
||||||
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)}
|
|
||||||
})),
|
})),
|
||||||
Boolean(b) => doit("bool", Json::String(b.to_string())),
|
Boolean(b) => doit("bool", Json::String(format!("{}", b))),
|
||||||
Datetime(s) => doit("datetime", Json::String(s)),
|
Datetime(s) => doit("datetime", Json::String(s)),
|
||||||
Array(arr) => {
|
Array(arr) => {
|
||||||
let is_table = match arr.as_slice().first() {
|
let is_table = match arr.first() {
|
||||||
Some(&Table(..)) => true,
|
Some(&Table(..)) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue