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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use super::Event;
use snafu::Snafu;

/// Trait to implement a builder for [`Event`]:
/// ```
/// use cloudevents::event::{EventBuilderV10, EventBuilder};
/// use chrono::Utc;
/// use url::Url;
///
/// let event = EventBuilderV10::new()
///     .id("my_event.my_application")
///     .source("http://localhost:8080")
///     .ty("example.demo")
///     .time(Utc::now())
///     .build()
///     .unwrap();
/// ```
///
/// You can create an [`EventBuilder`] starting from an existing [`Event`] using the [`From`] trait.
/// You can create a default [`EventBuilder`] setting default values for some attributes.
pub trait EventBuilder
where
    Self: Clone + Sized + From<Event> + Default,
{
    /// Create a new empty builder
    fn new() -> Self;

    /// Build [`Event`]
    fn build(self) -> Result<Event, Error>;
}

/// Represents an error during build process
#[derive(Debug, Snafu, Clone)]
pub enum Error {
    #[snafu(display("Missing required attribute {}", attribute_name))]
    MissingRequiredAttribute { attribute_name: &'static str },
    #[snafu(display(
        "Error while setting attribute '{}' with timestamp type: {}",
        attribute_name,
        source
    ))]
    ParseTimeError {
        attribute_name: &'static str,
        source: chrono::ParseError,
    },
    #[snafu(display(
        "Error while setting attribute '{}' with uri type: {}",
        attribute_name,
        source
    ))]
    ParseUrlError {
        attribute_name: &'static str,
        source: url::ParseError,
    },
    #[snafu(display(
        "Invalid value setting attribute '{}' with uriref type",
        attribute_name,
    ))]
    InvalidUriRefError { attribute_name: &'static str },
}

#[cfg(test)]
mod tests {
    use crate::test::fixtures;
    use crate::Event;
    use crate::EventBuilder;
    use crate::EventBuilderV03;
    use crate::EventBuilderV10;
    use claim::*;
    use rstest::rstest;
    use serde_json::{json, Value};
    use serde_yaml;

    /// Test conversions

    #[test]
    fn v10_to_v03() {
        let in_event = fixtures::v10::full_json_data();
        let out_event = EventBuilderV03::from(in_event).build().unwrap();
        assert_eq!(fixtures::v03::full_json_data(), out_event)
    }

    #[test]
    fn v03_to_v10() {
        let in_event = fixtures::v03::full_json_data();
        let out_event = EventBuilderV10::from(in_event).build().unwrap();
        assert_eq!(fixtures::v10::full_json_data(), out_event)
    }

    /// Test YAML
    /// This test checks if the usage of serde_json::Value makes the Deserialize implementation incompatible with
    /// other Deserializers
    #[test]
    fn deserialize_yaml_should_succeed() {
        let input = r#"
    id: aaa
    type: bbb
    source: http://localhost
    datacontenttype: application/json
    data: true
    specversion: "1.0"
    "#;

        let expected = EventBuilderV10::new()
            .id("aaa")
            .ty("bbb")
            .source("http://localhost")
            .data("application/json", serde_json::Value::Bool(true))
            .build()
            .unwrap();

        let deserialize_result: Result<Event, serde_yaml::Error> = serde_yaml::from_str(input);
        assert_ok!(&deserialize_result);
        let deserialized = deserialize_result.unwrap();
        assert_eq!(deserialized, expected)
    }

    /// Test Json
    /// This test is a parametrized test that uses data from tests/test_data
    #[rstest(
        in_event,
        out_json,
        case::minimal_v03(fixtures::v03::minimal(), fixtures::v03::minimal_json()),
        case::full_v03_no_data(fixtures::v03::full_no_data(), fixtures::v03::full_no_data_json()),
        case::full_v03_with_json_data(
            fixtures::v03::full_json_data(),
            fixtures::v03::full_json_data_json()
        ),
        case::full_v03_with_xml_string_data(
            fixtures::v03::full_xml_string_data(),
            fixtures::v03::full_xml_string_data_json()
        ),
        case::full_v03_with_xml_base64_data(
            fixtures::v03::full_xml_binary_data(),
            fixtures::v03::full_xml_base64_data_json()
        ),
        case::minimal_v10(fixtures::v10::minimal(), fixtures::v10::minimal_json()),
        case::full_v10_no_data(fixtures::v10::full_no_data(), fixtures::v10::full_no_data_json()),
        case::full_v10_with_json_data(
            fixtures::v10::full_json_data(),
            fixtures::v10::full_json_data_json()
        ),
        case::full_v10_with_xml_string_data(
            fixtures::v10::full_xml_string_data(),
            fixtures::v10::full_xml_string_data_json()
        ),
        case::full_v10_with_xml_base64_data(
            fixtures::v10::full_xml_binary_data(),
            fixtures::v10::full_xml_base64_data_json()
        )
    )]
    fn serialize_should_succeed(in_event: Event, out_json: Value) {
        // Event -> serde_json::Value
        let serialize_result = serde_json::to_value(in_event.clone());
        assert_ok!(&serialize_result);
        let actual_json = serialize_result.unwrap();
        assert_eq!(&actual_json, &out_json);

        // serde_json::Value -> String
        let actual_json_serialized = actual_json.to_string();
        assert_eq!(actual_json_serialized, out_json.to_string());

        // String -> Event
        let deserialize_result: Result<Event, serde_json::Error> =
            serde_json::from_str(&actual_json_serialized);
        assert_ok!(&deserialize_result);
        let deserialize_json = deserialize_result.unwrap();
        assert_eq!(deserialize_json, in_event)
    }

    /// This test is a parametrized test that uses data from tests/test_data
    #[rstest(
        in_json,
        out_event,
        case::minimal_v03(fixtures::v03::minimal_json(), fixtures::v03::minimal()),
        case::full_v03_no_data(fixtures::v03::full_no_data_json(), fixtures::v03::full_no_data()),
        case::full_v03_with_json_data(
            fixtures::v03::full_json_data_json(),
            fixtures::v03::full_json_data()
        ),
        case::full_v03_with_json_base64_data(
            fixtures::v03::full_json_base64_data_json(),
            fixtures::v03::full_json_data()
        ),
        case::full_v03_with_xml_string_data(
            fixtures::v03::full_xml_string_data_json(),
            fixtures::v03::full_xml_string_data()
        ),
        case::full_v03_with_xml_base64_data(
            fixtures::v03::full_xml_base64_data_json(),
            fixtures::v03::full_xml_binary_data()
        ),
        case::minimal_v10(fixtures::v10::minimal_json(), fixtures::v10::minimal()),
        case::full_v10_no_data(fixtures::v10::full_no_data_json(), fixtures::v10::full_no_data()),
        case::full_v10_with_json_data(
            fixtures::v10::full_json_data_json(),
            fixtures::v10::full_json_data()
        ),
        case::full_v10_with_json_base64_data(
            fixtures::v10::full_json_base64_data_json(),
            fixtures::v10::full_json_data()
        ),
        case::full_v10_with_non_json_base64_data(
            fixtures::v10::full_non_json_base64_data(),
            fixtures::v10::full_non_json_data()
        ),
        case::full_v10_with_xml_string_data(
            fixtures::v10::full_xml_string_data_json(),
            fixtures::v10::full_xml_string_data()
        ),
        case::full_v10_with_xml_base64_data(
            fixtures::v10::full_xml_base64_data_json(),
            fixtures::v10::full_xml_binary_data()
        )
    )]
    fn deserialize_json_should_succeed(in_json: Value, out_event: Event) {
        let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
        assert_ok!(&deserialize_result);
        let deserialize_json = deserialize_result.unwrap();
        assert_eq!(deserialize_json, out_event)
    }

    #[test]
    fn deserialize_with_null_attribute() {
        let in_json = json!({
            "specversion" : "1.0",
            "type" : "com.example.someevent",
            "source" : "/mycontext",
            "id" : "A234-1234-1234",
            "time" : null,
            "comexampleextension1" : "value",
            "comexampleothervalue" : 5,
            "datacontenttype" : "text/xml",
            "data" : "<much wow=\"xml\"/>"
        });

        let out_event = EventBuilderV10::new()
            .ty("com.example.someevent")
            .source("/mycontext")
            .id("A234-1234-1234")
            .data("text/xml", "<much wow=\"xml\"/>")
            .extension("comexampleextension1", "value")
            .extension("comexampleothervalue", 5)
            .build()
            .unwrap();

        let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
        assert_ok!(&deserialize_result);
        let deserialize_json = deserialize_result.unwrap();
        assert_eq!(deserialize_json, out_event)
    }

    #[test]
    fn deserialize_with_null_ext() {
        let in_json = json!({
            "specversion" : "1.0",
            "type" : "com.example.someevent",
            "source" : "/mycontext",
            "id" : "A234-1234-1234",
            "time" : "2018-04-05T17:31:00Z",
            "comexampleextension1" : "value",
            "comexampleothervalue" : 5,
            "unsetextension": null,
            "datacontenttype" : "text/xml",
            "data" : "<much wow=\"xml\"/>"
        });

        let out_event = EventBuilderV10::new()
            .ty("com.example.someevent")
            .source("/mycontext")
            .id("A234-1234-1234")
            .time("2018-04-05T17:31:00Z")
            .data("text/xml", "<much wow=\"xml\"/>")
            .extension("comexampleextension1", "value")
            .extension("comexampleothervalue", 5)
            .build()
            .unwrap();

        let deserialize_result: Result<Event, serde_json::Error> = serde_json::from_value(in_json);
        assert_ok!(&deserialize_result);
        let deserialize_json = deserialize_result.unwrap();
        assert_eq!(deserialize_json, out_event)
    }
}