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
//! # Bumblebee
//!
//! Bumblebee is a JSON transformer with simple built in rules that can easily be implemented by even
//! the average user. It is designed to be extensible, simple to use and serializable for easy
//! storage and creation within service and apps.
//!
//! Source values that are not found or are incompatible will show up as `null` values in
//! the output.
//!
//! ```rust
//! use bumblebee::prelude::*;
//! use bumblebee::errors::Result;
//!
//! fn test_example() -> Result<()> {
//! let trans = TransformerBuilder::default()
//! .add_direct("user_id", "id")?
//! .add_direct("full-name", "name")?
//! .add_flatten(
//! "nicknames",
//! "",
//! FlattenOps {
//! recursive: true,
//! prefix: Some("nickname"),
//! separator: Some("_"),
//! manipulation: None,
//! },
//! )?
//! .add_direct("nested.inner.key", "prev_nested")?
//! .add_direct("nested.my_arr[1]", "prev_arr")?
//! .build()?;
//! let input = r#"
//! {
//! "user_id":"111",
//! "full-name":"Dean Karn",
//! "nicknames":["Deano","Joey Bloggs"],
//! "nested": {
//! "inner":{
//! "key":"value"
//! },
//! "my_arr":[null,"arr_value",null]
//! }
//! }"#;
//! let expected = r#"{"id":"111","name":"Dean Karn","nickname_1":"Deano","nickname_2":"Joey Bloggs","prev_arr":"arr_value","prev_nested":"value"}"#;
//! let res = trans.apply_from_str(input)?;
//! assert_eq!(expected, serde_json::to_string(&res)?);
//! Ok(())
//! }
//! ```
//!
//! or direct from struct to struct
//!
//! ```rust
//! use bumblebee::prelude::*;
//! use bumblebee::errors::Result;
//! use serde::{Serialize, Deserialize};
//!
//! fn test_struct() -> Result<()> {
//! #[derive(Debug, Serialize)]
//! struct From {
//! existing: String,
//! }
//!
//! #[derive(Debug, Deserialize, PartialEq)]
//! struct To {
//! new: String,
//! }
//!
//! let trans = TransformerBuilder::default()
//! .add_direct("existing", "new")?
//! .build()?;
//!
//! let from = From {
//! existing: String::from("existing_value"),
//! };
//!
//! let expected = To {
//! new: String::from("existing_value"),
//! };
//! let res: To = trans.apply_to(from)?;
//! assert_eq!(expected, res);
//! Ok(())
//! }
//! ```
//!