Created
October 6, 2017 16:34
-
-
Save jbolila/873bb00c4f332f6c41bdea60f2299880 to your computer and use it in GitHub Desktop.
Serde types + variation types and serde_json::Value::pointer fun learning Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde; | |
extern crate serde_json; | |
extern crate serde_yaml; | |
use std::collections::BTreeMap as Map; | |
#[derive(Serialize, Deserialize, Debug)] | |
#[serde(tag = "method")] | |
enum Action { | |
GET { | |
operation: String, | |
criteria: String, | |
#[serde(rename = "statusCode")] | |
status_code: u32, | |
#[serde(skip_serializing_if = "Map::is_empty", default)] | |
assert: Map<String, String>, | |
}, | |
POST { | |
operation: String, | |
payload: serde_yaml::Value, | |
#[serde(rename = "statusCode")] | |
status_code: u32, | |
#[serde(skip_serializing_if = "Map::is_empty", default)] | |
assert: Map<String, String>, | |
}, | |
} | |
fn try_main() -> Result<(), Box<::std::error::Error>> { | |
let build_struct = " | |
- | |
method: POST | |
operation: element/group | |
payload: 'payloads/group' | |
statusCode: 201 | |
assert: | |
great: '101' | |
awesome: days in the beach | |
- | |
method: POST | |
operation: element/user | |
# payload: { 'java': { 'install_flavor': 'oracle' } } | |
payload: | |
context: ./dir | |
dockerfile: Dockerfile-alternate | |
args: | |
buildno: 1 | |
statusCode: 201 | |
- | |
method: GET | |
operation: element/user | |
criteria: 'payloads/user' | |
statusCode: 201 | |
"; | |
let action: Vec<Action> = serde_yaml::from_str(build_struct)?; | |
let post = serde_json::to_string(&action[1])?; | |
let post: serde_json::Value = serde_json::from_str(&post)?; | |
// println!( | |
// "POST[1]: {}", | |
// post.pointer("/payload/java/install_flavor").unwrap() | |
// ); | |
println!("Pointer: {}", post.pointer("/payload/dockerfile").unwrap()); | |
// Pointer: "Dockerfile-alternate" | |
Ok(()) | |
} | |
fn main() { | |
try_main().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment