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
//! # Proteus
//!
//! Proteus is intended to make dynamic transformation of data using serde serializable, deserialize
//! using JSON and a JSON transformation syntax similar to Javascript JSON syntax.
//!
//! ```rust
//! use proteus::{actions, TransformBuilder};
//! use std::error::Error;
//!
//! // This example show the basic usage of transformations
//! fn main() -> Result<(), Box<dyn Error>> {
//! let input = r#"
//! {
//! "user_id":"111",
//! "first_name":"Dean",
//! "last_name":"Karn",
//! "addresses": [
//! { "street":"26 Here Blvd", "postal":"123456", "country":"Canada", "primary":true },
//! { "street":"26 Lakeside Cottage Lane.", "postal":"654321", "country":"Canada" }
//! ],
//! "nested": {
//! "inner":{
//! "key":"value"
//! },
//! "my_arr":[null,"arr_value",null]
//! }
//! }"#;
//! let trans = TransformBuilder::default()
//! .add_actions(actions!(
//! ("user_id", "id"),
//! (
//! r#"join(" ", const("Mr."), first_name, last_name)"#,
//! "full-name"
//! ),
//! (
//! r#"join(", ", addresses[0].street, addresses[0].postal, addresses[0].country)"#,
//! "address"
//! ),
//! ("nested.inner.key", "prev_nested"),
//! ("nested.my_arr", "my_arr"),
//! (r#"const("arr_value_2")"#, "my_arr[]")
//! )?)
//! .build()?;
//! let res = trans.apply_from_str(input)?;
//! println!("{}", serde_json::to_string_pretty(&res)?);
//! Ok(())
//! }
//! ```
//!
//! or direct from struct to struct
//!
//! ```rust
//! use proteus::{actions, TransformBuilder};
//! use serde::{Deserialize, Serialize};
//! use std::error::Error;
//!
//! #[derive(Serialize)]
//! struct KV {
//! pub key: String,
//! }
//!
//! #[derive(Serialize)]
//! struct Nested {
//! pub inner: KV,
//! pub my_arr: Vec<Option<String>>,
//! }
//!
//! #[derive(Serialize)]
//! struct Address {
//! pub street: String,
//! pub postal: String,
//! pub country: String,
//! }
//!
//! #[derive(Serialize)]
//! struct RawUserInfo {
//! pub user_id: String,
//! pub first_name: String,
//! pub last_name: String,
//! pub addresses: Vec<Address>,
//! pub nested: Nested,
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct User {
//! pub id: String,
//! #[serde(rename = "full-name")]
//! pub full_name: String,
//! pub address: String,
//! pub prev_nested: String,
//! pub my_arr: Vec<Option<String>>,
//! }
//!
//! // This example show the basic usage of transformations
//! fn main() -> Result<(), Box<dyn Error>> {
//! let input = RawUserInfo {
//! user_id: "111".to_string(),
//! first_name: "Dean".to_string(),
//! last_name: "Karn".to_string(),
//! addresses: vec![
//! Address {
//! street: "26 Here Blvd".to_string(),
//! postal: "123456".to_string(),
//! country: "Canada".to_string(),
//! },
//! Address {
//! street: "26 Lakeside Cottage Lane.".to_string(),
//! postal: "654321".to_string(),
//! country: "Canada".to_string(),
//! },
//! ],
//! nested: Nested {
//! inner: KV {
//! key: "value".to_string(),
//! },
//! my_arr: vec![None, Some("arr_value".to_owned()), None],
//! },
//! };
//! let trans = TransformBuilder::default()
//! .add_actions(actions!(
//! ("user_id", "id"),
//! (
//! r#"join(" ", const("Mr."), first_name, last_name)"#,
//! "full-name"
//! ),
//! (
//! r#"join(", ", addresses[0].street, addresses[0].postal, addresses[0].country)"#,
//! "address"
//! ),
//! ("nested.inner.key", "prev_nested"),
//! ("nested.my_arr", "my_arr"),
//! (r#"const("arr_value_2")"#, "my_arr[]")
//! )?)
//! .build()?;
//! let res: User = trans.apply_to(input)?;
//! println!("{}", serde_json::to_string_pretty(&res)?);
//! Ok(())
//! }
//! ```
//!
pub mod action;
pub mod actions;
pub mod errors;
pub mod parser;
pub mod transformer;
#[doc(inline)]
pub use parser::{Parsable, Parser, COMMA_SEP_RE, QUOTED_STR_RE};
#[doc(inline)]
pub use transformer::TransformBuilder;
#[doc(inline)]
pub use errors::Error;
/// This macros is shorthand for creating a set of actions to be added to [TransformBuilder](struct.TransformBuilder.html).
#[macro_export]
macro_rules! actions {
($($p:expr),*) => {
{
let mut parsables = Vec::new();
$(
parsables.push(proteus::Parsable::new($p.0, $p.1));
)*
proteus::Parser::parse_multi(&parsables)
}
};
}