Skip to content

Instantly share code, notes, and snippets.

@jthomas
Created January 18, 2017 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthomas/354752510897c6626519e8e56a5f80c3 to your computer and use it in GitHub Desktop.
Save jthomas/354752510897c6626519e8e56a5f80c3 to your computer and use it in GitHub Desktop.
Sample OpenWhisk Action using Rust
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