From c9e8b7db4481cd12aeb452784bfd2061fc429d58 Mon Sep 17 00:00:00 2001 From: Kushal Date: Sun, 28 Jul 2024 14:51:25 -0400 Subject: [PATCH] ask for consent store this information as a configuration --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4a4cb7..1a65a8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -133,6 +133,17 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + [[package]] name = "fastrand" version = "2.1.0" @@ -266,8 +277,20 @@ name = "helloworld" version = "0.1.1" dependencies = [ "dotenv", + "etcetera", "rand", "reqwest", + "serde", + "serde_json", +] + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0988c7e..20e3d0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" rand = "0.8.5" reqwest = { version = "0.12.5", features = ["blocking", "json"] } dotenv = "0.15" +etcetera = "0.8.0" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs index d71b840..a3c0e0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,46 @@ mod greatest_common_divisor; use rand::Rng; use std::cmp::Ordering; +use std::fs; use std::io; use reqwest::blocking::Client; use reqwest::Error; +use etcetera::{choose_app_strategy, AppStrategyArgs, AppStrategy}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +struct Config { + analytics_consent: bool, +} fn main() -> Result<(), Error> { - play_guessing_game()?; + let strategy = choose_app_strategy(AppStrategyArgs { + top_level_domain: "org".to_string(), + author: "Kushal Hada".to_string(), + app_name: "KusGuessingGame".to_string(), + }).unwrap(); + + let config_path = strategy.config_dir().join("config.json"); + let config = if config_path.exists() { + let config_data = fs::read_to_string(&config_path).expect("Unable to read config file"); + serde_json::from_str(&config_data).expect("Unable to parse config file") + } else { + println!("Do you consent to analytics? (yes/no)"); + let mut consent = String::new(); + io::stdin().read_line(&mut consent).expect("Failed to read line"); + let analytics_consent = matches!(consent.trim().to_lowercase().as_str(), "yes" | "y"); + let new_config = Config { analytics_consent }; + let config_data = serde_json::to_string(&new_config).expect("Unable to serialize config"); + fs::create_dir_all(strategy.config_dir()).expect("Unable to create config directory"); + fs::write(&config_path, config_data).expect("Unable to write config file"); + new_config + }; + + play_guessing_game(config.analytics_consent)?; Ok(()) } -fn play_guessing_game() -> Result<(), Error> { +fn play_guessing_game(analytics_consent: bool) -> Result<(), Error> { println!("Guess the number!"); let secret_number = rand::thread_rng().gen_range(1..=100); @@ -40,7 +70,9 @@ fn play_guessing_game() -> Result<(), Error> { Ordering::Greater => println!("Too big!"), Ordering::Equal => { println!("You win!"); - fetch_hello_world()?; + if analytics_consent { + fetch_hello_world()?; + } println!("Press Enter to exit..."); let mut exit = String::new(); io::stdin().read_line(&mut exit).expect("Failed to read line");