From f06fae1602e857975ba3b3157ef103d38175c8db Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 21 Jun 2014 23:35:49 -0700 Subject: Implement Encoder/Decoder for libserialize traits --- src/toml.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/toml.rs') diff --git a/src/toml.rs b/src/toml.rs index 74a4d36..7c75085 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -18,23 +18,46 @@ //! println!("{}", value); //! ``` //! +//! # Conversions +//! +//! This library also supports using the standard `Encodable` and `Decodable` +//! traits with TOML values. This library provides the following conversion +//! capabilities: +//! +//! * `String` => `toml::Value` - via `Parser` +//! * `toml::Value` => `String` - via `Show` +//! * `toml::Value` => rust object - via `Decoder` +//! * rust object => `toml::Value` - via `Encoder` +//! +//! Convenience functions for performing multiple conversions at a time are also +//! provided. +//! //! [1]: https://github.com/mojombo/toml //! [2]: https://github.com/BurntSushi/toml-test #![crate_type = "lib"] #![feature(macro_rules)] #![deny(warnings, missing_doc)] +#![allow(visible_private_types)] + +extern crate serialize; use std::collections::HashMap; +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::{Error, NeedsKey, NoValue}; +pub use serialization::{InvalidMapKeyLocation, InvalidMapKeyType}; mod parser; -#[cfg(test)] -mod test; +mod show; +mod serialization; +#[cfg(test)] mod test; /// Representation of a TOML value. -#[deriving(Show, PartialEq, Clone)] +#[deriving(PartialEq, Clone)] #[allow(missing_doc)] pub enum Value { String(String), @@ -120,3 +143,9 @@ impl Value { match *self { Table(ref s) => Some(s), _ => None } } } + +impl FromStr for Value { + fn from_str(s: &str) -> Option { + Parser::new(s).parse().map(Table) + } +} -- cgit v1.2.3