diff --git a/src/easy/datetime.rs b/src/easy/datetime.rs index 7952dd61..57dee1e9 100644 --- a/src/easy/datetime.rs +++ b/src/easy/datetime.rs @@ -18,6 +18,17 @@ pub enum Datetime { LocalTime(LocalTime), } +impl std::fmt::Display for Datetime { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + Datetime::OffsetDateTime(v) => v.fmt(f), + Datetime::LocalDateTime(v) => v.fmt(f), + Datetime::LocalDate(v) => v.fmt(f), + Datetime::LocalTime(v) => v.fmt(f), + } + } +} + mod string { use std::fmt::Display; use std::str::FromStr; diff --git a/tests/easy_decoder.rs b/tests/easy_decoder.rs new file mode 100644 index 00000000..fdc0bfb8 --- /dev/null +++ b/tests/easy_decoder.rs @@ -0,0 +1,58 @@ +#![cfg(feature = "easy")] + +pub struct Decoder; + +impl toml_test_harness::Decoder for Decoder { + fn name(&self) -> &str { + "toml_edit" + } + + fn decode(&self, data: &[u8]) -> Result { + let data = std::str::from_utf8(data).map_err(toml_test_harness::Error::new)?; + let document = data + .parse::() + .map_err(toml_test_harness::Error::new)?; + value_to_decoded(&document) + } +} + +fn value_to_decoded( + value: &toml_edit::easy::Value, +) -> Result { + match value { + toml_edit::easy::Value::Integer(v) => Ok(toml_test_harness::Decoded::Value( + toml_test_harness::DecodedValue::from(*v), + )), + toml_edit::easy::Value::String(v) => Ok(toml_test_harness::Decoded::Value( + toml_test_harness::DecodedValue::from(v), + )), + toml_edit::easy::Value::Float(v) => Ok(toml_test_harness::Decoded::Value( + toml_test_harness::DecodedValue::from(*v), + )), + toml_edit::easy::Value::Datetime(v) => Ok(toml_test_harness::Decoded::Value( + toml_test_harness::DecodedValue::Datetime(v.to_string()), + )), + toml_edit::easy::Value::Boolean(v) => Ok(toml_test_harness::Decoded::Value( + toml_test_harness::DecodedValue::from(*v), + )), + toml_edit::easy::Value::Array(v) => { + let v: Result<_, toml_test_harness::Error> = v.iter().map(value_to_decoded).collect(); + Ok(toml_test_harness::Decoded::Array(v?)) + } + toml_edit::easy::Value::Table(v) => table_to_decoded(v), + } +} + +fn table_to_decoded( + value: &toml_edit::easy::value::Table, +) -> Result { + let table: Result<_, toml_test_harness::Error> = value + .iter() + .map(|(k, v)| { + let k = k.to_owned(); + let v = value_to_decoded(v)?; + Ok((k, v)) + }) + .collect(); + Ok(toml_test_harness::Decoded::Table(table?)) +} diff --git a/tests/easy_decoder_compliance.rs b/tests/easy_decoder_compliance.rs new file mode 100644 index 00000000..ab2ecc34 --- /dev/null +++ b/tests/easy_decoder_compliance.rs @@ -0,0 +1,12 @@ +#![cfg(feature = "easy")] + +mod easy_decoder; + +fn main() { + let decoder = easy_decoder::Decoder; + let mut harness = toml_test_harness::DecoderHarness::new(decoder); + harness + .ignore(["valid/string/multiline-quotes.toml"]) + .unwrap(); + harness.test(); +}