Use more of serde's superpowers
diff --git a/rust1.32/src/Cargo.lock b/rust1.32/src/Cargo.lock
index 2015f28..deb8f40 100644
--- a/rust1.32/src/Cargo.lock
+++ b/rust1.32/src/Cargo.lock
@@ -4,6 +4,8 @@
dependencies = [
"actions 0.1.0",
"libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -25,6 +27,22 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "proc-macro2"
+version = "0.4.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "0.6.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "ryu"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -35,6 +53,16 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "serde_derive"
+version = "1.0.89"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "serde_json"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -44,9 +72,29 @@
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
]
+[[package]]
+name = "syn"
+version = "0.15.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
[metadata]
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
"checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" = "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e"
+"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
+"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
"checksum serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560"
+"checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c"
"checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d"
+"checksum syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)" = "525bd55255f03c816e5d7f615587bd13030c7103354fadb104993dcee6a788ec"
+"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
diff --git a/rust1.32/src/action_loop/Cargo.toml b/rust1.32/src/action_loop/Cargo.toml
index 253fd02..7093ea7 100644
--- a/rust1.32/src/action_loop/Cargo.toml
+++ b/rust1.32/src/action_loop/Cargo.toml
@@ -4,6 +4,8 @@
authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"]
[dependencies]
+serde = "1.0"
+serde_derive = "1.0"
serde_json = "1.0"
libc = "0.2.49"
-actions = { path = "../actions" }
\ No newline at end of file
+actions = { path = "../actions" }
diff --git a/rust1.32/src/action_loop/src/main.rs b/rust1.32/src/action_loop/src/main.rs
index ea6da96..6bb9d5f 100644
--- a/rust1.32/src/action_loop/src/main.rs
+++ b/rust1.32/src/action_loop/src/main.rs
@@ -1,52 +1,48 @@
extern crate actions;
extern crate libc;
+extern crate serde;
+extern crate serde_derive;
extern crate serde_json;
use actions::main as actionMain;
+use serde_derive::Deserialize;
+
use serde_json::{Error, Value};
use std::collections::HashMap;
use std::env;
use std::fs::File;
-use std::io::{self, stderr, stdout, Write};
+use std::io::{stderr, stdin, stdout, Write};
use std::os::unix::io::FromRawFd;
+#[derive(Debug, Clone, PartialEq, Deserialize)]
+struct Input {
+ value: HashMap<String, Value>,
+ #[serde(flatten)]
+ environment: HashMap<String, Value>,
+}
+
fn main() {
let mut fd3 = unsafe { File::from_raw_fd(3) };
loop {
let mut buffer = String::new();
- io::stdin().read_line(&mut buffer).unwrap();
- let parsed_input: Result<HashMap<String, Value>, Error> = serde_json::from_str(&buffer);
- let mut payload: HashMap<String, Value> = HashMap::new();
+ stdin().read_line(&mut buffer).unwrap();
+ let parsed_input: Result<Input, Error> = serde_json::from_str(&buffer);
match parsed_input {
- Ok(n) => {
- for (key, val) in n {
- if key == "value" {
- let mut unparsed_payload: Result<HashMap<String, Value>, Error> =
- serde_json::from_value(val);
- match unparsed_payload {
- Ok(value) => payload = value,
- Err(err) => {
- eprintln!("Error parsing value json: {}", err);
- continue;
- }
- }
- } else {
- env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string());
+ Ok(input) => {
+ for (key, val) in input.environment {
+ env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string());
+ }
+ match serde_json::to_string(&actionMain(input.value)) {
+ Ok(result) => {
+ writeln!(&mut fd3, "{}", result).expect("Error writing on fd3");
+ }
+ Err(err) => {
+ eprintln!("Error formatting result value json: {}", err);
}
}
}
- Err(e) => {
- eprintln!("Error: {}", e);
- continue;
- }
- }
-
- match serde_json::to_string(&actionMain(payload)) {
- Ok(result) => {
- writeln!(&mut fd3, "{}", result).expect("Error writing on fd3");
- }
Err(err) => {
- eprintln!("Error formatting result value json: {}", err);
+ eprintln!("Error parsing input: {}", err);
}
}
stdout().flush().expect("Error flushing stdout");