This is an automated email from the ASF dual-hosted git repository. msciabarra pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-runtime-rust.git
The following commit(s) were added to refs/heads/master by this push: new 4427257 Modification in Main Action method signature and improved the error log (#2) 4427257 is described below commit 44272573ed9ee00315c879e2912d42e0334c6c15 Author: Roberto Diaz <robe...@theagilemonkeys.com> AuthorDate: Sun Mar 10 11:23:14 2019 +0000 Modification in Main Action method signature and improved the error log (#2) * modified action signature and changed example action * format code * changed response error message for action loop * change error msg * refactored error messages --- rust1.32/src/action_loop/src/main.rs | 22 +++++++++++++++------- rust1.32/src/actions/Cargo.toml | 4 ++++ rust1.32/src/actions/src/lib.rs | 27 ++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/rust1.32/src/action_loop/src/main.rs b/rust1.32/src/action_loop/src/main.rs index 7d874c9..25144be 100644 --- a/rust1.32/src/action_loop/src/main.rs +++ b/rust1.32/src/action_loop/src/main.rs @@ -12,11 +12,16 @@ use std::{ #[derive(Debug, Clone, PartialEq, Deserialize)] struct Input { - value: HashMap<String, Value>, + value: Value, #[serde(flatten)] environment: HashMap<String, Value>, } +fn log_error(fd3: &mut File, error: Error) { + writeln!(fd3, "{{\"error\":\"{}\"}}\n", error).expect("Error writing on fd3"); + eprintln!("error: {}", error); +} + fn main() { let mut fd3 = unsafe { File::from_raw_fd(3) }; let stdin = stdin(); @@ -28,17 +33,20 @@ fn main() { 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"); - } + match actionMain(input.value) { + Ok(action_result) => match serde_json::to_string(&action_result) { + Ok(response) => { + writeln!(&mut fd3, "{}", response).expect("Error writing on fd3") + } + Err(err) => log_error(&mut fd3, err), + }, Err(err) => { - eprintln!("Error formatting result value json: {}", err); + log_error(&mut fd3, err); } } } Err(err) => { - eprintln!("Error parsing input: {}", err); + log_error(&mut fd3, err); } } stdout().flush().expect("Error flushing stdout"); diff --git a/rust1.32/src/actions/Cargo.toml b/rust1.32/src/actions/Cargo.toml index 315401c..c4bb533 100644 --- a/rust1.32/src/actions/Cargo.toml +++ b/rust1.32/src/actions/Cargo.toml @@ -2,7 +2,11 @@ name = "actions" version = "0.1.0" authors = ["Roberto Diaz <robe...@theagilemonkeys.com>"] +edition = "2018" [dependencies] serde_json = "1.0" +serde = "1.0" +serde_derive = "1.0" + diff --git a/rust1.32/src/actions/src/lib.rs b/rust1.32/src/actions/src/lib.rs index cbeafbd..732e23b 100644 --- a/rust1.32/src/actions/src/lib.rs +++ b/rust1.32/src/actions/src/lib.rs @@ -1,10 +1,27 @@ extern crate serde_json; -use std::collections::HashMap; -use serde_json::Value; +use serde_derive::{Deserialize, Serialize}; +use serde_json::{Error, Value}; +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +struct Input { + #[serde(default = "stranger")] + name: String, +} + +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +struct Output { + greeting: String, +} + +fn stranger() -> String { + "stranger".to_string() +} -pub fn main(mut input_data: HashMap<String, Value>) -> HashMap<String, Value> { - input_data.insert("added_key".to_string(),Value::String("test".to_string())); - input_data +pub fn main(args: Value) -> Result<Value, Error> { + let input: Input = serde_json::from_value(args)?; + let output = Output { + greeting: format!("Hello, {}", input.name), + }; + serde_json::to_value(output) }