Skip to content

Commit 4427257

Browse files
rdiaz82sciabarracom
authored andcommitted
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
1 parent 71e0d02 commit 4427257

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

rust1.32/src/action_loop/src/main.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ use std::{
1212

1313
#[derive(Debug, Clone, PartialEq, Deserialize)]
1414
struct Input {
15-
value: HashMap<String, Value>,
15+
value: Value,
1616
#[serde(flatten)]
1717
environment: HashMap<String, Value>,
1818
}
1919

20+
fn log_error(fd3: &mut File, error: Error) {
21+
writeln!(fd3, "{{\"error\":\"{}\"}}\n", error).expect("Error writing on fd3");
22+
eprintln!("error: {}", error);
23+
}
24+
2025
fn main() {
2126
let mut fd3 = unsafe { File::from_raw_fd(3) };
2227
let stdin = stdin();
@@ -28,17 +33,20 @@ fn main() {
2833
for (key, val) in input.environment {
2934
env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string());
3035
}
31-
match serde_json::to_string(&actionMain(input.value)) {
32-
Ok(result) => {
33-
writeln!(&mut fd3, "{}", result).expect("Error writing on fd3");
34-
}
36+
match actionMain(input.value) {
37+
Ok(action_result) => match serde_json::to_string(&action_result) {
38+
Ok(response) => {
39+
writeln!(&mut fd3, "{}", response).expect("Error writing on fd3")
40+
}
41+
Err(err) => log_error(&mut fd3, err),
42+
},
3543
Err(err) => {
36-
eprintln!("Error formatting result value json: {}", err);
44+
log_error(&mut fd3, err);
3745
}
3846
}
3947
}
4048
Err(err) => {
41-
eprintln!("Error parsing input: {}", err);
49+
log_error(&mut fd3, err);
4250
}
4351
}
4452
stdout().flush().expect("Error flushing stdout");

rust1.32/src/actions/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
name = "actions"
33
version = "0.1.0"
44
authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"]
5+
edition = "2018"
56

67
[dependencies]
78
serde_json = "1.0"
9+
serde = "1.0"
10+
serde_derive = "1.0"
11+
812

rust1.32/src/actions/src/lib.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
extern crate serde_json;
22

3-
use std::collections::HashMap;
4-
use serde_json::Value;
3+
use serde_derive::{Deserialize, Serialize};
4+
use serde_json::{Error, Value};
55

6+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7+
struct Input {
8+
#[serde(default = "stranger")]
9+
name: String,
10+
}
11+
12+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13+
struct Output {
14+
greeting: String,
15+
}
16+
17+
fn stranger() -> String {
18+
"stranger".to_string()
19+
}
620

7-
pub fn main(mut input_data: HashMap<String, Value>) -> HashMap<String, Value> {
8-
input_data.insert("added_key".to_string(),Value::String("test".to_string()));
9-
input_data
21+
pub fn main(args: Value) -> Result<Value, Error> {
22+
let input: Input = serde_json::from_value(args)?;
23+
let output = Output {
24+
greeting: format!("Hello, {}", input.name),
25+
};
26+
serde_json::to_value(output)
1027
}

0 commit comments

Comments
 (0)