Skip to content

Commit

Permalink
test: Verify easy's decoder (toml-rs#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
epage authored Sep 8, 2021
1 parent 927037d commit 613b5ff
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/easy/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions tests/easy_decoder.rs
Original file line number Diff line number Diff line change
@@ -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<toml_test_harness::Decoded, toml_test_harness::Error> {
let data = std::str::from_utf8(data).map_err(toml_test_harness::Error::new)?;
let document = data
.parse::<toml_edit::easy::Value>()
.map_err(toml_test_harness::Error::new)?;
value_to_decoded(&document)
}
}

fn value_to_decoded(
value: &toml_edit::easy::Value,
) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> {
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<toml_test_harness::Decoded, toml_test_harness::Error> {
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?))
}
12 changes: 12 additions & 0 deletions tests/easy_decoder_compliance.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 613b5ff

Please sign in to comment.