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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::{extension::TomlTableExt, JsonValue, TomlValue};
use serde_json::Number;

/// Extension trait for [`toml::Value`](toml::Value).
pub trait TomlValueExt {
    /// If the `Value` is an integer, represent it as `u8` if possible.
    /// Returns `None` otherwise.
    fn as_u8(&self) -> Option<u8>;

    /// If the `Value` is an integer, represent it as `u16` if possible.
    /// Returns `None` otherwise.
    fn as_u16(&self) -> Option<u16>;

    /// If the `Value` is an integer, represent it as `u32` if possible.
    /// Returns `None` otherwise.
    fn as_u32(&self) -> Option<u32>;

    /// If the `Value` is an integer, represent it as `usize` if possible.
    /// Returns `None` otherwise.
    fn as_usize(&self) -> Option<usize>;

    /// If the `Value` is an integer, represent it as `i8` if possible.
    /// Returns `None` otherwise.
    fn as_i8(&self) -> Option<i8>;

    /// If the `Value` is an integer, represent it as `i16` if possible.
    /// Returns `None` otherwise.
    fn as_i16(&self) -> Option<i16>;

    /// If the `Value` is an integer, represent it as `i32` if possible.
    /// Returns `None` otherwise.
    fn as_i32(&self) -> Option<i32>;

    /// If the `Value` is an integer, represent it as `isize` if possible.
    /// Returns `None` otherwise.
    fn as_isize(&self) -> Option<isize>;

    /// If the `Value` is a float, represent it as `f32` if possible.
    /// Returns `None` otherwise.
    fn as_f32(&self) -> Option<f32>;

    /// Converts `self` to a JSON value.
    fn to_json_value(&self) -> JsonValue;
}

impl TomlValueExt for TomlValue {
    #[inline]
    fn as_u8(&self) -> Option<u8> {
        self.as_integer().and_then(|i| u8::try_from(i).ok())
    }

    #[inline]
    fn as_u16(&self) -> Option<u16> {
        self.as_integer().and_then(|i| u16::try_from(i).ok())
    }

    #[inline]
    fn as_u32(&self) -> Option<u32> {
        self.as_integer().and_then(|i| u32::try_from(i).ok())
    }

    #[inline]
    fn as_usize(&self) -> Option<usize> {
        self.as_integer().and_then(|i| usize::try_from(i).ok())
    }

    #[inline]
    fn as_i8(&self) -> Option<i8> {
        self.as_integer().and_then(|i| i8::try_from(i).ok())
    }

    #[inline]
    fn as_i16(&self) -> Option<i16> {
        self.as_integer().and_then(|i| i16::try_from(i).ok())
    }

    #[inline]
    fn as_i32(&self) -> Option<i32> {
        self.as_integer().and_then(|i| i32::try_from(i).ok())
    }

    #[inline]
    fn as_isize(&self) -> Option<isize> {
        self.as_integer().and_then(|i| isize::try_from(i).ok())
    }

    #[inline]
    fn as_f32(&self) -> Option<f32> {
        self.as_float().map(|f| f as f32)
    }

    fn to_json_value(&self) -> JsonValue {
        match self {
            TomlValue::String(s) => JsonValue::String(s.to_owned()),
            TomlValue::Integer(i) => JsonValue::Number((*i).into()),
            TomlValue::Float(f) => {
                if let Some(number) = Number::from_f64(*f) {
                    JsonValue::Number(number)
                } else {
                    JsonValue::Null
                }
            }
            TomlValue::Boolean(b) => JsonValue::Bool(*b),
            TomlValue::Datetime(dt) => JsonValue::String(dt.to_string()),
            TomlValue::Array(vec) => {
                let vec = vec.iter().map(|v| v.to_json_value()).collect();
                JsonValue::Array(vec)
            }
            TomlValue::Table(table) => JsonValue::Object(table.to_map()),
        }
    }
}