1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
use std::io::{Read, Write}; use std::path::Path; use serde::{Deserialize, Serialize}; use toml; use Result; /// This trait allows to convert TOML objects to deserializable values. /// /// # Examples /// /// ``` /// extern crate serde; /// #[macro_use] /// extern crate serde_derive; /// extern crate tomlconv; /// /// use tomlconv::FromToml; /// /// // Defines a deserializable struct. /// #[derive(Deserialize)] /// struct Foo { /// bar: String, /// baz: usize /// } /// impl FromToml for Foo {} /// /// # fn main() { /// // Converts from the TOML string to a `Foo` value. /// let toml = r#" /// bar = "aaa" /// baz = 123 /// "#; /// let foo = Foo::from_toml_str(toml).unwrap(); /// assert_eq!(foo.bar, "aaa"); /// assert_eq!(foo.baz, 123); /// # } /// ``` pub trait FromToml: for<'a> Deserialize<'a> { /// Converts from the TOML file to an instance of this implementation. fn from_toml_file<P: AsRef<Path>>(path: P) -> Result<Self> { track!(::from_toml_file(path)) } /// Reads a TOML string from the reader and converts it to an instance of this implementation. fn from_toml_reader<R: Read>(reader: R) -> Result<Self> { track!(::from_toml_reader(reader)) } /// Converts from the TOML string to an instance of this implementation. fn from_toml_str(toml: &str) -> Result<Self> { track!(::from_toml_str(toml)) } /// Converts from the TOML value to an instance of this implementation. fn from_toml(toml: toml::Value) -> Result<Self> { track!(::from_toml(toml)) } } /// This trait allows to convert serializable values to TOML objects. /// /// # Examples /// /// ``` /// extern crate serde; /// #[macro_use] /// extern crate serde_derive; /// extern crate tomlconv; /// /// use tomlconv::ToToml; /// /// // Defines a serializable struct. /// #[derive(Serialize)] /// struct Foo { /// bar: &'static str, /// baz: usize /// } /// impl ToToml for Foo {} /// /// # fn main() { /// // Converts the `Foo` value to a TOML string. /// let foo = Foo { bar: "aaa", baz: 123 }; /// let toml = foo.to_toml_string().unwrap(); /// assert_eq!(toml, "\ /// bar = \"aaa\" /// baz = 123 /// "); /// # } /// ``` pub trait ToToml: Serialize { /// Converts this to a TOML string and writes it to the speficied file. fn to_toml_file<P: AsRef<Path>>(&self, path: P) -> Result<()> { track!(::to_toml_file(self, path)) } /// Converts this to a TOML string and writes it to the writer. fn to_toml_writer<W: Write>(&self, writer: W) -> Result<()> { track!(::to_toml_writer(self, writer)) } /// Converts this to a TOML string. fn to_toml_string(&self) -> Result<String> { track!(::to_toml_string(self)) } /// Converts this to a TOML value. fn to_toml(&self) -> Result<toml::Value> { track!(::to_toml(self)) } }