Created
January 18, 2017 15:05
-
-
Save jthomas/354752510897c6626519e8e56a5f80c3 to your computer and use it in GitHub Desktop.
Sample OpenWhisk Action using Rust
This file contains 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
extern crate rustc_serialize; | |
use rustc_serialize::json; | |
use rustc_serialize::json::Json; | |
use std::env; | |
#[derive(RustcDecodable, RustcEncodable)] | |
pub struct Greeting { | |
message: String | |
} | |
fn main() { | |
let mut name = "stranger".to_string(); | |
// first arg contains JSON parameters | |
if let Some(arg1) = env::args().nth(1) { | |
// parse JSON and extract 'name' field | |
let params = Json::from_str(&arg1).unwrap(); | |
if let Some(params_obj) = params.as_object() { | |
if let Some(params_name) = params_obj.get("name") { | |
name = params_name.as_string().unwrap().to_string(); | |
} | |
} | |
}; | |
let greeting = Greeting { | |
message: format!("Hello, {}!", name), | |
}; | |
println!("{}", json::encode(&greeting).unwrap()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment