I want to write a function that can load data from JSON strings. However, if there are different structs and Keys, I dont know how to pass to function.
#[derive(Serialize, Deserialize, Debug)]
pub struct TestOne
{
pub a : u64,
pub b : u64
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TestTwo
{
pub c : String,
pub d : u64
}
let mut map : HashMap<u64,u64> = HashMap::new();
let file_read = File::open("file_one,json".to_string()).unwrap();
for line in BufReader::new(file_read).lines()
{
let mut line_struct: TestOne = serde_json::from_str(&line.unwrap().to_string()).unwrap();
map.insert(line_struct.a, line_struct.b);
}
Now, I want to make that as function to pass struct values so that I can reuse the same code. Something like this,
pub fn load_file(file_name : String, Struct_definition : ???, key : ??? , value : ??? ) ->HashMap<u64,u64>
{
let mut map : HashMap<u64,u64> = HashMap::new();
let file_read = File::open(file_name).unwrap();
for line in BufReader::new(file_read).lines()
{
let mut line_struct: Struct_definition = serde_json::from_str(&line.unwrap().to_string()).unwrap();
map.insert(line_struct.key, line_struct.value); //want to use the struct values like this
}
map
}
In other words, I want to write a common function for all these kind of loading files into memory.