Traits

mail@pastecode.io avatar
unknown
rust
2 years ago
5.0 kB
0
Indexable
Never
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, "Role: 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, "Role: 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, "Role: 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) -> 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();

    serde_data
}

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 person = Teacher{
        name: data["name"].to_string(), 
        age: data["age"].to_string().parse::<i64>().unwrap(), 
        student: vec![data["student"].to_string()], 
        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 person = Doctor{
        name: data["name"].to_string(), 
        age: data["age"].to_string().parse::<i64>().unwrap(), 
        patient: vec![data["patient"].to_string()], 
        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 person = Police{
        name: data["name"].to_string(), 
        age: data["age"].to_string().parse::<i64>().unwrap(), 
        weapon: vec![data["student"].to_string()], 
        location: GPS { lat: data["location"]["lat"].to_string().parse::<f64>().unwrap(), lon: data["location"]["lon"].to_string().parse::<f64>().unwrap() }
    };
    println!("{}", person);
}

fn main() {
    let text = read_file("./text.json".to_string());
    let role = assign_role(text.clone());

    match role {
        EmployeeType::Teacher => assign_teacher(text),
        EmployeeType::Doctor =>  assign_doctor(text),
        EmployeeType::Police =>  assign_police(text),
        EmployeeType::Invalid => println!("Invalid"),
    }

    }