summaryrefslogtreecommitdiffstats
path: root/vendor/toml/tests/encoder.rs
blob: e76a74a26ee6e379f5b8ab0a8367b6e1a7a4f2ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![cfg(all(feature = "parse", feature = "display"))]

#[derive(Copy, Clone)]
pub struct Encoder;

impl toml_test_harness::Encoder for Encoder {
    fn name(&self) -> &str {
        "toml"
    }

    fn encode(&self, data: toml_test_harness::Decoded) -> Result<String, toml_test_harness::Error> {
        let value = from_decoded(&data)?;
        let toml::Value::Table(document) = value else {
            return Err(toml_test_harness::Error::new("no root table"));
        };
        let s = toml::to_string(&document).map_err(toml_test_harness::Error::new)?;
        Ok(s)
    }
}

fn from_decoded(
    decoded: &toml_test_harness::Decoded,
) -> Result<toml::Value, toml_test_harness::Error> {
    let value = match decoded {
        toml_test_harness::Decoded::Value(value) => from_decoded_value(value)?,
        toml_test_harness::Decoded::Table(value) => toml::Value::Table(from_table(value)?),
        toml_test_harness::Decoded::Array(value) => toml::Value::Array(from_array(value)?),
    };
    Ok(value)
}

fn from_decoded_value(
    decoded: &toml_test_harness::DecodedValue,
) -> Result<toml::Value, toml_test_harness::Error> {
    match decoded {
        toml_test_harness::DecodedValue::String(value) => Ok(toml::Value::String(value.clone())),
        toml_test_harness::DecodedValue::Integer(value) => value
            .parse::<i64>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Integer),
        toml_test_harness::DecodedValue::Float(value) => value
            .parse::<f64>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Float),
        toml_test_harness::DecodedValue::Bool(value) => value
            .parse::<bool>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Boolean),
        toml_test_harness::DecodedValue::Datetime(value) => value
            .parse::<toml::value::Datetime>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Datetime),
        toml_test_harness::DecodedValue::DatetimeLocal(value) => value
            .parse::<toml::value::Datetime>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Datetime),
        toml_test_harness::DecodedValue::DateLocal(value) => value
            .parse::<toml::value::Datetime>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Datetime),
        toml_test_harness::DecodedValue::TimeLocal(value) => value
            .parse::<toml::value::Datetime>()
            .map_err(toml_test_harness::Error::new)
            .map(toml::Value::Datetime),
    }
}

fn from_table(
    decoded: &std::collections::HashMap<String, toml_test_harness::Decoded>,
) -> Result<toml::value::Table, toml_test_harness::Error> {
    decoded
        .iter()
        .map(|(k, v)| {
            let v = from_decoded(v)?;
            Ok((k.to_owned(), v))
        })
        .collect()
}

fn from_array(
    decoded: &[toml_test_harness::Decoded],
) -> Result<toml::value::Array, toml_test_harness::Error> {
    decoded.iter().map(from_decoded).collect()
}