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
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::convert::From;

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
#[serde(untagged)]
/// Represents all the possible [CloudEvents extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) values
pub enum ExtensionValue {
    /// Represents a [`String`](std::string::String) value.
    String(String),
    /// Represents a [`bool`](bool) value.
    Boolean(bool),
    /// Represents an integer [`i64`](i64) value.
    Integer(i64),
    /// Represents a [Json `Value`](serde_json::value::Value).
    Json(Value),
}

impl From<String> for ExtensionValue {
    fn from(s: String) -> Self {
        ExtensionValue::String(s)
    }
}

impl From<bool> for ExtensionValue {
    fn from(s: bool) -> Self {
        ExtensionValue::Boolean(s)
    }
}

impl From<i64> for ExtensionValue {
    fn from(s: i64) -> Self {
        ExtensionValue::Integer(s)
    }
}

impl From<Value> for ExtensionValue {
    fn from(s: Value) -> Self {
        ExtensionValue::Json(s)
    }
}

impl ExtensionValue {
    pub fn from_string<S>(s: S) -> Self
    where
        S: Into<String>,
    {
        ExtensionValue::from(s.into())
    }

    pub fn from_i64<S>(s: S) -> Self
    where
        S: Into<i64>,
    {
        ExtensionValue::from(s.into())
    }

    pub fn from_bool<S>(s: S) -> Self
    where
        S: Into<bool>,
    {
        ExtensionValue::from(s.into())
    }

    pub fn from_json_value<S>(s: S) -> Self
    where
        S: Into<serde_json::Value>,
    {
        ExtensionValue::from(s.into())
    }
}