| 1 |
#![cfg(all(feature = "parse", feature = "display"))] |
| 2 |
#![allow(dead_code)] |
| 3 |
|
| 4 |
#[derive(Copy, Clone)] |
| 5 |
pub(crate) struct Decoder; |
| 6 |
|
| 7 |
impl toml_test_harness::Decoder for Decoder { |
| 8 |
fn name(&self) -> &str { |
| 9 |
"toml" |
| 10 |
} |
| 11 |
|
| 12 |
fn decode(&self, data: &[u8]) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> { |
| 13 |
let data = std::str::from_utf8(data).map_err(toml_test_harness::Error::new)?; |
| 14 |
let document = data |
| 15 |
.parse::<toml::Table>() |
| 16 |
.map_err(toml_test_harness::Error::new)?; |
| 17 |
let value = toml::Value::Table(document); |
| 18 |
value_to_decoded(&value) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
fn value_to_decoded( |
| 23 |
value: &toml::Value, |
| 24 |
) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> { |
| 25 |
match value { |
| 26 |
toml::Value::Integer(v) => Ok(toml_test_harness::Decoded::Value( |
| 27 |
toml_test_harness::DecodedValue::from(*v), |
| 28 |
)), |
| 29 |
toml::Value::String(v) => Ok(toml_test_harness::Decoded::Value( |
| 30 |
toml_test_harness::DecodedValue::from(v), |
| 31 |
)), |
| 32 |
toml::Value::Float(v) => Ok(toml_test_harness::Decoded::Value( |
| 33 |
toml_test_harness::DecodedValue::from(*v), |
| 34 |
)), |
| 35 |
toml::Value::Datetime(v) => { |
| 36 |
let value = v.to_string(); |
| 37 |
let value = match (v.date.is_some(), v.time.is_some(), v.offset.is_some()) { |
| 38 |
(true, true, true) => toml_test_harness::DecodedValue::Datetime(value), |
| 39 |
(true, true, false) => toml_test_harness::DecodedValue::DatetimeLocal(value), |
| 40 |
(true, false, false) => toml_test_harness::DecodedValue::DateLocal(value), |
| 41 |
(false, true, false) => toml_test_harness::DecodedValue::TimeLocal(value), |
| 42 |
_ => unreachable!("Unsupported case"), |
| 43 |
}; |
| 44 |
Ok(toml_test_harness::Decoded::Value(value)) |
| 45 |
} |
| 46 |
toml::Value::Boolean(v) => Ok(toml_test_harness::Decoded::Value( |
| 47 |
toml_test_harness::DecodedValue::from(*v), |
| 48 |
)), |
| 49 |
toml::Value::Array(v) => { |
| 50 |
let v: Result<_, toml_test_harness::Error> = v.iter().map(value_to_decoded).collect(); |
| 51 |
Ok(toml_test_harness::Decoded::Array(v?)) |
| 52 |
} |
| 53 |
toml::Value::Table(v) => table_to_decoded(v), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
fn table_to_decoded( |
| 58 |
value: &toml::value::Table, |
| 59 |
) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> { |
| 60 |
let table: Result<_, toml_test_harness::Error> = value |
| 61 |
.iter() |
| 62 |
.map(|(k, v)| { |
| 63 |
let k = k.to_owned(); |
| 64 |
let v = value_to_decoded(v)?; |
| 65 |
Ok((k, v)) |
| 66 |
}) |
| 67 |
.collect(); |
| 68 |
Ok(toml_test_harness::Decoded::Table(table?)) |
| 69 |
} |