summaryrefslogtreecommitdiffstats
path: root/vendor/toml_edit-0.19.11/tests/decoder.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/toml_edit-0.19.11/tests/decoder.rs
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/toml_edit-0.19.11/tests/decoder.rs')
-rw-r--r--vendor/toml_edit-0.19.11/tests/decoder.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/vendor/toml_edit-0.19.11/tests/decoder.rs b/vendor/toml_edit-0.19.11/tests/decoder.rs
new file mode 100644
index 000000000..7306d4557
--- /dev/null
+++ b/vendor/toml_edit-0.19.11/tests/decoder.rs
@@ -0,0 +1,100 @@
+#[derive(Copy, Clone)]
+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::Document>()
+ .map_err(toml_test_harness::Error::new)?;
+ document_to_decoded(&document)
+ }
+}
+
+fn document_to_decoded(
+ value: &toml_edit::Document,
+) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> {
+ table_to_decoded(value)
+}
+
+fn item_to_decoded(
+ value: &toml_edit::Item,
+) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> {
+ match value {
+ toml_edit::Item::None => unreachable!("No nones"),
+ toml_edit::Item::Value(v) => value_to_decoded(v),
+ toml_edit::Item::Table(v) => table_to_decoded(v),
+ toml_edit::Item::ArrayOfTables(v) => {
+ let v: Result<_, toml_test_harness::Error> = v.iter().map(table_to_decoded).collect();
+ Ok(toml_test_harness::Decoded::Array(v?))
+ }
+ }
+}
+
+fn value_to_decoded(
+ value: &toml_edit::Value,
+) -> Result<toml_test_harness::Decoded, toml_test_harness::Error> {
+ match value {
+ toml_edit::Value::Integer(v) => Ok(toml_test_harness::Decoded::Value(
+ toml_test_harness::DecodedValue::from(*v.value()),
+ )),
+ toml_edit::Value::String(v) => Ok(toml_test_harness::Decoded::Value(
+ toml_test_harness::DecodedValue::from(v.value()),
+ )),
+ toml_edit::Value::Float(v) => Ok(toml_test_harness::Decoded::Value(
+ toml_test_harness::DecodedValue::from(*v.value()),
+ )),
+ toml_edit::Value::Datetime(v) => {
+ let v = v.value();
+ let value = v.to_string();
+ let value = match (v.date.is_some(), v.time.is_some(), v.offset.is_some()) {
+ (true, true, true) => toml_test_harness::DecodedValue::Datetime(value),
+ (true, true, false) => toml_test_harness::DecodedValue::DatetimeLocal(value),
+ (true, false, false) => toml_test_harness::DecodedValue::DateLocal(value),
+ (false, true, false) => toml_test_harness::DecodedValue::TimeLocal(value),
+ _ => unreachable!("Unsupported case"),
+ };
+ Ok(toml_test_harness::Decoded::Value(value))
+ }
+ toml_edit::Value::Boolean(v) => Ok(toml_test_harness::Decoded::Value(
+ toml_test_harness::DecodedValue::from(*v.value()),
+ )),
+ toml_edit::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::Value::InlineTable(v) => inline_table_to_decoded(v),
+ }
+}
+
+fn table_to_decoded(
+ value: &toml_edit::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 = item_to_decoded(v)?;
+ Ok((k, v))
+ })
+ .collect();
+ Ok(toml_test_harness::Decoded::Table(table?))
+}
+
+fn inline_table_to_decoded(
+ value: &toml_edit::InlineTable,
+) -> 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?))
+}