fix: unwrap data so i can access the struct

This commit is contained in:
Daniel Alejandro Gallegos 2023-11-12 14:54:29 -05:00
parent 4864f6fb19
commit 82aa779331
Signed by: taco
GPG key ID: 5A09E616957C4F12
3 changed files with 77 additions and 3 deletions

66
Cargo.lock generated
View file

@ -2,12 +2,29 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "getrandom"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.2" version = "0.14.2"
@ -24,12 +41,24 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.69" version = "1.0.69"
@ -48,6 +77,36 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.192" version = "1.0.192"
@ -81,6 +140,7 @@ dependencies = [
name = "strongbad_email" name = "strongbad_email"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"rand",
"serde", "serde",
"toml", "toml",
] ]
@ -136,6 +196,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "winnow" name = "winnow"
version = "0.5.19" version = "0.5.19"

View file

@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.8.5"
serde = { version = "1.0.192", features = ["derive"] } serde = { version = "1.0.192", features = ["derive"] }
toml = "0.8.8" toml = "0.8.8"

View file

@ -1,3 +1,5 @@
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::Deserialize; use serde::Deserialize;
use std::fs; use std::fs;
use toml; use toml;
@ -15,8 +17,13 @@ struct Emails {
fn main() { fn main() {
println!("Preeeeow, world."); println!("Preeeeow, world.");
let toml_file = fs::read_to_string("src/sbemails.toml").expect("failed to read sbemails.toml"); let toml_file = fs::read_to_string("src/sbemails.toml")
let sbemails: Sbemails = toml::from_str(&toml_file).expect("failed to deserialize sbemails.toml"); .expect("failed to read sbemails.toml");
println!("{:#?}", sbemails); let sbemails: Sbemails = toml::from_str(&toml_file)
.expect("failed to deserialize sbemails.toml");
let mut random_number = thread_rng();
let sbemail = sbemails.emails.choose(&mut random_number).unwrap();
println!("sbemail number: {:?}", sbemail.number);
println!("sbemail title: {:?}", sbemail.title);
} }