|
16 | 16 | # limitations under the License.
|
17 | 17 | #
|
18 | 18 | -->
|
19 |
| -# openwhisk-runtime-rust |
20 |
| - |
21 |
| -Work in Progress... It will be awesome! |
| 19 | +# Apache OpenWhisk Runtime for Rust |
22 | 20 |
|
23 | 21 | [](http://www.apache.org/licenses/LICENSE-2.0)
|
24 | 22 | [](https://travis-ci.org/apache/openwhisk-runtime-rust)
|
25 | 23 |
|
| 24 | +### Give it a try today |
| 25 | +To use as a Docker action: |
| 26 | + |
| 27 | +``` |
| 28 | +wsk action update myAction my_action.rs --docker openwhisk/actionloop-rust-v1.34 |
| 29 | +``` |
| 30 | + |
| 31 | +The file `my_action.rs` looks like: |
| 32 | + |
| 33 | +``` |
| 34 | +extern crate serde_json; |
| 35 | +
|
| 36 | +use serde_derive::{Deserialize, Serialize}; |
| 37 | +use serde_json::{Error, Value}; |
| 38 | +
|
| 39 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 40 | +struct Input { |
| 41 | + #[serde(default = "stranger")] |
| 42 | + name: String, |
| 43 | +} |
| 44 | +
|
| 45 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 46 | +struct Output { |
| 47 | + body: String, |
| 48 | +} |
| 49 | +
|
| 50 | +fn stranger() -> String { |
| 51 | + "stranger".to_string() |
| 52 | +} |
| 53 | +
|
| 54 | +pub fn main(args: Value) -> Result<Value, Error> { |
| 55 | + let input: Input = serde_json::from_value(args)?; |
| 56 | + let output = Output { |
| 57 | + body: format!("Hello, {}", input.name), |
| 58 | + }; |
| 59 | + serde_json::to_value(output) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +The action is mainly composed by a `main` function that accepts a JSON `serdes Value` as input and returns a `Result` including a JSON `serde Value`. |
| 64 | + |
| 65 | +### Managing dependencies |
| 66 | + |
| 67 | +If your action needs external dependencies, you need to provide a zip file including your source and your cargo file with all your dependencies. The folder structure is the following: |
| 68 | +``` |
| 69 | +|- Cargo.toml |
| 70 | +|- src |
| 71 | + |- lib.rs |
| 72 | +``` |
| 73 | +Here is an example of a `Cargo.toml` file |
| 74 | +``` |
| 75 | +[package] |
| 76 | +name = "actions" |
| 77 | +version = "0.1.0" |
| 78 | +authors = ["John Doe <john@doe.domain>"] |
| 79 | +edition = "2018" |
| 80 | +
|
| 81 | +[dependencies] |
| 82 | +serde_json = "1.0" |
| 83 | +serde = "1.0" |
| 84 | +serde_derive = "1.0" |
| 85 | +``` |
| 86 | +Once you have all your code zipped in a file with the showed folder structure you can generate your action with the following command: |
| 87 | +``` |
| 88 | +wsk action create yourAction /full_path_to/yourCode.zip --docker openwhisk/actionloop-rust-v1.34 |
| 89 | +``` |
0 commit comments