diff --git a/rust-module-hello-world/module-src/Cargo.lock b/rust-module-hello-world/module-src/Cargo.lock index b6d9f8f4b..b988c36f7 100644 --- a/rust-module-hello-world/module-src/Cargo.lock +++ b/rust-module-hello-world/module-src/Cargo.lock @@ -1,87 +1,94 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "dtoa" -version = "0.4.2" +name = "anyhow" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" [[package]] name = "helloworld" version = "0.1.0" dependencies = [ - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", + "anyhow", + "serde", + "serde_json", ] [[package]] name = "itoa" -version = "0.4.1" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" [[package]] name = "proc-macro2" -version = "0.4.6" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ - "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid", ] [[package]] name = "quote" -version = "0.6.3" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" dependencies = [ - "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "serde" -version = "1.0.66" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" -version = "1.0.66" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" dependencies = [ - "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2", + "quote", + "syn", ] [[package]] name = "serde_json" -version = "1.0.20" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" dependencies = [ - "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa", + "ryu", + "serde", ] [[package]] name = "syn" -version = "0.14.2" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" dependencies = [ - "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] name = "unicode-xid" -version = "0.1.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" -"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682" -"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6" -"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035" -"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" -"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79" -"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875" -"checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d" -"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/rust-module-hello-world/module-src/Cargo.toml b/rust-module-hello-world/module-src/Cargo.toml index 082f05931..532e6a935 100644 --- a/rust-module-hello-world/module-src/Cargo.toml +++ b/rust-module-hello-world/module-src/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "helloworld" version = "0.1.0" -authors = ["Sviatoslav Sydorenko "] +authors = ["Sviatoslav Sydorenko ", "Follpvosten "] +edition = "2018" [dependencies] -serde = "1.0.66" -serde_derive = "1.0.66" -serde_json = "1.0.20" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +anyhow = "1" diff --git a/rust-module-hello-world/module-src/src/main.rs b/rust-module-hello-world/module-src/src/main.rs index bbd7288a4..2e0ffce7b 100644 --- a/rust-module-hello-world/module-src/src/main.rs +++ b/rust-module-hello-world/module-src/src/main.rs @@ -1,107 +1,51 @@ -extern crate serde; -extern crate serde_json; - -use std::env; -use std::fs::File; -use std::io::prelude::*; -use std::process; - -#[macro_use] -extern crate serde_derive; - -use serde_json::Error; - +use anyhow::{anyhow, Context}; +use serde::{Deserialize, Serialize}; +use std::{env, fs, process}; fn default_name_arg() -> String { String::from("World") } - - -#[derive(Serialize, Deserialize)] +#[derive(Deserialize)] struct ModuleArgs { #[serde(default = "default_name_arg")] name: String, } - -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize)] struct Response { - msg: String, - changed: bool, - failed: bool, -} - - -fn exit_json(response_body: Response) { - return_response(response_body) + msg: String, + changed: bool, + failed: bool, } - -fn fail_json(response_body: Response) { - let failed_response = &mut response_body.clone(); - failed_response.failed = true; - return_response(failed_response.clone()) -} - - -fn return_response(resp: Response) { - println!("{}", serde_json::to_string(&resp).unwrap()); - process::exit(resp.failed as i32); -} - - -fn read_file_contents(file_name: &str) -> Result> { - let mut json_string = String::new(); - File::open(file_name)?.read_to_string(&mut json_string)?; - Ok(json_string) -} - - -fn parse_module_args(json_input: String) -> Result { - Ok( - ModuleArgs::from( - serde_json::from_str( - json_input.as_str() - )? - ) - ) -} - - fn main() { - let args: Vec = env::args().collect(); - let program = &args[0]; - let input_file_name = match args.len() { - 2 => &args[1], - _ => { - eprintln!("module '{}' expects exactly one argument!", program); - fail_json(Response { - msg: "No module arguments file provided".to_owned(), + let (response, code) = match run_module() { + Ok(response) => (response, 0), + Err(err) => ( + Response { + msg: err.to_string(), changed: false, failed: true, - }); - "" - } + }, + 1, + ), }; - let json_input = read_file_contents(input_file_name).map_err(|err| { - eprintln!("Could not read file '{}': {}", input_file_name, err); - fail_json(Response { - msg: format!("Could not read input JSON file '{}': {}", input_file_name, err), - changed: false, - failed: true, - }) - }).unwrap(); - let module_args = parse_module_args(json_input).map_err(|err| { - eprintln!("Error during parsing JSON module arguments: {}", err); - fail_json(Response { - msg: format!("Malformed input JSON module arguments: {}", err), - changed: false, - failed: true, - }) - }).unwrap(); - exit_json(Response { - msg: format!("Hello, {}!", module_args.name.as_str()), + println!("{}", serde_json::to_string(&response).unwrap()); + process::exit(code); +} + +fn run_module() -> anyhow::Result { + let input_filename = env::args().nth(1).ok_or(anyhow!( + "module '{}' expects exactly one argument!", + env::args().next().unwrap() + ))?; + let json_input = fs::read_to_string(&input_filename) + .with_context(|| format!("Could not read file '{}'", input_filename))?; + let module_args: ModuleArgs = serde_json::from_str(&json_input) + .with_context(|| format!("Malformed input JSON module arguments"))?; + Ok(Response { + msg: format!("Hello, {}!", module_args.name), changed: true, failed: false, - }); + }) }