1 #![cfg(all(feature = "parse", feature = "display"))]
2
3 #[derive(Copy, Clone)]
4 pub struct Encoder;
5
6 impl toml_test_harness::Encoder for Encoder {
name(&self) -> &str7 fn name(&self) -> &str {
8 "toml"
9 }
10
encode(&self, data: toml_test_harness::Decoded) -> Result<String, toml_test_harness::Error>11 fn encode(&self, data: toml_test_harness::Decoded) -> Result<String, toml_test_harness::Error> {
12 let value = from_decoded(&data)?;
13 let toml::Value::Table(document) = value else {
14 return Err(toml_test_harness::Error::new("no root table"));
15 };
16 let s = toml::to_string(&document).map_err(toml_test_harness::Error::new)?;
17 Ok(s)
18 }
19 }
20
from_decoded( decoded: &toml_test_harness::Decoded, ) -> Result<toml::Value, toml_test_harness::Error>21 fn from_decoded(
22 decoded: &toml_test_harness::Decoded,
23 ) -> Result<toml::Value, toml_test_harness::Error> {
24 let value = match decoded {
25 toml_test_harness::Decoded::Value(value) => from_decoded_value(value)?,
26 toml_test_harness::Decoded::Table(value) => toml::Value::Table(from_table(value)?),
27 toml_test_harness::Decoded::Array(value) => toml::Value::Array(from_array(value)?),
28 };
29 Ok(value)
30 }
31
from_decoded_value( decoded: &toml_test_harness::DecodedValue, ) -> Result<toml::Value, toml_test_harness::Error>32 fn from_decoded_value(
33 decoded: &toml_test_harness::DecodedValue,
34 ) -> Result<toml::Value, toml_test_harness::Error> {
35 match decoded {
36 toml_test_harness::DecodedValue::String(value) => Ok(toml::Value::String(value.clone())),
37 toml_test_harness::DecodedValue::Integer(value) => value
38 .parse::<i64>()
39 .map_err(toml_test_harness::Error::new)
40 .map(toml::Value::Integer),
41 toml_test_harness::DecodedValue::Float(value) => value
42 .parse::<f64>()
43 .map_err(toml_test_harness::Error::new)
44 .map(toml::Value::Float),
45 toml_test_harness::DecodedValue::Bool(value) => value
46 .parse::<bool>()
47 .map_err(toml_test_harness::Error::new)
48 .map(toml::Value::Boolean),
49 toml_test_harness::DecodedValue::Datetime(value) => value
50 .parse::<toml::value::Datetime>()
51 .map_err(toml_test_harness::Error::new)
52 .map(toml::Value::Datetime),
53 toml_test_harness::DecodedValue::DatetimeLocal(value) => value
54 .parse::<toml::value::Datetime>()
55 .map_err(toml_test_harness::Error::new)
56 .map(toml::Value::Datetime),
57 toml_test_harness::DecodedValue::DateLocal(value) => value
58 .parse::<toml::value::Datetime>()
59 .map_err(toml_test_harness::Error::new)
60 .map(toml::Value::Datetime),
61 toml_test_harness::DecodedValue::TimeLocal(value) => value
62 .parse::<toml::value::Datetime>()
63 .map_err(toml_test_harness::Error::new)
64 .map(toml::Value::Datetime),
65 }
66 }
67
from_table( decoded: &std::collections::HashMap<String, toml_test_harness::Decoded>, ) -> Result<toml::value::Table, toml_test_harness::Error>68 fn from_table(
69 decoded: &std::collections::HashMap<String, toml_test_harness::Decoded>,
70 ) -> Result<toml::value::Table, toml_test_harness::Error> {
71 decoded
72 .iter()
73 .map(|(k, v)| {
74 let v = from_decoded(v)?;
75 Ok((k.to_owned(), v))
76 })
77 .collect()
78 }
79
from_array( decoded: &[toml_test_harness::Decoded], ) -> Result<toml::value::Array, toml_test_harness::Error>80 fn from_array(
81 decoded: &[toml_test_harness::Decoded],
82 ) -> Result<toml::value::Array, toml_test_harness::Error> {
83 decoded.iter().map(from_decoded).collect()
84 }
85