Refactored code for readability

This commit is contained in:
scorpion9979 2019-06-03 12:21:38 +03:00
parent 50acaf0ae1
commit da01e797a3

View File

@ -1,6 +1,7 @@
use rand::seq::SliceRandom;
use serde_json;
use std::fs::File;
use std::io::Error;
pub enum NpcKind {
Wolf,
@ -16,11 +17,14 @@ impl NpcKind {
}
}
const npc_names_dir: &str = "common/assets/npc_names.json";
pub fn get_npc_name(npc_type: NpcKind) -> String {
let file = File::open("common/assets/npc_names.json").expect("file should open read only");
let json: serde_json::Value =
serde_json::from_reader(file).expect("file should be proper JSON");
let npc_names = json
let npc_names_file =
File::open(npc_names_dir).expect(&format!("opening {} in read-only mode", npc_names_dir));
let npc_names_json: serde_json::Value = serde_json::from_reader(npc_names_file)
.expect(&format!("reading json contents from {}", npc_names_dir));
let npc_names = npc_names_json
.get(npc_type.as_str())
.expect("accessing json using NPC type provided as key")
.as_array()