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
use std::any::Any;
use std::fmt;

use chrono::{DateTime, FixedOffset};

/// Represents primitive types that are supported for conversion into a BTreeMap that can support
/// heterogeneous values. Inspired by `serde_json::Value`s.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Unknown,
    String(String),
    Double(f64),
    Bool(bool),
    Long(i64),
    UnsignedLong(u64),
    Duration(chrono::Duration),
    Base64Binary(Vec<u8>),
    TimeRFC(DateTime<FixedOffset>),
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl Value {
    /// Given a genericized input type, encapsulate it as a Value that can be used in a map
    /// container type when converting to and from a struct.
    pub fn new<T: Any>(value: T) -> Value {
        let any_val = &value as &dyn Any;
        if let Some(val) = any_val.downcast_ref::<f64>() {
            Value::Double(*val)
        } else if let Some(val) = any_val.downcast_ref::<bool>() {
            Value::Bool(*val)
        } else if let Some(val) = any_val.downcast_ref::<i64>() {
            Value::Long(*val)
        } else if let Some(val) = any_val.downcast_ref::<u64>() {
            Value::UnsignedLong(*val)
        } else if let Some(val) = any_val.downcast_ref::<chrono::Duration>() {
            Value::Duration(*val)
        } else if let Some(val) = any_val.downcast_ref::<Vec<u8>>() {
            Value::Base64Binary(val.clone())
        } else if let Some(val) = any_val.downcast_ref::<DateTime<FixedOffset>>() {
            Value::TimeRFC(*val)
        } else if let Some(val) = any_val.downcast_ref::<String>() {
            Value::String(val.to_string())
        } else {
            Value::Unknown
        }
    }

    pub fn downcast<T>(&self) -> Option<T> {
        None
    }

    pub fn bool(&self) -> Option<bool> {
        if let Value::Bool(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn i64(&self) -> Option<i64> {
        if let Value::Long(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn u64(&self) -> Option<u64> {
        if let Value::UnsignedLong(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn f64(&self) -> Option<f64> {
        if let Value::Double(val) = self {
            Some(*val)
        } else {
            None
        }
    }

    pub fn string(&self) -> Option<String> {
        if let Value::String(string) = self {
            Some(string.to_string())
        } else {
            None
        }
    }
}