Traits
unknown
rust
2 years ago
5.9 kB
11
Indexable
extern crate serde_json; extern crate serde; #[macro_use] extern crate serde_derive; use std::fmt; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] struct GPS{ lat: f64, lon: f64, } #[derive(Debug)] enum EmployeeType { Teacher, Doctor, Police, Invalid, } #[derive(Debug, Serialize, Deserialize)] struct Teacher{ name: String, age: i64, location: GPS, student: Vec<String>, } struct Police{ name: String, age: i64, location: GPS, weapon: Vec<String>, } struct Doctor{ name: String, age: i64, location: GPS, patient: Vec<String>, } trait Employee{ fn find_location(&self) -> GPS; fn find_name(&self) -> &String; fn find_age(&self) -> i64; fn find_properties(&self) -> &Vec<String>; } impl Employee for Teacher{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.student } } impl Employee for Police{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.weapon } } impl Employee for Doctor{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.patient } } impl fmt::Display for Teacher { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "\nRole: Teacher\n \tName: {}\n \tAge: {}\n \tStudent: {:?}\n \tPos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } impl fmt::Display for Police { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "\nRole: Police\n \tName: {}\n \tAge: {}\n \tWeapon: {:?}\n \tPos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } impl fmt::Display for Doctor { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "\nRole: Doctor\n \tName: {}\n \tAge: {}\n \tPatient: {:?}\n \tPos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } fn read_file(dir: String) -> Vec<serde_json::Value>{ let data = std::fs::read_to_string(dir).unwrap(); let serde_data: serde_json::Value = serde_json::from_str(data.as_str()).unwrap(); let data = serde_data.as_array(); let data = data.unwrap(); // for person in data { // println!("{}", person["name"]); // } data.to_vec() } fn assign_role(data: serde_json::Value) -> EmployeeType{ let mut x = data["employee_type"].to_string(); x.pop(); x.remove(0); let mut role = EmployeeType::Invalid; if x == "Teacher"{ role = EmployeeType::Teacher; }else if x == "Doctor" { role = EmployeeType::Doctor; }else if x == "Police" { role = EmployeeType::Police; } role } fn assign_teacher(data: &serde_json::Value){ let p = data["student"].to_string(); let properties = format_vector(p); let person = Teacher{ name: data["name"].to_string(), age: data["age"].to_string().parse::<i64>().unwrap(), student: properties, location: GPS { lat: data["location"]["lat"].to_string().parse::<f64>().unwrap(), lon: data["location"]["lon"].to_string().parse::<f64>().unwrap() } }; println!("{}", person); } fn assign_doctor(data: &serde_json::Value){ let p = data["patient"].to_string(); let properties = format_vector(p); let person = Doctor{ name: data["name"].to_string(), age: data["age"].to_string().parse::<i64>().unwrap(), patient: properties, location: GPS { lat: data["location"]["lat"].to_string().parse::<f64>().unwrap(), lon: data["location"]["lon"].to_string().parse::<f64>().unwrap() } }; println!("{}", person); } fn assign_police(data: &serde_json::Value){ let p = data["weapon"].to_string(); let properties = format_vector(p); let person = Police{ name: data["name"].to_string(), age: data["age"].to_string().parse::<i64>().unwrap(), weapon: properties, location: GPS { lat: data["location"]["lat"].to_string().parse::<f64>().unwrap(), lon: data["location"]["lon"].to_string().parse::<f64>().unwrap() } }; println!("{}", person); } fn format_vector(x : String) -> Vec<String>{ let split = x.split(","); let pvec = split.collect::<Vec<&str>>(); let mut pvec_string: Vec<String> = Vec::new(); for i in pvec{ let i_string = i.to_string(); let i_string = i_string.replace(&['\"', '\'', '[', ']'], ""); pvec_string.push(i_string); } pvec_string } fn main() { let text = read_file("./text.json".to_string()); let mut avg_age = 0; for i in &text{ let role = assign_role(i.clone()); match role { EmployeeType::Teacher => assign_teacher(&i), EmployeeType::Doctor => assign_doctor(&i), EmployeeType::Police => assign_police(&i), EmployeeType::Invalid => println!("Invalid"), } avg_age += i["age"].to_string().parse::<i64>().unwrap(); } println!("The average age of employees: {}", avg_age/(text.len() as i64)); }
Editor is loading...