parent
1ca52c5ea4
commit
22ad6e7ea9
15
src/lib.rs
15
src/lib.rs
|
@ -46,6 +46,7 @@ extern crate serialize;
|
||||||
|
|
||||||
use std::collections::TreeMap;
|
use std::collections::TreeMap;
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
|
use std::string;
|
||||||
|
|
||||||
pub use parser::{Parser,ParserError};
|
pub use parser::{Parser,ParserError};
|
||||||
pub use serialization::{Encoder, encode, encode_str};
|
pub use serialization::{Encoder, encode, encode_str};
|
||||||
|
@ -61,17 +62,17 @@ mod serialization;
|
||||||
#[deriving(PartialEq, Clone)]
|
#[deriving(PartialEq, Clone)]
|
||||||
#[allow(missing_doc)]
|
#[allow(missing_doc)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
String(String),
|
String(string::String),
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Datetime(String),
|
Datetime(string::String),
|
||||||
Array(Array),
|
Array(TomlArray),
|
||||||
Table(Table),
|
Table(TomlTable),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Array = Vec<Value>;
|
pub type TomlArray = Vec<Value>;
|
||||||
pub type Table = TreeMap<String, Value>;
|
pub type TomlTable = TreeMap<string::String, Value>;
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
/// Tests whether this and another value have the same type.
|
/// Tests whether this and another value have the same type.
|
||||||
|
@ -140,7 +141,7 @@ impl Value {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the table value if it is a table.
|
/// Extracts the table value if it is a table.
|
||||||
pub fn as_table<'a>(&'a self) -> Option<&'a Table> {
|
pub fn as_table<'a>(&'a self) -> Option<&'a TomlTable> {
|
||||||
match *self { Table(ref s) => Some(s), _ => None }
|
match *self { Table(ref s) => Some(s), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::collections::{TreeMap, HashSet};
|
||||||
use std::num::FromStrRadix;
|
use std::num::FromStrRadix;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use {Array, Table, Value, String, Float, Integer, Boolean, Datetime};
|
use {Array, Table, Value, Float, Integer, Boolean, Datetime, TomlTable};
|
||||||
|
|
||||||
/// Parser for converting a string to a TOML `Value` instance.
|
/// Parser for converting a string to a TOML `Value` instance.
|
||||||
///
|
///
|
||||||
|
@ -129,13 +129,13 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
/// Executes the parser, parsing the string contained within.
|
/// Executes the parser, parsing the string contained within.
|
||||||
///
|
///
|
||||||
/// This function will return the `Table` instance if parsing is successful,
|
/// This function will return the `TomlTable` instance if parsing is
|
||||||
/// or it will return `None` if any parse error or invalid TOML error
|
/// successful, or it will return `None` if any parse error or invalid TOML
|
||||||
/// occurs.
|
/// error occurs.
|
||||||
///
|
///
|
||||||
/// If an error occurs, the `errors` field of this parser can be consulted
|
/// If an error occurs, the `errors` field of this parser can be consulted
|
||||||
/// to determine the cause of the parse failure.
|
/// to determine the cause of the parse failure.
|
||||||
pub fn parse(&mut self) -> Option<Table> {
|
pub fn parse(&mut self) -> Option<TomlTable> {
|
||||||
let mut ret = TreeMap::new();
|
let mut ret = TreeMap::new();
|
||||||
loop {
|
loop {
|
||||||
self.ws();
|
self.ws();
|
||||||
|
@ -189,7 +189,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn values(&mut self, into: &mut Table) -> bool {
|
fn values(&mut self, into: &mut TomlTable) -> bool {
|
||||||
loop {
|
loop {
|
||||||
self.ws();
|
self.ws();
|
||||||
match self.cur.clone().next() {
|
match self.cur.clone().next() {
|
||||||
|
@ -273,7 +273,7 @@ impl<'a> Parser<'a> {
|
||||||
self.eat('\n');
|
self.eat('\n');
|
||||||
} else {
|
} else {
|
||||||
// empty
|
// empty
|
||||||
return Some(String(ret))
|
return Some(::String(ret))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(String(ret));
|
return Some(::String(ret));
|
||||||
|
|
||||||
fn escape(me: &mut Parser, pos: uint, multiline: bool) -> Option<char> {
|
fn escape(me: &mut Parser, pos: uint, multiline: bool) -> Option<char> {
|
||||||
match me.cur.next() {
|
match me.cur.next() {
|
||||||
|
@ -434,7 +434,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(String(ret));
|
return Some(::String(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn number_or_datetime(&mut self, start: uint) -> Option<Value> {
|
fn number_or_datetime(&mut self, start: uint) -> Option<Value> {
|
||||||
|
@ -595,7 +595,7 @@ impl<'a> Parser<'a> {
|
||||||
return Some(Array(ret))
|
return Some(Array(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(&mut self, into: &mut Table, key: String, value: Value,
|
fn insert(&mut self, into: &mut TomlTable, key: String, value: Value,
|
||||||
key_lo: uint) {
|
key_lo: uint) {
|
||||||
if into.contains_key(&key) {
|
if into.contains_key(&key) {
|
||||||
self.errors.push(ParserError {
|
self.errors.push(ParserError {
|
||||||
|
@ -608,8 +608,8 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recurse<'a>(&mut self, mut cur: &'a mut Table, orig_key: &'a str,
|
fn recurse<'a>(&mut self, mut cur: &'a mut TomlTable, orig_key: &'a str,
|
||||||
key_lo: uint) -> Option<(&'a mut Table, &'a str)> {
|
key_lo: uint) -> Option<(&'a mut TomlTable, &'a str)> {
|
||||||
if orig_key.starts_with(".") || orig_key.ends_with(".") ||
|
if orig_key.starts_with(".") || orig_key.ends_with(".") ||
|
||||||
orig_key.contains("..") {
|
orig_key.contains("..") {
|
||||||
self.errors.push(ParserError {
|
self.errors.push(ParserError {
|
||||||
|
@ -670,7 +670,7 @@ impl<'a> Parser<'a> {
|
||||||
return Some((cur, orig_key.slice_from(key.len() + 1)))
|
return Some((cur, orig_key.slice_from(key.len() + 1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_table(&mut self, into: &mut Table, key: String, value: Table,
|
fn insert_table(&mut self, into: &mut TomlTable, key: String, value: TomlTable,
|
||||||
key_lo: uint) {
|
key_lo: uint) {
|
||||||
if !self.tables_defined.insert(key.clone()) {
|
if !self.tables_defined.insert(key.clone()) {
|
||||||
self.errors.push(ParserError {
|
self.errors.push(ParserError {
|
||||||
|
@ -712,7 +712,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_array(&mut self, into: &mut Table, key: String, value: Value,
|
fn insert_array(&mut self, into: &mut TomlTable, key: String, value: Value,
|
||||||
key_lo: uint) {
|
key_lo: uint) {
|
||||||
let (into, key) = match self.recurse(into, key.as_slice(), key_lo) {
|
let (into, key) = match self.recurse(into, key.as_slice(), key_lo) {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::mem;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use serialize;
|
use serialize;
|
||||||
use {Value, Table, Array, String, Integer, Float, Boolean, Parser};
|
use {Value, Table, Array, Integer, Float, Boolean, Parser, TomlTable};
|
||||||
|
|
||||||
/// A structure to transform Rust values into TOML values.
|
/// A structure to transform Rust values into TOML values.
|
||||||
///
|
///
|
||||||
|
@ -38,7 +38,7 @@ pub struct Encoder {
|
||||||
///
|
///
|
||||||
/// This field can be used to extract the return value after feeding a value
|
/// This field can be used to extract the return value after feeding a value
|
||||||
/// into this `Encoder`.
|
/// into this `Encoder`.
|
||||||
pub toml: Table,
|
pub toml: TomlTable,
|
||||||
state: EncoderState,
|
state: EncoderState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ impl Encoder {
|
||||||
}
|
}
|
||||||
NextMapKey => {
|
NextMapKey => {
|
||||||
match v {
|
match v {
|
||||||
String(s) => { self.state = NextKey(s); Ok(()) }
|
::String(s) => { self.state = NextKey(s); Ok(()) }
|
||||||
_ => Err(InvalidMapKeyType)
|
_ => Err(InvalidMapKeyType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,7 +194,7 @@ impl serialize::Encoder<Error> for Encoder {
|
||||||
self.emit_str(v.to_string().as_slice())
|
self.emit_str(v.to_string().as_slice())
|
||||||
}
|
}
|
||||||
fn emit_str(&mut self, v: &str) -> Result<(), Error> {
|
fn emit_str(&mut self, v: &str) -> Result<(), Error> {
|
||||||
self.emit_value(String(v.to_string()))
|
self.emit_value(::String(v.to_string()))
|
||||||
}
|
}
|
||||||
fn emit_enum(&mut self, _name: &str,
|
fn emit_enum(&mut self, _name: &str,
|
||||||
f: |&mut Encoder| -> Result<(), Error>) -> Result<(), Error> {
|
f: |&mut Encoder| -> Result<(), Error>) -> Result<(), Error> {
|
||||||
|
@ -409,8 +409,8 @@ impl Decoder {
|
||||||
impl serialize::Decoder<DecodeError> for Decoder {
|
impl serialize::Decoder<DecodeError> for Decoder {
|
||||||
fn read_nil(&mut self) -> Result<(), DecodeError> {
|
fn read_nil(&mut self) -> Result<(), DecodeError> {
|
||||||
match self.toml {
|
match self.toml {
|
||||||
Some(String(ref s)) if s.len() == 0 => {}
|
Some(::String(ref s)) if s.len() == 0 => {}
|
||||||
Some(String(..)) => return Err(self.err(NilTooLong)),
|
Some(::String(..)) => return Err(self.err(NilTooLong)),
|
||||||
ref found => return Err(self.mismatch("string", found)),
|
ref found => return Err(self.mismatch("string", found)),
|
||||||
}
|
}
|
||||||
self.toml.take();
|
self.toml.take();
|
||||||
|
@ -466,7 +466,7 @@ impl serialize::Decoder<DecodeError> 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(String(ref s)) if s.as_slice().char_len() == 1 =>
|
Some(::String(ref s)) if s.as_slice().char_len() == 1 =>
|
||||||
s.as_slice().char_at(0),
|
s.as_slice().char_at(0),
|
||||||
ref found => return Err(self.mismatch("string", found)),
|
ref found => return Err(self.mismatch("string", found)),
|
||||||
};
|
};
|
||||||
|
@ -475,7 +475,7 @@ impl serialize::Decoder<DecodeError> for Decoder {
|
||||||
}
|
}
|
||||||
fn read_str(&mut self) -> Result<String, DecodeError> {
|
fn read_str(&mut self) -> Result<String, DecodeError> {
|
||||||
match self.toml.take() {
|
match self.toml.take() {
|
||||||
Some(String(s)) => Ok(s),
|
Some(::String(s)) => Ok(s),
|
||||||
found => {
|
found => {
|
||||||
let err = Err(self.mismatch("string", &found));
|
let err = Err(self.mismatch("string", &found));
|
||||||
self.toml = found;
|
self.toml = found;
|
||||||
|
@ -673,7 +673,7 @@ impl serialize::Decoder<DecodeError> 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(String(key.to_string())),
|
f(&mut self.sub_decoder(Some(::String(key.to_string())),
|
||||||
key.as_slice()))
|
key.as_slice()))
|
||||||
}
|
}
|
||||||
None => Err(self.err(ExpectedMapKey(idx))),
|
None => Err(self.err(ExpectedMapKey(idx))),
|
||||||
|
@ -761,7 +761,7 @@ mod tests {
|
||||||
use serialize::{Encodable, Decodable};
|
use serialize::{Encodable, Decodable};
|
||||||
|
|
||||||
use super::{Encoder, Decoder, DecodeError};
|
use super::{Encoder, Decoder, DecodeError};
|
||||||
use {Table, Integer, String, Array, Float};
|
use {Table, Integer, Array, Float};
|
||||||
|
|
||||||
macro_rules! encode( ($t:expr) => ({
|
macro_rules! encode( ($t:expr) => ({
|
||||||
let mut e = Encoder::new();
|
let mut e = Encoder::new();
|
||||||
|
@ -816,7 +816,7 @@ mod tests {
|
||||||
map! {
|
map! {
|
||||||
a: Integer(2),
|
a: Integer(2),
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: String("test".to_string())
|
a: ::String("test".to_string())
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
@ -837,7 +837,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut d_good = Decoder::new(Integer(5));
|
let mut d_good = Decoder::new(Integer(5));
|
||||||
let mut d_bad1 = Decoder::new(String("not an int".to_string()));
|
let mut d_bad1 = Decoder::new(::String("not an int".to_string()));
|
||||||
let mut d_bad2 = Decoder::new(Integer(11));
|
let mut d_bad2 = Decoder::new(Integer(11));
|
||||||
|
|
||||||
assert_eq!(Ok(Range10(5)), Decodable::decode(&mut d_good));
|
assert_eq!(Ok(Range10(5)), Decodable::decode(&mut d_good));
|
||||||
|
@ -908,12 +908,12 @@ mod tests {
|
||||||
map! {
|
map! {
|
||||||
a: Table(map! {
|
a: Table(map! {
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: String("foo".to_string()),
|
a: ::String("foo".to_string()),
|
||||||
b: Float(4.5)
|
b: Float(4.5)
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: String("bar".to_string()),
|
a: ::String("bar".to_string()),
|
||||||
b: Float(1.0)
|
b: Float(1.0)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -947,7 +947,7 @@ mod tests {
|
||||||
foo: Integer(10),
|
foo: Integer(10),
|
||||||
bar: Integer(4)
|
bar: Integer(4)
|
||||||
}),
|
}),
|
||||||
set: Array(vec![String("a".to_string())])
|
set: Array(vec![::String("a".to_string())])
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
@ -963,7 +963,7 @@ mod tests {
|
||||||
encode!(v),
|
encode!(v),
|
||||||
map! {
|
map! {
|
||||||
_field0: Integer(1),
|
_field0: Integer(1),
|
||||||
_field1: String("foo".to_string()),
|
_field1: ::String("foo".to_string()),
|
||||||
_field2: Float(4.5)
|
_field2: Float(4.5)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1058,7 +1058,7 @@ mod tests {
|
||||||
let v = Foo { a: Last(Foo2 { test: "test".to_string() }) };
|
let v = Foo { a: Last(Foo2 { test: "test".to_string() }) };
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
encode!(v),
|
encode!(v),
|
||||||
map! { a: Table(map! { test: String("test".to_string()) }) }
|
map! { a: Table(map! { test: ::String("test".to_string()) }) }
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
}
|
}
|
||||||
|
@ -1129,7 +1129,7 @@ mod tests {
|
||||||
let v = Foo { a: map! { a: "foo".to_string() } };
|
let v = Foo { a: map! { a: "foo".to_string() } };
|
||||||
let mut d = Decoder::new(Table(map! {
|
let mut d = Decoder::new(Table(map! {
|
||||||
a: Table(map! {
|
a: Table(map! {
|
||||||
a: String("foo".to_string())
|
a: ::String("foo".to_string())
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
||||||
|
@ -1144,7 +1144,7 @@ mod tests {
|
||||||
|
|
||||||
let v = Foo { a: vec!["a".to_string()] };
|
let v = Foo { a: vec!["a".to_string()] };
|
||||||
let mut d = Decoder::new(Table(map! {
|
let mut d = Decoder::new(Table(map! {
|
||||||
a: Array(vec![String("a".to_string())])
|
a: Array(vec![::String("a".to_string())])
|
||||||
}));
|
}));
|
||||||
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table};
|
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table, TomlTable};
|
||||||
|
|
||||||
struct Printer<'a, 'b:'a> {
|
struct Printer<'a, 'b:'a> {
|
||||||
output: &'a mut fmt::Formatter<'b>,
|
output: &'a mut fmt::Formatter<'b>,
|
||||||
|
@ -51,7 +51,7 @@ impl fmt::Show for Value {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Printer<'a, 'b> {
|
impl<'a, 'b> Printer<'a, 'b> {
|
||||||
fn print(&mut self, table: &'a Table) -> fmt::Result {
|
fn print(&mut self, table: &'a TomlTable) -> fmt::Result {
|
||||||
for (k, v) in table.iter() {
|
for (k, v) in table.iter() {
|
||||||
match *v {
|
match *v {
|
||||||
Table(..) => continue,
|
Table(..) => continue,
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::num::strconv;
|
||||||
use std::collections::TreeMap;
|
use std::collections::TreeMap;
|
||||||
use self::serialize::json;
|
use self::serialize::json;
|
||||||
|
|
||||||
use {Parser, Value, Table, String, Integer, Float, Boolean, Datetime, Array};
|
use {Parser, Value, Table, Integer, Float, Boolean, Datetime, Array};
|
||||||
|
|
||||||
fn to_json(toml: Value) -> json::Json {
|
fn to_json(toml: Value) -> json::Json {
|
||||||
fn doit(s: &str, json: json::Json) -> json::Json {
|
fn doit(s: &str, json: json::Json) -> json::Json {
|
||||||
|
@ -14,7 +14,7 @@ fn to_json(toml: Value) -> json::Json {
|
||||||
json::Object(map)
|
json::Object(map)
|
||||||
}
|
}
|
||||||
match toml {
|
match toml {
|
||||||
String(s) => doit("string", json::String(s)),
|
::String(s) => doit("string", json::String(s)),
|
||||||
Integer(i) => doit("integer", json::String(i.to_string())),
|
Integer(i) => doit("integer", json::String(i.to_string())),
|
||||||
Float(f) => doit("float", json::String({
|
Float(f) => doit("float", json::String({
|
||||||
let (bytes, _) =
|
let (bytes, _) =
|
||||||
|
|
Loading…
Reference in a new issue