Clean up warnings and documentation

This commit is contained in:
Alex Crichton 2014-06-22 22:31:09 -07:00
parent f06fae1602
commit 16d5e67fa5
2 changed files with 51 additions and 20 deletions

View file

@ -1,10 +1,8 @@
#![allow(warnings)]
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
use serialize; 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. /// A structure to transform Rust values into TOML values.
/// ///
@ -43,6 +41,11 @@ pub struct Encoder {
state: EncoderState, 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 { pub struct Decoder {
toml: Option<Value>, toml: Option<Value>,
} }
@ -313,7 +316,35 @@ impl serialize::Encoder<Error> 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<T: serialize::Decodable<Decoder, Error>>(toml: Value)
-> Option<T>
{
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<T: serialize::Decodable<Decoder, Error>>(s: &str)
-> Option<T>
{
Parser::new(s).parse().and_then(|t| decode(Table(t)))
}
impl Decoder { 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 { pub fn new(toml: Value) -> Decoder {
Decoder { toml: Some(toml) } Decoder { toml: Some(toml) }
} }
@ -389,34 +420,34 @@ impl serialize::Decoder<Error> for Decoder {
} }
// Compound types: // Compound types:
fn read_enum<T>(&mut self, name: &str, fn read_enum<T>(&mut self, _name: &str,
f: |&mut Decoder| -> Result<T, Error>) -> Result<T, Error> { _f: |&mut Decoder| -> Result<T, Error>) -> Result<T, Error> {
fail!() fail!()
} }
fn read_enum_variant<T>(&mut self, fn read_enum_variant<T>(&mut self,
names: &[&str], _names: &[&str],
f: |&mut Decoder, uint| -> Result<T, Error>) _f: |&mut Decoder, uint| -> Result<T, Error>)
-> Result<T, Error> { -> Result<T, Error> {
fail!() fail!()
} }
fn read_enum_variant_arg<T>(&mut self, fn read_enum_variant_arg<T>(&mut self,
a_idx: uint, _a_idx: uint,
f: |&mut Decoder| -> Result<T, Error>) _f: |&mut Decoder| -> Result<T, Error>)
-> Result<T, Error> { -> Result<T, Error> {
fail!() fail!()
} }
fn read_enum_struct_variant<T>(&mut self, fn read_enum_struct_variant<T>(&mut self,
names: &[&str], _names: &[&str],
f: |&mut Decoder, uint| -> Result<T, Error>) _f: |&mut Decoder, uint| -> Result<T, Error>)
-> Result<T, Error> { -> Result<T, Error> {
fail!() fail!()
} }
fn read_enum_struct_variant_field<T>(&mut self, fn read_enum_struct_variant_field<T>(&mut self,
f_name: &str, _f_name: &str,
f_idx: uint, _f_idx: uint,
f: |&mut Decoder| -> Result<T, Error>) _f: |&mut Decoder| -> Result<T, Error>)
-> Result<T, Error> { -> Result<T, Error> {
fail!() fail!()
} }
@ -432,7 +463,7 @@ impl serialize::Decoder<Error> for Decoder {
} }
fn read_struct_field<T>(&mut self, fn read_struct_field<T>(&mut self,
f_name: &str, f_name: &str,
f_idx: uint, _f_idx: uint,
f: |&mut Decoder| -> Result<T, Error>) f: |&mut Decoder| -> Result<T, Error>)
-> Result<T, Error> { -> Result<T, Error> {
match self.toml { match self.toml {
@ -460,15 +491,15 @@ impl serialize::Decoder<Error> for Decoder {
} }
fn read_tuple_struct<T>(&mut self, fn read_tuple_struct<T>(&mut self,
s_name: &str, _s_name: &str,
f: |&mut Decoder, uint| -> Result<T, Error>) _f: |&mut Decoder, uint| -> Result<T, Error>)
-> Result<T, Error> -> Result<T, Error>
{ {
fail!() fail!()
} }
fn read_tuple_struct_arg<T>(&mut self, fn read_tuple_struct_arg<T>(&mut self,
a_idx: uint, _a_idx: uint,
f: |&mut Decoder| -> Result<T, Error>) _f: |&mut Decoder| -> Result<T, Error>)
-> Result<T, Error> -> Result<T, Error>
{ {
fail!() fail!()

View file

@ -47,7 +47,7 @@ use std::from_str::FromStr;
pub use parser::{Parser, Error}; pub use parser::{Parser, Error};
pub use serialization::{Encoder, encode, encode_str}; 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::{Error, NeedsKey, NoValue};
pub use serialization::{InvalidMapKeyLocation, InvalidMapKeyType}; pub use serialization::{InvalidMapKeyLocation, InvalidMapKeyType};