diff --git a/src/serialization.rs b/src/serialization.rs index 515e8dd..ef2ac88 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -1,10 +1,8 @@ -#![allow(warnings)] - use std::collections::HashMap; use std::mem; use serialize; -use {Value, Table, Array, String, Integer, Float, Boolean}; +use {Value, Table, Array, String, Integer, Float, Boolean, Parser}; /// A structure to transform Rust values into TOML values. /// @@ -43,6 +41,11 @@ pub struct Encoder { state: EncoderState, } +/// A structure to transform TOML values into Rust values. +/// +/// This decoder implements the serialization `Decoder` interface, allowing +/// `Decodable` types to be generated by this decoder. The input is any +/// arbitrary TOML value. pub struct Decoder { toml: Option, } @@ -313,7 +316,35 @@ impl serialize::Encoder for Encoder { } } +/// Decodes a TOML value into a decodable type. +/// +/// This function will consume the given TOML value and attempt to decode it +/// into the type specified. If decoding fails, `None` will be returned. If a +/// finer-grained error is desired, then it is recommended to use `Decodable` +/// directly. +pub fn decode>(toml: Value) + -> Option +{ + serialize::Decodable::decode(&mut Decoder::new(toml)).ok() +} + +/// Decodes a string into a toml-encoded value. +/// +/// This function will parse the given string into a TOML value, and then parse +/// the TOML value into the desired type. If any error occurs `None` is return. +/// If more fine-grained errors are desired, these steps should be driven +/// manually. +pub fn decode_str>(s: &str) + -> Option +{ + Parser::new(s).parse().and_then(|t| decode(Table(t))) +} + impl Decoder { + /// Creates a new decoder, consuming the TOML value to decode. + /// + /// This decoder can be passed to the `Decodable` methods or driven + /// manually. pub fn new(toml: Value) -> Decoder { Decoder { toml: Some(toml) } } @@ -389,34 +420,34 @@ impl serialize::Decoder for Decoder { } // Compound types: - fn read_enum(&mut self, name: &str, - f: |&mut Decoder| -> Result) -> Result { + fn read_enum(&mut self, _name: &str, + _f: |&mut Decoder| -> Result) -> Result { fail!() } fn read_enum_variant(&mut self, - names: &[&str], - f: |&mut Decoder, uint| -> Result) + _names: &[&str], + _f: |&mut Decoder, uint| -> Result) -> Result { fail!() } fn read_enum_variant_arg(&mut self, - a_idx: uint, - f: |&mut Decoder| -> Result) + _a_idx: uint, + _f: |&mut Decoder| -> Result) -> Result { fail!() } fn read_enum_struct_variant(&mut self, - names: &[&str], - f: |&mut Decoder, uint| -> Result) + _names: &[&str], + _f: |&mut Decoder, uint| -> Result) -> Result { fail!() } fn read_enum_struct_variant_field(&mut self, - f_name: &str, - f_idx: uint, - f: |&mut Decoder| -> Result) + _f_name: &str, + _f_idx: uint, + _f: |&mut Decoder| -> Result) -> Result { fail!() } @@ -432,7 +463,7 @@ impl serialize::Decoder for Decoder { } fn read_struct_field(&mut self, f_name: &str, - f_idx: uint, + _f_idx: uint, f: |&mut Decoder| -> Result) -> Result { match self.toml { @@ -460,15 +491,15 @@ impl serialize::Decoder for Decoder { } fn read_tuple_struct(&mut self, - s_name: &str, - f: |&mut Decoder, uint| -> Result) + _s_name: &str, + _f: |&mut Decoder, uint| -> Result) -> Result { fail!() } fn read_tuple_struct_arg(&mut self, - a_idx: uint, - f: |&mut Decoder| -> Result) + _a_idx: uint, + _f: |&mut Decoder| -> Result) -> Result { fail!() diff --git a/src/toml.rs b/src/toml.rs index 7c75085..33be0f8 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -47,7 +47,7 @@ use std::from_str::FromStr; pub use parser::{Parser, Error}; pub use serialization::{Encoder, encode, encode_str}; -// pub use serialization::{Encoder, encode, encode_str}; +pub use serialization::{Decoder, decode, decode_str}; pub use serialization::{Error, NeedsKey, NoValue}; pub use serialization::{InvalidMapKeyLocation, InvalidMapKeyType};