hashmap -> struct
This commit is contained in:
35
src/json.rs
35
src/json.rs
@@ -1,26 +1,33 @@
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::io::{ErrorKind, Read};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn get_json(path: &Path) -> HashMap<String, String> {
|
||||
let mut data = HashMap::new();
|
||||
pub struct JsonStruct {
|
||||
pub ha_url: String,
|
||||
pub access_token: String,
|
||||
pub light_entity_id: String,
|
||||
}
|
||||
|
||||
impl JsonStruct {
|
||||
fn validate_content(&self) -> bool {
|
||||
!self.ha_url.is_empty() && !self.access_token.is_empty() && !self.light_entity_id.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_json(path: &Path) -> JsonStruct {
|
||||
let json_data = read_file(path);
|
||||
|
||||
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() {
|
||||
|
||||
let data = JsonStruct {
|
||||
ha_url: json::stringify(parsed_data["ha_url"].clone()),
|
||||
access_token: json::stringify(parsed_data["access_token"].clone()),
|
||||
light_entity_id: json::stringify(parsed_data["light_entity_id"].clone()),
|
||||
};
|
||||
|
||||
if !data.validate_content() {
|
||||
panic!("[ERROR] JSON data is NULL");
|
||||
}
|
||||
|
||||
data.insert("ha_url".to_string(), json::stringify(ha_url));
|
||||
data.insert("access_token".to_string(), json::stringify(access_token));
|
||||
data.insert("light_entity_id".to_string(), json::stringify(light_entity_id));
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user