use std::{fs, path::PathBuf};
use serde::Deserialize;
use toml::{self, de::Error};
#[derive(Debug, Clone, Deserialize)]
pub struct PSpeaker{
pub name: String,
pub title: String,
pub label: String,
pub function: String,
pub affiliation: String,
pub description: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct PVecSpeaker{
pub speaker: Vec<PSpeaker>,
}
pub fn load_vec_speaker(filename: &PathBuf) -> Result<PVecSpeaker, Error> {
let vec_speaker: PVecSpeaker = match fs::read_to_string(filename) {
Ok(str) => match toml::from_str(&str){
Ok(value) => value,
Err(error) => panic!("load_vec_speaker : cannot parse file {:?}\n\tParsing error {}", filename, error)
},
Err(err) => panic!("load_vec_speaker : cannot read file {:?}\n\tError: {}", filename, err)
};
Ok(vec_speaker)
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_load_vec_speaker(){
let vec_speaker: PVecSpeaker = load_vec_speaker(&PathBuf::from("tests/timetable/speaker.toml")).unwrap();
assert_eq!(vec_speaker.speaker.len(), 1);
let speaker = vec_speaker.speaker.first().unwrap();
assert_eq!(speaker.name, String::from("Pierre Aubert"));
assert_eq!(speaker.title, String::from("Dr"));
assert_eq!(speaker.label, String::from("pierre_aubert"));
assert_eq!(speaker.function, String::from("Research Engineer at LAPP"));
assert_eq!(speaker.affiliation, String::from("LAPP / IN2P3 / CNRS"));
assert_eq!(speaker.description, String::from("Pierre Aubert is a software developer specialized in code optimization for CPUs and GPUs, mainly on modern C++, Rust and also on Python. He works at LAPP, the Laboratory of Annecy of Particle Physics."));
}
}