Proteus   ![Build Status] ![Latest Version]

This library is intended to make dynamic transformation of data using serde serializable, deserialize using JSON and a JSON transformation syntax similar to Javascript JSON syntax. It supports registering custom Actions for use in syntax


toml [dependencies] proteus = "0.1"

Getter/Setter Syntax

The Getter and Setter syntax is custom to support custom/dynamic Actions and nearly identical with the Setter having additional options. If other parsing syntax is desired it can be used to build the Transformation in the same way that is done internally.

NOTE: order of operations is important.

Getter

| syntax | description | ---------|-------------| | | This sets the entire result to the provided value. | | id | Sets a JSON Object's name. eg. key in HashMap | | [0] | Same as above ^. | | profile.first_name | Combine Object names with dot notation. | | profile.address[0].street | Combinations using dot notation and indexes is also supported. |

| syntax | description | ---------|-------------| | | this will grab the top-level value which could be any valid type: Object, array, ... | | id | By itself any text is considered to be a JSON Object's name. | | [] | This appends the source data to an array, creating it if it doesn't exist and is only valid at the end of set syntax eg. profile.address[] | | [+] | The source Array should append all of it's values into the destination Array and is only valid at the end of set syntax eg. profile.address[] | | [-] | The source Array values should replace the destination Array's values at the overlapping indexes and is only valid at the end of set syntax eg. profile.address[] | | {} | This merges the supplied Object overtop of the existing and is only valid at the end of set syntax eg. profile{} | | profile.first_name | Combine Object names with dot notation. | | profile.address[0].street | Combinations using dot notation and indexes is also supported. |

Example usages

```rust use proteus::{actions, TransformBuilder}; use std::error::Error;

// This example show the basic usage of transformations fn main() -> Result<(), Box> { let input = r#" { "userid":"111", "firstname":"Dean", "lastname":"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" }, "myarr":[null,"arrvalue",null] } }"#; let trans = TransformBuilder::default() .addactions(actions!( ("userid", "id"), ( r#"join(" ", const("Mr."), firstname, lastname)"#, "full-name" ), ( r#"join(", ", addresses[0].street, addresses[0].postal, addresses[0].country)"#, "address" ), ("nested.inner.key", "prevnested"), ("nested.myarr", "myarr"), (r#"const("arrvalue2")"#, "myarr[]") )?) .build()?; let res = trans.applyfromstr(input)?; println!("{}", serdejson::tostringpretty(&res)?); Ok(()) } ```

or when you want to do struct to struct transformations

```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>, }

[derive(Serialize)]

struct Address { pub street: String, pub postal: String, pub country: String, }

[derive(Serialize)]

struct RawUserInfo { pub userid: String, pub firstname: String, pub last_name: String, pub addresses: Vec

, pub nested: Nested, }

[derive(Serialize, Deserialize)]

struct User { pub id: String, #[serde(rename = "full-name")] pub fullname: String, pub address: String, pub prevnested: String, pub my_arr: Vec>, }

// This example show the basic usage of transformations fn main() -> Result<(), Box> { let input = RawUserInfo { userid: "111".tostring(), firstname: "Dean".tostring(), lastname: "Karn".tostring(), addresses: vec![ Address { street: "26 Here Blvd".tostring(), postal: "123456".tostring(), country: "Canada".tostring(), }, Address { street: "26 Lakeside Cottage Lane.".tostring(), postal: "654321".tostring(), country: "Canada".tostring(), }, ], nested: Nested { inner: KV { key: "value".tostring(), }, myarr: vec![None, Some("arrvalue".toowned()), None], }, }; let trans = TransformBuilder::default() .addactions(actions!( ("userid", "id"), ( r#"join(" ", const("Mr."), firstname, lastname)"#, "full-name" ), ( r#"join(", ", addresses[0].street, addresses[0].postal, addresses[0].country)"#, "address" ), ("nested.inner.key", "prevnested"), ("nested.myarr", "myarr"), (r#"const("arrvalue2")"#, "myarr[]") )?) .build()?; let res: User = trans.applyto(input)?; println!("{}", serdejson::tostringpretty(&res)?); Ok(()) } ```

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Proteus by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.