parent
b6e487e05b
commit
6e4e8251b1
56
src/lib.rs
56
src/lib.rs
|
@ -58,8 +58,6 @@ pub use serialization::DecodeErrorKind::{ApplicationError, ExpectedField};
|
||||||
pub use serialization::DecodeErrorKind::{ExpectedMapElement, ExpectedMapKey, NoEnumVariants};
|
pub use serialization::DecodeErrorKind::{ExpectedMapElement, ExpectedMapKey, NoEnumVariants};
|
||||||
pub use serialization::DecodeErrorKind::{ExpectedType, NilTooLong};
|
pub use serialization::DecodeErrorKind::{ExpectedType, NilTooLong};
|
||||||
|
|
||||||
pub use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
|
||||||
|
|
||||||
mod parser;
|
mod parser;
|
||||||
mod show;
|
mod show;
|
||||||
mod serialization;
|
mod serialization;
|
||||||
|
@ -73,8 +71,8 @@ pub enum Value {
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Datetime(string::String),
|
Datetime(string::String),
|
||||||
Array(TomlArray),
|
Array(Array),
|
||||||
Table(TomlTable),
|
Table(Table),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type representing a TOML array, payload of the Value::Array variant
|
/// Type representing a TOML array, payload of the Value::Array variant
|
||||||
|
@ -87,13 +85,13 @@ impl Value {
|
||||||
/// Tests whether this and another value have the same type.
|
/// Tests whether this and another value have the same type.
|
||||||
pub fn same_type(&self, other: &Value) -> bool {
|
pub fn same_type(&self, other: &Value) -> bool {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&String(..), &String(..)) |
|
(&Value::String(..), &Value::String(..)) |
|
||||||
(&Integer(..), &Integer(..)) |
|
(&Value::Integer(..), &Value::Integer(..)) |
|
||||||
(&Float(..), &Float(..)) |
|
(&Value::Float(..), &Value::Float(..)) |
|
||||||
(&Boolean(..), &Boolean(..)) |
|
(&Value::Boolean(..), &Value::Boolean(..)) |
|
||||||
(&Datetime(..), &Datetime(..)) |
|
(&Value::Datetime(..), &Value::Datetime(..)) |
|
||||||
(&Array(..), &Array(..)) |
|
(&Value::Array(..), &Value::Array(..)) |
|
||||||
(&Table(..), &Table(..)) => true,
|
(&Value::Table(..), &Value::Table(..)) => true,
|
||||||
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
@ -102,34 +100,34 @@ impl Value {
|
||||||
/// Returns a human-readable representation of the type of this value.
|
/// Returns a human-readable representation of the type of this value.
|
||||||
pub fn type_str(&self) -> &'static str {
|
pub fn type_str(&self) -> &'static str {
|
||||||
match *self {
|
match *self {
|
||||||
String(..) => "string",
|
Value::String(..) => "string",
|
||||||
Integer(..) => "integer",
|
Value::Integer(..) => "integer",
|
||||||
Float(..) => "float",
|
Value::Float(..) => "float",
|
||||||
Boolean(..) => "boolean",
|
Value::Boolean(..) => "boolean",
|
||||||
Datetime(..) => "datetime",
|
Value::Datetime(..) => "datetime",
|
||||||
Array(..) => "array",
|
Value::Array(..) => "array",
|
||||||
Table(..) => "table",
|
Value::Table(..) => "table",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 { String(ref s) => Some(s.as_slice()), _ => None }
|
match *self { Value::String(ref s) => Some(s.as_slice()), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the integer value if it is an integer.
|
/// Extracts the integer value if it is an integer.
|
||||||
pub fn as_integer(&self) -> Option<i64> {
|
pub fn as_integer(&self) -> Option<i64> {
|
||||||
match *self { Integer(i) => Some(i), _ => None }
|
match *self { Value::Integer(i) => Some(i), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the float value if it is a float.
|
/// Extracts the float value if it is a float.
|
||||||
pub fn as_float(&self) -> Option<f64> {
|
pub fn as_float(&self) -> Option<f64> {
|
||||||
match *self { Float(f) => Some(f), _ => None }
|
match *self { Value::Float(f) => Some(f), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the boolean value if it is a boolean.
|
/// Extracts the boolean value if it is a boolean.
|
||||||
pub fn as_bool(&self) -> Option<bool> {
|
pub fn as_bool(&self) -> Option<bool> {
|
||||||
match *self { Boolean(b) => Some(b), _ => None }
|
match *self { Value::Boolean(b) => Some(b), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts the datetime value if it is a datetime.
|
/// Extracts the datetime value if it is a datetime.
|
||||||
|
@ -141,17 +139,17 @@ 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 { Datetime(ref s) => Some(s.as_slice()), _ => None }
|
match *self { Value::Datetime(ref s) => Some(s.as_slice()), _ => 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 { Array(ref s) => Some(s.as_slice()), _ => None }
|
match *self { Value::Array(ref s) => Some(s.as_slice()), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 TomlTable> {
|
pub fn as_table<'a>(&'a self) -> Option<&'a Table> {
|
||||||
match *self { Table(ref s) => Some(s), _ => None }
|
match *self { Value::Table(ref s) => Some(s), _ => None }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lookups for value at specified path.
|
/// Lookups for value at specified path.
|
||||||
|
@ -186,13 +184,13 @@ impl Value {
|
||||||
let mut cur_value = self;
|
let mut cur_value = self;
|
||||||
for key in path.split('.') {
|
for key in path.split('.') {
|
||||||
match cur_value {
|
match cur_value {
|
||||||
&Table(ref hm) => {
|
&Value::Table(ref hm) => {
|
||||||
match hm.find_with(|k| key.cmp(k.as_slice())) {
|
match hm.find_with(|k| key.cmp(k.as_slice())) {
|
||||||
Some(v) => cur_value = v,
|
Some(v) => cur_value = v,
|
||||||
_ => return None
|
_ => return None
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
&Array(ref v) => {
|
&Value::Array(ref v) => {
|
||||||
let idx: Option<uint> = FromStr::from_str(key);
|
let idx: Option<uint> = FromStr::from_str(key);
|
||||||
match idx {
|
match idx {
|
||||||
Some(idx) if idx < v.len() => cur_value = &v[idx],
|
Some(idx) if idx < v.len() => cur_value = &v[idx],
|
||||||
|
@ -209,7 +207,7 @@ impl Value {
|
||||||
|
|
||||||
impl FromStr for Value {
|
impl FromStr for Value {
|
||||||
fn from_str(s: &str) -> Option<Value> {
|
fn from_str(s: &str) -> Option<Value> {
|
||||||
Parser::new(s).parse().map(Table)
|
Parser::new(s).parse().map(Value::Table)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ use std::error::Error;
|
||||||
use std::num::FromStrRadix;
|
use std::num::FromStrRadix;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use {Value, TomlTable};
|
use Table as TomlTable;
|
||||||
use Value::{Array, Table, Float, Integer, Boolean, Datetime};
|
use Value::{mod, 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.
|
||||||
///
|
///
|
||||||
|
|
|
@ -4,7 +4,8 @@ use std::fmt;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
use serialize;
|
use serialize;
|
||||||
use {Value, Parser, TomlTable};
|
use {Value, Parser};
|
||||||
|
use Table as TomlTable;
|
||||||
use Value::{Table, Array, Integer, Float, Boolean};
|
use Value::{Table, Array, Integer, Float, Boolean};
|
||||||
|
|
||||||
use self::EncoderState::{Start, NextKey, NextArray, NextMapKey};
|
use self::EncoderState::{Start, NextKey, NextArray, NextMapKey};
|
||||||
|
@ -26,7 +27,7 @@ use self::DecodeErrorKind::{ExpectedMapElement, NoEnumVariants, NilTooLong};
|
||||||
/// extern crate toml;
|
/// extern crate toml;
|
||||||
///
|
///
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// use toml::{Encoder, Integer};
|
/// use toml::{Encoder, Value};
|
||||||
/// use serialize::Encodable;
|
/// use serialize::Encodable;
|
||||||
///
|
///
|
||||||
/// #[deriving(Encodable)]
|
/// #[deriving(Encodable)]
|
||||||
|
@ -36,7 +37,7 @@ use self::DecodeErrorKind::{ExpectedMapElement, NoEnumVariants, NilTooLong};
|
||||||
/// let mut e = Encoder::new();
|
/// let mut e = Encoder::new();
|
||||||
/// my_struct.encode(&mut e).unwrap();
|
/// my_struct.encode(&mut e).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Integer(4)))
|
/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4)))
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Encoder {
|
pub struct Encoder {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use {Value, TomlTable};
|
use Table as TomlTable;
|
||||||
use Value::{String, Integer, Float, Boolean, Datetime, Array, Table};
|
use Value::{mod, 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>,
|
||||||
|
|
Loading…
Reference in a new issue