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