Export prisoner data to JSON.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-07-02 12:04:43 +02:00
parent 33cb815296
commit b9c2ae82d6
3 changed files with 68 additions and 4 deletions

45
Cargo.lock generated
View File

@ -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",
]

View File

@ -13,3 +13,5 @@ license = "Zlib"
[dependencies]
rand = "0.8"
structopt = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -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);
}
}