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
use crate::array::Array;
use crate::object::Object;
use ordered_float::OrderedFloat;

/// Represents values of various possible other types.
/// Value is intended to be convertible to the same set
/// of types as Lua and is a superset of the types possible
/// in TOML and JSON.
#[derive(Clone, PartialEq, Hash, Eq, Ord, PartialOrd)]
pub enum Value {
    Null,
    Bool(bool),
    String(String),
    Array(Array),
    Object(Object),
    U64(u64),
    I64(i64),
    F64(OrderedFloat<f64>),
}

impl Default for Value {
    fn default() -> Self {
        Self::Null
    }
}

impl std::fmt::Debug for Value {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::String(s) => fmt.write_fmt(format_args!("{:?}", s)),
            Self::Null => fmt.write_str("nil"),
            Self::Bool(i) => i.fmt(fmt),
            Self::I64(i) => i.fmt(fmt),
            Self::U64(i) => i.fmt(fmt),
            Self::F64(i) => i.fmt(fmt),
            Self::Array(a) => a.fmt(fmt),
            Self::Object(o) => o.fmt(fmt),
        }
    }
}

impl Value {
    pub fn variant_name(&self) -> &str {
        match self {
            Self::Null => "Null",
            Self::Bool(_) => "Bool",
            Self::String(_) => "String",
            Self::Array(_) => "Array",
            Self::Object(_) => "Object",
            Self::U64(_) => "U64",
            Self::I64(_) => "I64",
            Self::F64(_) => "F64",
        }
    }

    pub fn coerce_unsigned(&self) -> Option<u64> {
        match self {
            Self::U64(u) => Some(*u),
            Self::I64(i) => (*i).try_into().ok(),
            Self::F64(OrderedFloat(f))
                if f.fract() == 0.0 && *f >= u64::MIN as f64 && *f <= u64::MAX as f64 =>
            {
                Some(*f as u64)
            }
            _ => None,
        }
    }

    pub fn coerce_signed(&self) -> Option<i64> {
        match self {
            Self::I64(u) => Some(*u),
            Self::U64(i) => (*i).try_into().ok(),
            Self::F64(OrderedFloat(f))
                if f.fract() == 0.0 && *f >= i64::MIN as f64 && *f <= i64::MAX as f64 =>
            {
                Some(*f as i64)
            }
            _ => None,
        }
    }

    pub fn coerce_float(&self) -> Option<f64> {
        match self {
            Self::I64(u) => Some(*u as f64),
            Self::U64(i) => Some(*i as f64),
            Self::F64(OrderedFloat(f)) => Some(*f),
            _ => None,
        }
    }
}