Update to namespaced enum variants.
This commit is contained in:
parent
c5284ffc32
commit
bfcfa89e95
14
src/lib.rs
14
src/lib.rs
|
@ -50,11 +50,15 @@ 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};
|
||||||
pub use serialization::{Decoder, decode, decode_str};
|
pub use serialization::{Decoder, decode, decode_str};
|
||||||
pub use serialization::{Error, NeedsKey, NoValue};
|
pub use serialization::Error;
|
||||||
pub use serialization::{InvalidMapKeyLocation, InvalidMapKeyType};
|
pub use serialization::Error::{NeedsKey, NoValue};
|
||||||
pub use serialization::{DecodeError, ApplicationError, ExpectedField};
|
pub use serialization::Error::{InvalidMapKeyLocation, InvalidMapKeyType};
|
||||||
pub use serialization::{ExpectedMapElement, ExpectedMapKey, NoEnumVariants};
|
pub use serialization::{DecodeError, DecodeErrorKind};
|
||||||
pub use serialization::{ExpectedType, NilTooLong};
|
pub use serialization::DecodeErrorKind::{ApplicationError, ExpectedField};
|
||||||
|
pub use serialization::DecodeErrorKind::{ExpectedMapElement, ExpectedMapKey, NoEnumVariants};
|
||||||
|
pub use serialization::DecodeErrorKind::{ExpectedType, NilTooLong};
|
||||||
|
|
||||||
|
pub use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
||||||
|
|
||||||
mod parser;
|
mod parser;
|
||||||
mod show;
|
mod show;
|
||||||
|
|
|
@ -4,7 +4,8 @@ use std::error::Error;
|
||||||
use std::num::FromStrRadix;
|
use std::num::FromStrRadix;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use {Array, Table, Value, Float, Integer, Boolean, Datetime, TomlTable};
|
use {Value, TomlTable};
|
||||||
|
use Value::{Array, Table, Float, Integer, Boolean, Datetime};
|
||||||
|
|
||||||
/// Parser for converting a string to a TOML `Value` instance.
|
/// Parser for converting a string to a TOML `Value` instance.
|
||||||
///
|
///
|
||||||
|
@ -285,7 +286,7 @@ impl<'a> Parser<'a> {
|
||||||
self.eat('\n');
|
self.eat('\n');
|
||||||
} else {
|
} else {
|
||||||
// empty
|
// empty
|
||||||
return Some(::String(ret))
|
return Some(Value::String(ret))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,7 +329,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(::String(ret));
|
return Some(Value::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() {
|
||||||
|
@ -447,7 +448,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Some(::String(ret));
|
return Some(Value::String(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn number_or_datetime(&mut self, start: uint) -> Option<Value> {
|
fn number_or_datetime(&mut self, start: uint) -> Option<Value> {
|
||||||
|
@ -768,7 +769,8 @@ impl Error for ParserError {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use {Table, Parser};
|
use Value::Table;
|
||||||
|
use Parser;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn crlf() {
|
fn crlf() {
|
||||||
|
|
|
@ -4,7 +4,13 @@ use std::fmt;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
use serialize;
|
use serialize;
|
||||||
use {Value, Table, Array, Integer, Float, Boolean, Parser, TomlTable};
|
use {Value, Parser, TomlTable};
|
||||||
|
use Value::{Table, Array, Integer, Float, Boolean};
|
||||||
|
|
||||||
|
use self::EncoderState::{Start, NextKey, NextArray, NextMapKey};
|
||||||
|
use self::Error::{NeedsKey, NoValue, InvalidMapKeyLocation, InvalidMapKeyType};
|
||||||
|
use self::DecodeErrorKind::{ApplicationError, ExpectedField, ExpectedType, ExpectedMapKey};
|
||||||
|
use self::DecodeErrorKind::{ExpectedMapElement, NoEnumVariants, NilTooLong};
|
||||||
|
|
||||||
/// A structure to transform Rust values into TOML values.
|
/// A structure to transform Rust values into TOML values.
|
||||||
///
|
///
|
||||||
|
@ -142,7 +148,7 @@ impl Encoder {
|
||||||
}
|
}
|
||||||
NextMapKey => {
|
NextMapKey => {
|
||||||
match v {
|
match v {
|
||||||
::String(s) => { self.state = NextKey(s); Ok(()) }
|
Value::String(s) => { self.state = NextKey(s); Ok(()) }
|
||||||
_ => Err(InvalidMapKeyType)
|
_ => Err(InvalidMapKeyType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,7 +200,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(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 +415,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(Value::String(ref s)) if s.len() == 0 => {}
|
||||||
Some(::String(..)) => return Err(self.err(NilTooLong)),
|
Some(Value::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 +472,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(Value::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 +481,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(Value::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;
|
||||||
|
@ -680,7 +686,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(Value::String(key.to_string())),
|
||||||
key.as_slice()))
|
key.as_slice()))
|
||||||
}
|
}
|
||||||
None => Err(self.err(ExpectedMapKey(idx))),
|
None => Err(self.err(ExpectedMapKey(idx))),
|
||||||
|
@ -801,7 +807,8 @@ mod tests {
|
||||||
use serialize::{Encodable, Decodable};
|
use serialize::{Encodable, Decodable};
|
||||||
|
|
||||||
use super::{Encoder, Decoder, DecodeError};
|
use super::{Encoder, Decoder, DecodeError};
|
||||||
use {Table, Integer, Array, Float};
|
use Value;
|
||||||
|
use Value::{Table, Integer, Array, Float};
|
||||||
|
|
||||||
macro_rules! encode( ($t:expr) => ({
|
macro_rules! encode( ($t:expr) => ({
|
||||||
let mut e = Encoder::new();
|
let mut e = Encoder::new();
|
||||||
|
@ -856,7 +863,7 @@ mod tests {
|
||||||
map! {
|
map! {
|
||||||
a: Integer(2),
|
a: Integer(2),
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: ::String("test".to_string())
|
a: Value::String("test".to_string())
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
@ -877,7 +884,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(Value::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));
|
||||||
|
@ -948,12 +955,12 @@ mod tests {
|
||||||
map! {
|
map! {
|
||||||
a: Table(map! {
|
a: Table(map! {
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: ::String("foo".to_string()),
|
a: Value::String("foo".to_string()),
|
||||||
b: Float(4.5)
|
b: Float(4.5)
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
b: Table(map! {
|
b: Table(map! {
|
||||||
a: ::String("bar".to_string()),
|
a: Value::String("bar".to_string()),
|
||||||
b: Float(1.0)
|
b: Float(1.0)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -987,7 +994,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![Value::String("a".to_string())])
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
@ -1003,7 +1010,7 @@ mod tests {
|
||||||
encode!(v),
|
encode!(v),
|
||||||
map! {
|
map! {
|
||||||
_field0: Integer(1),
|
_field0: Integer(1),
|
||||||
_field1: ::String("foo".to_string()),
|
_field1: Value::String("foo".to_string()),
|
||||||
_field2: Float(4.5)
|
_field2: Float(4.5)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1081,24 +1088,24 @@ mod tests {
|
||||||
test: String,
|
test: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
let v = Foo { a: Bar(10) };
|
let v = Foo { a: E::Bar(10) };
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
encode!(v),
|
encode!(v),
|
||||||
map! { a: Integer(10) }
|
map! { a: Integer(10) }
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
|
||||||
let v = Foo { a: Baz(10.2) };
|
let v = Foo { a: E::Baz(10.2) };
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
encode!(v),
|
encode!(v),
|
||||||
map! { a: Float(10.2) }
|
map! { a: Float(10.2) }
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
|
||||||
let v = Foo { a: Last(Foo2 { test: "test".to_string() }) };
|
let v = Foo { a: E::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: Value::String("test".to_string()) }) }
|
||||||
);
|
);
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
}
|
}
|
||||||
|
@ -1169,7 +1176,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: Value::String("foo".to_string())
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
||||||
|
@ -1184,7 +1191,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![Value::String("a".to_string())])
|
||||||
}));
|
}));
|
||||||
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
assert_eq!(v, Decodable::decode(&mut d).unwrap());
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table, TomlTable};
|
use {Value, TomlTable};
|
||||||
|
use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
||||||
|
|
||||||
struct Printer<'a, 'b:'a> {
|
struct Printer<'a, 'b:'a> {
|
||||||
output: &'a mut fmt::Formatter<'b>,
|
output: &'a mut fmt::Formatter<'b>,
|
||||||
|
@ -100,7 +101,8 @@ impl<'a, 'b> Printer<'a, 'b> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[allow(warnings)]
|
#[allow(warnings)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table};
|
use Value;
|
||||||
|
use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
||||||
use std::collections::TreeMap;
|
use std::collections::TreeMap;
|
||||||
|
|
||||||
macro_rules! map( ($($k:expr: $v:expr),*) => ({
|
macro_rules! map( ($($k:expr: $v:expr),*) => ({
|
||||||
|
|
|
@ -4,7 +4,8 @@ use std::num::strconv;
|
||||||
use std::collections::TreeMap;
|
use std::collections::TreeMap;
|
||||||
use self::serialize::json;
|
use self::serialize::json;
|
||||||
|
|
||||||
use {Parser, Value, Table, Integer, Float, Boolean, Datetime, Array};
|
use {Parser, Value};
|
||||||
|
use 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 +15,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)),
|
Value::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