use serde_json;
use std::io;
use crate::constants::urls::URL_REGISTRY_STYLES_JSON;
use super::init_fetch_functions::handle_fetch_from_init;
const LABEL: &str = "label";
pub async fn handle_index_styles() {
let styles_index_result = handle_fetch_from_init(URL_REGISTRY_STYLES_JSON).await;
if let Ok(styles_index) = styles_index_result {
let vec_styles: Vec<serde_json::Value> = serde_json::from_str(&styles_index).unwrap();
ask_user_choose_style(vec_styles);
}
}
fn ask_user_choose_style(vec_styles: Vec<serde_json::Value>) {
for (index, style) in vec_styles.iter().enumerate() {
if let Some(label) = style.get(LABEL) {
println!("{}: {}", index + 1, label);
}
}
println!("Please choose a style by entering the corresponding number:");
let mut user_input = String::new();
io::stdin().read_line(&mut user_input).expect("Failed to read line");
if let Ok(index) = user_input.trim().parse::<usize>() {
if index > 0 && index <= vec_styles.len() {
if let Some(selected_style) = vec_styles.get(index - 1) {
if let Some(label) = selected_style.get(LABEL) {
println!("You selected: {}", label);
}
}
} else {
println!(
"Invalid choice. Please select a number between 1 and {}.",
vec_styles.len()
);
}
} else {
println!("Invalid input. Please enter a number.");
}
}