fixed panic messages

This commit is contained in:
Martin Vrhovšek 2025-01-18 23:42:10 +01:00
parent 34a23ea200
commit 5fca81c6bf

View File

@ -1,25 +1,20 @@
use std::path::Path;
use std::fs::File;
use std::io::{ErrorKind, Read};
use std::process::exit;
use std::collections::HashMap;
pub fn get_json(path: &Path) -> HashMap<String, String> {
let mut data = HashMap::new();
let json_data = read_file(path);
let parsed_data = json::parse(json_data.as_str()).unwrap_or_else(|e| {
eprintln!("[ERROR] Invalid JSON: {e}");
exit(1);
});
let parsed_data = json::parse(json_data.as_str()).expect("[ERROR] Invalid JSON");
let ha_url = parsed_data["ha_url"].clone();
let access_token = parsed_data["access_token"].clone();
let light_entity_id = parsed_data["light_entity_id"].clone();
if ha_url.is_null() || access_token.is_null() || light_entity_id.is_null() {
eprintln!("[ERROR] JSON data is NULL");
exit(1);
panic!("[ERROR] JSON data is NULL");
}
data.insert("ha_url".to_string(), json::stringify(ha_url));
@ -44,11 +39,8 @@ fn read_file(path: &Path) -> String {
}
},
Err(err) => match err.kind() {
ErrorKind::NotFound => {
eprintln!("[ERROR] The file '{}' does not exist", path.display());
exit(1);
},
_ => panic!("[ERROR] Unexpected error, closing ..."),
ErrorKind::NotFound => panic!("[ERROR] The file '{}' does not exist", path.display()),
_ => panic!("[ERROR] Unexpected error reading the JSON"),
}
}
}