Export prisoner data to JSON.
This commit is contained in:
parent
33cb815296
commit
b9c2ae82d6
45
Cargo.lock
generated
45
Cargo.lock
generated
@ -78,6 +78,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@ -168,11 +174,50 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.138"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.138"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "skyums-protocol"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"structopt",
|
||||
]
|
||||
|
||||
|
@ -13,3 +13,5 @@ license = "Zlib"
|
||||
[dependencies]
|
||||
rand = "0.8"
|
||||
structopt = "0.3"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
25
src/main.rs
25
src/main.rs
@ -25,6 +25,7 @@
|
||||
extern crate rand;
|
||||
|
||||
use structopt::StructOpt;
|
||||
use serde::{Serialize,Deserialize};
|
||||
|
||||
// define the commandline parameters
|
||||
#[derive(StructOpt)]
|
||||
@ -33,18 +34,24 @@ struct Opt
|
||||
{
|
||||
#[structopt(short, long, default_value="100", help = "The number of inmates in the problem")]
|
||||
size:u32,
|
||||
#[structopt(long, help = "Print to stdout the JSON for the results")]
|
||||
json:bool,
|
||||
}
|
||||
|
||||
// define the prisoner data
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Prisoner
|
||||
{
|
||||
// prisoner id number
|
||||
id:u32,
|
||||
// if the prisoner found his number
|
||||
found:bool,
|
||||
// number of attempt he found it in
|
||||
attempts:u32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main()
|
||||
{
|
||||
// read the commandline parameters
|
||||
let opts = Opt::from_args();
|
||||
|
||||
@ -77,6 +84,8 @@ fn main() {
|
||||
{
|
||||
// create the prisoner for index `i`
|
||||
let mut prisoner_i = Prisoner {
|
||||
// assign the id number
|
||||
id: i,
|
||||
// assume the prisoner does not find his box
|
||||
found: false,
|
||||
// set number of attempts to 0
|
||||
@ -108,12 +117,20 @@ fn main() {
|
||||
}
|
||||
|
||||
// if any prisoner has not found their number
|
||||
if prisoners.iter().any(|i| !i.found)
|
||||
if !opts.json
|
||||
{
|
||||
println!("failure");
|
||||
if prisoners.iter().any(|i| !i.found)
|
||||
{
|
||||
println!("failure");
|
||||
}
|
||||
else
|
||||
{
|
||||
println!("success");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
println!("success");
|
||||
let out_json = serde_json::to_string(&prisoners).unwrap();
|
||||
println!("{}", out_json);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user