1 #[derive(Copy, Clone)]
2 pub struct Encoder;
3 
4 impl toml_test_harness::Encoder for Encoder {
name(&self) -> &str5     fn name(&self) -> &str {
6         "toml_edit"
7     }
8 
encode(&self, data: toml_test_harness::Decoded) -> Result<String, toml_test_harness::Error>9     fn encode(&self, data: toml_test_harness::Decoded) -> Result<String, toml_test_harness::Error> {
10         let doc = decoded_to_document(&data)?;
11         Ok(doc.to_string())
12     }
13 }
14 
decoded_to_document( decoded: &toml_test_harness::Decoded, ) -> Result<toml_edit::Document, toml_test_harness::Error>15 fn decoded_to_document(
16     decoded: &toml_test_harness::Decoded,
17 ) -> Result<toml_edit::Document, toml_test_harness::Error> {
18     let item = root_from_decoded(decoded)?;
19     let mut doc = toml_edit::Document::new();
20     *doc = item;
21     Ok(doc)
22 }
23 
root_from_decoded( decoded: &toml_test_harness::Decoded, ) -> Result<toml_edit::Table, toml_test_harness::Error>24 fn root_from_decoded(
25     decoded: &toml_test_harness::Decoded,
26 ) -> Result<toml_edit::Table, toml_test_harness::Error> {
27     match decoded {
28         toml_test_harness::Decoded::Value(_) => {
29             Err(toml_test_harness::Error::new("Root cannot be a value"))
30         }
31         toml_test_harness::Decoded::Table(value) => value
32             .iter()
33             .map(|(k, v)| {
34                 let k = k.as_str();
35                 let v = from_decoded(v)?;
36                 Ok((k, v))
37             })
38             .collect(),
39         toml_test_harness::Decoded::Array(_) => {
40             Err(toml_test_harness::Error::new("Root cannot be an array"))
41         }
42     }
43 }
44 
from_decoded( decoded: &toml_test_harness::Decoded, ) -> Result<toml_edit::Value, toml_test_harness::Error>45 fn from_decoded(
46     decoded: &toml_test_harness::Decoded,
47 ) -> Result<toml_edit::Value, toml_test_harness::Error> {
48     let value = match decoded {
49         toml_test_harness::Decoded::Value(value) => from_decoded_value(value)?,
50         toml_test_harness::Decoded::Table(value) => {
51             toml_edit::Value::InlineTable(from_table(value)?)
52         }
53         toml_test_harness::Decoded::Array(value) => toml_edit::Value::Array(from_array(value)?),
54     };
55     Ok(value)
56 }
57 
from_decoded_value( decoded: &toml_test_harness::DecodedValue, ) -> Result<toml_edit::Value, toml_test_harness::Error>58 fn from_decoded_value(
59     decoded: &toml_test_harness::DecodedValue,
60 ) -> Result<toml_edit::Value, toml_test_harness::Error> {
61     let value: toml_edit::Value = match decoded {
62         toml_test_harness::DecodedValue::String(value) => value.into(),
63         toml_test_harness::DecodedValue::Integer(value) => value
64             .parse::<i64>()
65             .map_err(toml_test_harness::Error::new)?
66             .into(),
67         toml_test_harness::DecodedValue::Float(value) => value
68             .parse::<f64>()
69             .map_err(toml_test_harness::Error::new)?
70             .into(),
71         toml_test_harness::DecodedValue::Bool(value) => value
72             .parse::<bool>()
73             .map_err(toml_test_harness::Error::new)?
74             .into(),
75         toml_test_harness::DecodedValue::Datetime(value) => value
76             .parse::<toml_edit::Datetime>()
77             .map_err(toml_test_harness::Error::new)?
78             .into(),
79         toml_test_harness::DecodedValue::DatetimeLocal(value) => value
80             .parse::<toml_edit::Datetime>()
81             .map_err(toml_test_harness::Error::new)?
82             .into(),
83         toml_test_harness::DecodedValue::DateLocal(value) => value
84             .parse::<toml_edit::Datetime>()
85             .map_err(toml_test_harness::Error::new)?
86             .into(),
87         toml_test_harness::DecodedValue::TimeLocal(value) => value
88             .parse::<toml_edit::Datetime>()
89             .map_err(toml_test_harness::Error::new)?
90             .into(),
91     };
92     Ok(value)
93 }
94 
from_table( decoded: &std::collections::HashMap<String, toml_test_harness::Decoded>, ) -> Result<toml_edit::InlineTable, toml_test_harness::Error>95 fn from_table(
96     decoded: &std::collections::HashMap<String, toml_test_harness::Decoded>,
97 ) -> Result<toml_edit::InlineTable, toml_test_harness::Error> {
98     decoded
99         .iter()
100         .map(|(k, v)| {
101             let v = from_decoded(v)?;
102             Ok((k, v))
103         })
104         .collect()
105 }
106 
from_array( decoded: &[toml_test_harness::Decoded], ) -> Result<toml_edit::Array, toml_test_harness::Error>107 fn from_array(
108     decoded: &[toml_test_harness::Decoded],
109 ) -> Result<toml_edit::Array, toml_test_harness::Error> {
110     decoded.iter().map(from_decoded).collect()
111 }
112