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", "libc",
] ]
[[package]]
name = "itoa"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -168,11 +174,50 @@ dependencies = [
"getrandom", "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]] [[package]]
name = "skyums-protocol" name = "skyums-protocol"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"rand", "rand",
"serde",
"serde_json",
"structopt", "structopt",
] ]

View File

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

View File

@ -25,6 +25,7 @@
extern crate rand; extern crate rand;
use structopt::StructOpt; use structopt::StructOpt;
use serde::{Serialize,Deserialize};
// define the commandline parameters // define the commandline parameters
#[derive(StructOpt)] #[derive(StructOpt)]
@ -33,18 +34,24 @@ struct Opt
{ {
#[structopt(short, long, default_value="100", help = "The number of inmates in the problem")] #[structopt(short, long, default_value="100", help = "The number of inmates in the problem")]
size:u32, size:u32,
#[structopt(long, help = "Print to stdout the JSON for the results")]
json:bool,
} }
// define the prisoner data // define the prisoner data
#[derive(Serialize, Deserialize)]
struct Prisoner struct Prisoner
{ {
// prisoner id number
id:u32,
// if the prisoner found his number // if the prisoner found his number
found:bool, found:bool,
// number of attempt he found it in // number of attempt he found it in
attempts:u32, attempts:u32,
} }
fn main() { fn main()
{
// read the commandline parameters // read the commandline parameters
let opts = Opt::from_args(); let opts = Opt::from_args();
@ -77,6 +84,8 @@ fn main() {
{ {
// create the prisoner for index `i` // create the prisoner for index `i`
let mut prisoner_i = Prisoner { let mut prisoner_i = Prisoner {
// assign the id number
id: i,
// assume the prisoner does not find his box // assume the prisoner does not find his box
found: false, found: false,
// set number of attempts to 0 // set number of attempts to 0
@ -108,6 +117,8 @@ fn main() {
} }
// if any prisoner has not found their number // if any prisoner has not found their number
if !opts.json
{
if prisoners.iter().any(|i| !i.found) if prisoners.iter().any(|i| !i.found)
{ {
println!("failure"); println!("failure");
@ -116,4 +127,10 @@ fn main() {
{ {
println!("success"); println!("success");
} }
}
else
{
let out_json = serde_json::to_string(&prisoners).unwrap();
println!("{}", out_json);
}
} }